diff --git a/core/dbt/cli/context.py b/core/dbt/cli/context.py index e0cf27624ca..62b500110b5 100644 --- a/core/dbt/cli/context.py +++ b/core/dbt/cli/context.py @@ -4,19 +4,21 @@ from dbt.cli.main import cli from dbt.config.project import Project + class DBTUsageException(Exception): pass + class DBTContext(Context): def __init__(self, args: List[str]) -> None: - try: + try: ctx = cli.make_context(cli.name, args) if args: cmd_name, cmd, cmd_args = cli.resolve_command(ctx, args) - cmd.make_context(cmd_name, cmd_args, parent=ctx) + cmd.make_context(cmd_name, cmd_args, parent=ctx) # type: ignore except (NoSuchOption, UsageError) as e: - raise DBTUsageException(e.message) - + raise DBTUsageException(e.message) + ctx.obj = {} # yikes? self.__dict__.update(ctx.__dict__) @@ -24,14 +26,14 @@ def __init__(self, args: List[str]) -> None: # @classmethod # def from_args(cls, args: List[str]) -> "DBTContext": - # try: + # try: # ctx = cli.make_context(cli.name, args) # if args: # cmd_name, cmd, cmd_args = cli.resolve_command(ctx, args) # cmd.make_context(cmd_name, cmd_args, parent=ctx) # except (NoSuchOption, UsageError) as e: - # raise DBTUsageException(e.message) - + # raise DBTUsageException(e.message) + # ctx.obj = {} # # yikes # ctx.__class__ = cls @@ -40,5 +42,5 @@ def __init__(self, args: List[str]) -> None: def set_project(self, project: Project): if not isinstance(project, Project): raise ValueError(f"{project} is a {type(project)}, expected a Project object.") - - self.obj["project"] = project \ No newline at end of file + + self.obj["project"] = project diff --git a/core/dbt/cli/flags.py b/core/dbt/cli/flags.py index a8af62bf61d..f82ce19ee56 100644 --- a/core/dbt/cli/flags.py +++ b/core/dbt/cli/flags.py @@ -1,11 +1,11 @@ # TODO Move this to /core/dbt/flags.py when we're ready to break things import os -import sys from dataclasses import dataclass from importlib import import_module from multiprocessing import get_context from pprint import pformat as pf from typing import Set +from contextvars import ContextVar from click import Context, get_current_context from click.core import ParameterSource @@ -18,6 +18,17 @@ import multiprocessing.popen_spawn_posix # type: ignore # noqa: F401 +INVOCATION_ARGS: ContextVar[list[str]] = ContextVar("invocation_args") + + +def set_invocation_args(args): + INVOCATION_ARGS.set(args) + + +def get_invocation_args(): + return INVOCATION_ARGS.get() + + @dataclass(frozen=True) class Flags: def __init__(self, ctx: Context = None, user_config: UserConfig = None) -> None: @@ -50,7 +61,7 @@ def assign_params(ctx, params_assigned_from_default): invoked_subcommand = getattr(import_module("dbt.cli.main"), invoked_subcommand_name) invoked_subcommand.allow_extra_args = True invoked_subcommand.ignore_unknown_options = True - invoked_subcommand_ctx = invoked_subcommand.make_context(None, sys.argv) + invoked_subcommand_ctx = invoked_subcommand.make_context(None, get_invocation_args()) assign_params(invoked_subcommand_ctx, params_assigned_from_default) if not user_config: diff --git a/core/dbt/cli/main.py b/core/dbt/cli/main.py index 4901caf56f4..09b571d31ed 100644 --- a/core/dbt/cli/main.py +++ b/core/dbt/cli/main.py @@ -1,13 +1,14 @@ import inspect # This is temporary for RAT-ing from copy import copy from pprint import pformat as pf # This is temporary for RAT-ing +import sys import click from dbt.adapters.factory import adapter_management from dbt.cli import params as p -from dbt.cli.flags import Flags from dbt.config import RuntimeConfig from dbt.config.runtime import load_project, load_profile +from dbt.cli.flags import Flags, set_invocation_args from dbt.events.functions import setup_event_logger from dbt.profiler import profiler from dbt.task.deps import DepsTask @@ -20,6 +21,7 @@ def cli_runner(): ls = copy(cli.commands["list"]) ls.hidden = True cli.add_command(ls, "ls") + set_invocation_args(sys.argv[1:]) # Run the cli cli()