Skip to content

Commit

Permalink
Merge pull request #97 from nicholasyager/nicholasyager-improve_selec…
Browse files Browse the repository at this point in the history
…tor_docs

fix(cli): Create custom Command type to support custom Usage docs
  • Loading branch information
nicholasyager authored Jul 17, 2023
2 parents f462d8a + 29ae018 commit a32f8ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
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 use tuple-type options, since type types will eat
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

0 comments on commit a32f8ba

Please sign in to comment.