Python Argparse - 基于另一个参数的值有条件地需要参数 [英] Python Argparse - conditionally required arguments based on the value of another argument
问题描述
我正在尝试编写一个守护程序服务,该服务可以在命令行中进行控制.比如启动服务
I am trying to write a daemon service, which can be controlled in the command line. For example, to start the service
python3 test.py -c start -d/mydownloadfolder/-j/myconfig.json
停止服务,
python3 test.py -c stop
-d
-j
参数仅在我启动服务时才需要.因此,我需要根据另一个参数的值来实现有条件要求的参数.
The -d
-j
parameters are required only when I start the service. Therefore, I need to implement conditionally required arguments based on the value of another argument.
我进行了一些搜索,发现了这篇有用的帖子 Python Argparse 有条件地要求的参数不同之处在于:我需要检查--command"的值而不是检查--command"的存在.
I did some search and found this useful post Python Argparse conditionally required arguments The difference is: instead of checking the existence of '--command', I need to check the value of the '--command'.
这是我的初步解决方案:
Here is my tentative solution:
PARSER.add_argument('-c', '--command', required=True, help='provide a valid command: start, stop, restart, or status')
NEED_MORE_ARGS = PARSER.parse_args().command.lower().startswith('start')
PARSER.add_argument('-d', '--download', required=NEED_MORE_ARGS , default=LOCAL_DOWNLOAD, help='set account download folder')
PARSER.add_argument('-j', '--input', required=NEED_MORE_ARGS, default=JSON_INPUT, help='set input json file')
我解析了中间的args得到NEED_MORE_ARGS(boolean),然后添加其他args.代码似乎不干净.有没有更好的方法来做到这一点?
I parsed the args in the middle to get NEED_MORE_ARGS(boolean), and then add other args. The code seems not clean. Is there a better way to do this?
==============
==============
更新:暂定解决方案不起作用.:(
Updated: The tentative solution does not work. :(
推荐答案
我认为你可以使用两个解析器来做到这一点:
I think you can use two parsers to do that:
import argparse
if __name__ == '__main__':
command_parser = argparse.ArgumentParser()
command_parser.add_argument('-c', '--command', required=True,
help='provide a valid command: start, stop, restart, or status')
if command_parser.parse_known_args()[0].command.lower().startswith('start'):
option_parser = argparse.ArgumentParser()
option_parser.add_argument('-d', '--download', required=True, help='set account download folder')
option_parser.add_argument('-j', '--input', required=True, help='set input json file')
option_parser.parse_known_args()
或者您可以使用子解析器,这在您的情况下可能更好:
or you can use a subparser, which is probably better in your case:
import argparse
if __name__ == '__main__':
command_parser = argparse.ArgumentParser()
subparsers = command_parser.add_subparsers(help='Choose a command')
start_parser = subparsers.add_parser('start', help='"start" help')
start_parser.add_argument('-d', '--download', required=True, help='set account download folder')
start_parser.add_argument('-j', '--input', required=True, help='set input json file')
start_parser.set_defaults(action=lambda: 'start')
stop_parser = subparsers.add_parser('stop', help='"stop" help')
stop_parser.set_defaults(action=lambda: 'stop')
command_parser.parse_args()
在这种情况下,命令行语法会有所不同:
in this case command line syntax will be bit different:
python3 test.py start -d /mydownloadfolder/ -j /myconfig.json
python3 test.py stop
这篇关于Python Argparse - 基于另一个参数的值有条件地需要参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!