例如,我想以不同的模式运行我的应用程序:
python3 ./myapp.py --mode server
为此,我创建了一个参数解析器并根据选择指定了模式:
_parser = argparse.ArgumentParser(description='some application')
_parser.add_argument('--mode', dest='mode', action='store', nargs=1, required=True,
choices={'server', 'client', 'stat', 'cli'}, help='Run app in selected mode')
在'cli'模式下,我想指定其他参数,例如:
python3 ./myapp.py --mode cli run-command --with-arguments
如何使这样的选项来处理参数?为每种模式使用子解析器,而不是从“选择”中选择?还是有其他方法?
使用子解析器。 AFAIK,参数不支持您想要的嵌套。