Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example click API usage #6307

Merged
merged 9 commits into from
Jan 3, 2023
25 changes: 25 additions & 0 deletions core/dbt/cli/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import click
import sys

from dbt.cli.main import cli as dbt, deps


def make_context(args):
try:
ctx = dbt.make_context('dbt', args)
except click.exceptions.Exit:
exit()

ctx.invoked_subcommand = ctx.protected_args[0] if ctx.protected_args else None
return ctx

# python core/dbt/cli/example.py
# python core/dbt/cli/example.py --version
# python core/dbt/cli/example.py deps --project-dir <project-dir-path>
if __name__ == "__main__":
ctx = make_context(sys.argv[1:])
MichelleArk marked this conversation as resolved.
Show resolved Hide resolved
dbt.invoke(ctx)

# Skips group-level context setting
# dbt.commands['deps'].invoke(ctx)
# deps.invoke(ctx)
2 changes: 1 addition & 1 deletion core/dbt/cli/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def assign_params(ctx):
invoked_subcommand = getattr(import_module("dbt.cli.main"), ctx.invoked_subcommand)
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, ctx.args or sys.argv)
assign_params(invoked_subcommand_ctx)

# Hard coded flags
Expand Down
15 changes: 8 additions & 7 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ def cli(ctx, **kwargs):
"""
ctx.obj = {}
flags = Flags()

# Version info
if flags.VERSION:
click.echo(f"`version` called\n ctx.params: {pf(ctx.params)}")
return
else:
del ctx.params["version"]

# Logging
# N.B. Legacy logger is not supported
setup_event_logger(
Expand All @@ -83,13 +91,6 @@ def cli(ctx, **kwargs):
# Adapter management
ctx.with_resource(adapter_management())

# Version info
if flags.VERSION:
click.echo(f"`version` called\n ctx.params: {pf(ctx.params)}")
return
else:
del ctx.params["version"]


# dbt build
@cli.command("build")
Expand Down