最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

command line - Python click CLI subcommand multiple times - Stack Overflow

programmeradmin0浏览0评论

I am new to the click CLI library, so this might be something trivial.

I have a script which has one group and one subcommand, something like this. It is a ML application which has a training phase and evaluation phase.

import click

@click.group()
@click.option('--learning_rate', type=click.FLOAT, default=1.e-4)
@click.pass_context
def train(ctx: click.Context, learning_rate: float):
    ...
    ctx.ensure_object(dict)
    ctx.obj['model'] = 'Trained Model'
    click.echo('Trained complete')

@trainmand
@click.option('--eval_param', type=click.INT)
@click.pass_context
def eval(ctx: click.Context, eval_param: int):
    ...
    trained_model = ctx.obj['model']
    click.echo(f'Using {trained_model} for evaluation using eval parameter {eval_param}')

if __name__ == '__main__':
    train()

I can use it like this

>> python ml.py --learning_rate 0.001 eval --eval_param 0
Trained complete
Using Trained Model for evaluation using eval parameter 0

However, I would like to run the evaluation process arbitrarily many times with different --eval_param. Only the parameters change -- the eval() function should remain same. But, dependning on the eval_param, the result changes. BTW, the different evals are independent of each other, given the trained model.

So, I want something like the following

python ml.py --learning_rate 0.001 eval --eval_param 0 eval --eval_param 1 eval --eval_param 2
Trained complete
Using Trained Model for evaluation using eval parameter 0
Using Trained Model for evaluation using eval parameter 1
Using Trained Model for evaluation using eval parameter 2

I can't really figure out what the right approach is. Any help is appreciated.

发布评论

评论列表(0)

  1. 暂无评论