-1

I have the following config:

mymodule/__init__.py:

@click.group()
@click.option('--env', required=True, type=str, help='Environment: t1, t2, t22,..., s, p', default=lambda: getenv("APP_ENV"))
@click.option('--load-type', help='Load type', type=str)
@click.option('--start-date', default=None, help='Start date', type=str)
@click.option('--end-date', default=None, help='End date', type=str)
@click.pass_context
def cli(ctx, env, load_type, start_date, end_date):
    ctx.ensure_object(dict)
    ctx['env'] = env
    ctx['load_type'] = load_type
    ctx['start_date'] = start_date
    ctx['end_date'] = end_date

foo.py

from . import cli

@cli.command()
@click.pass_context
def foo(ctx):
    with App(ctx['env'], ctx['load_type'], ctx['start_date'], ctx['end_date']) as app:
        app.start()

And setup.py

    'console_scripts': [
        'app=mymodule:cli'
    ]

When I run app --help, I get this:

(myvenv) bash$ app --help
Usage: app [OPTIONS] COMMAND [ARGS]...

Options:
  --env TEXT         Environment: t1, t2, t22,..., s, p  [required]
  --load-type TEXT   Load type
  --start-date TEXT  Start date
  --end-date TEXT    End date
  --help             Show this message and exit.

However, I don't get a section for available commands one of which is foo

That is, I need to see this:

(myvenv) bash$ app --help
Usage: app [OPTIONS] COMMAND [ARGS]...

Options:
  --env TEXT         Environment: t1, t2, t22,..., s, p  [required]
  --load-type TEXT   Load type
  --start-date TEXT  Start date
  --end-date TEXT    End date
  --help             Show this message and exit.

Commands:
  foo
s5s
  • 11,159
  • 21
  • 74
  • 121
  • @StephenRauch `mymodule` is where the __init__.py script with the click config lies. I've figured it out - everything has to be in the same file (see my answer). One can probably split it into multiple files but then has to call the `add_command()` with the click group which defeats the benefits of having it in multiple files and not having to edit the group file. – s5s Nov 19 '19 at 13:08

1 Answers1

0

So I had to have everything in the same python file like so:

@click.group()
@click.option('--env', required=True, type=str, help='Environment: t1, t2, t22,..., s, p', default=lambda: getenv("APP_ENV"))
@click.option('--load-type', help='Load type', type=str)
@click.option('--start-date', default=None, help='Start date', type=str)
@click.option('--end-date', default=None, help='End date', type=str)
@click.pass_context
def main(ctx, env, load_type, start_date, end_date):
    ctx.ensure_object(dict)
    ctx.obj['env'] = env
    ctx.obj['load_type'] = load_type
    ctx.obj['start_date'] = start_date
    ctx.obj['end_date'] = end_date

def start(ctx, class_type):
    with class_type(env=ctx.obj['env'], load_type=ctx.obj['load_type'], start_date=ctx.obj['start_date'], end_date=ctx.obj['end_date']) as app:
        app.start()


@main.command()
@click.pass_context
def cmd1(ctx):
    start(ctx, iVolatilityPrices)

@main.command()
@click.pass_context
def cmd2(ctx):
    start(ctx, iVolatilityRefData)

@main.command()
@click.pass_context
def cmd3(ctx):
    start(ctx, iVolatilitySurfaceData)

if __name__ == '__main__':
    main(obj={})

Then in setup.py, I point my app command to the main function above. Then I would get the main command show me the following menu:

(myvenv) bash$ app
Usage: app [OPTIONS] COMMAND [ARGS]...

Options:
  --env TEXT         Environment: t1, t2, t22,..., s, p  [required]
  --load-type TEXT   Load type
  --start-date TEXT  Start date
  --end-date TEXT    End date
  --help             Show this message and exit.

Commands:
  cmd1
  cmd2
  cmd3
s5s
  • 11,159
  • 21
  • 74
  • 121