单击不检测组中的命令

click not detecting commands in a group

我有以下配置:

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()

和setup.py

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

当我 运行 app --help 时,我得到这个:

(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.

但是,我没有找到可用命令的部分,其中之一是 foo

也就是说,我需要看到这个:

(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

所以我必须将所有内容都放在同一个 python 文件中,如下所示:

@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={})

然后在 setup.py 中,我将我的 app 命令指向上面的 main 函数。然后我会得到 main 命令显示以下菜单:

(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