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

fix(cli): Create custom Command type to support custom Usage docs #97

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions dbt_meshify/cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import functools

import click
from click import Context, HelpFormatter
from dbt.cli.options import MultiOption

# define common parameters
Expand Down Expand Up @@ -89,3 +90,16 @@ def wrapper_decorator(*args, **kwargs):
return func(*args, **kwargs)

return wrapper_decorator


class TupleCompatibleCommand(click.Command):
"""
A Click Command with a custom formatter that adds metavar options after all arguments.
This is valuable for commands that uses tuple-type options, since type types will eat
nicholasyager marked this conversation as resolved.
Show resolved Hide resolved
arguments.
"""

def format_usage(self, ctx: Context, formatter: HelpFormatter) -> None:
pieces = self.collect_usage_pieces(ctx)
pieces = pieces[1:] + [pieces[0]]
formatter.write_usage(ctx.command_path, " ".join(pieces))
19 changes: 14 additions & 5 deletions dbt_meshify/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import os
import sys
from pathlib import Path
Expand All @@ -11,6 +12,7 @@
from dbt_meshify.storage.dbt_project_creator import DbtSubprojectCreator

from .cli import (
TupleCompatibleCommand,
create_path,
exclude,
group_yml_path,
Expand All @@ -23,7 +25,7 @@
select,
selector,
)
from .dbt_projects import DbtProject, DbtProjectHolder, DbtSubProject
from .dbt_projects import DbtProject, DbtProjectHolder
from .storage.file_content_editors import DbtMeshConstructor

log_format = "<white>{time:HH:mm:ss}</white> | <level>{level}</level> | <level>{message}</level>"
Expand Down Expand Up @@ -78,15 +80,19 @@ def connect(projects_dir):
print(holder.project_map())


@cli.command(name="split")
@cli.command(
cls=TupleCompatibleCommand,
name="split",
)
@create_path
@click.argument("project_name")
@exclude
@project_path
@read_catalog
@select
@selector
def split(project_name, select, exclude, project_path, selector, create_path, read_catalog):
@click.pass_context
def split(ctx, project_name, select, exclude, project_path, selector, create_path, read_catalog):
"""
Splits out a new subproject from a dbt project by adding all necessary dbt Mesh constructs to the resources based on the selected resources.

Expand Down Expand Up @@ -185,7 +191,10 @@ def add_version(select, exclude, project_path, selector, prerelease, defined_in,
) from e


@operation.command(name="create-group")
@operation.command(
name="create-group",
cls=TupleCompatibleCommand,
)
@click.argument("name")
@exclude
@group_yml_path
Expand Down Expand Up @@ -248,7 +257,7 @@ def create_group(
raise FatalMeshifyException(f"Error creating group: {name}")


@cli.command(name="group")
@cli.command(name="group", cls=TupleCompatibleCommand)
@click.argument("name")
@exclude
@group_yml_path
Expand Down