argparse每个操作的不同强制/可用参数

2024-06-12 06:53:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望创建一个具有以下结构的参数解析器:

options [ 'backup', 'consistency_check', 'backup_and_consistency_check']

--database [ required ]
--action [ required choice from list options ]
  --where_to_backup_to [ only required if --action is 'backup' ]
  --what_file_to_consistency_check [ only required if --action is 'consistency_check']
--clean [ optional ]
  --force [ optional if --clean is also in arguments ]

如何使用ArgumentParser模块实现可选参数,这取决于作为另一个命令行参数所做的选择。在

如果命令行参数是

^{pr2}$

这就是我到目前为止所得到的(我知道这很少,但如果我从一开始就没有把它弄对的话,我不想让次纵火犯走上完全错误的方向)

actions = ['backup', 'consistency_check', 'backup_and_consistency_check']

def create_parser():
    parser = ArgumentParser(description='Parser for Backup / Consistency Check')

    parser.add_argument('--database', '-d', dest='db', help='Database name', choices=get_active_database_list())

    parser.add_argument('--action', '-a', dest='action', help='Action option', choices=actions)
    # --where_to_backup_to [ only if action = backup ]
    # --what_file_to_consistency_check [ only if action = cc ]
    parser.add_argument('--clean', '-c', dest='clean', help='Clean')
    # --force [ only available if --clean is also in arguments ]

    return parser

Tags: tocleanaddparseronly参数ifis
3条回答

如果子parser现在看起来太复杂了,我认为您仍然可以在没有它们的情况下获得一个有用的解析器:

def create_parser():
    parser = ArgumentParser(description='Parser for Backup / Consistency Check')

    parser.add_argument(' database', '-d', dest='db', help='Database name', choices=get_active_database_list())

    parser.add_argument(' action', '-a', help='Action option', choices=actions)
    parser.add_argument('target', help='target for backup or check')
    parser.add_argument(' clean', '-c', help='Clean') # default dest is 'clean'
    parser.add_argument(' force', help='force clean')
    return parser

如果database是必需的,您可能需要添加一个required=True参数。或者把它定位。否则,请考虑如果用户不提供它,您将怎么做。一、 如果args.db is None?是否有可以使用的默认数据库?在

看起来所有的操作选择都需要一个file或dir参数—备份或检查的目标。用户把它叫做“u”或“u”是什么意思?通过在这里使用位置,我要求他们给出某种名称,但这取决于你根据“动作”来解释它。在

看起来force只是clean的一个更强大的版本。如果用户指定 force而不是{},你认为他们想要什么?在这里,我接受两者,让你的代码选择最有意义的。在

我的哲学是解析器的主要目标是找出用户想要什么。错误检查在防止输入不明确时最有用。但不应该太挑剔。简单的解析器设计通常比过于复杂的解析器设计要好。在

传统的方法看起来更像如下:

def create_parser():
    parser = ArgumentParser(description='Parser for Backup / Consistency Check')

    parser.add_argument(' database', '-d', dest='db', help='Database name', choices=get_active_database_list())
    parser.add_argument(' timeout', '-t', dest='timeout', help='Timeout limit (in minutes)')

    subparsers = parser.add_subparsers()

    parser_backup = subparsers.add_parser('backup', help='Run a backup')
    parser_backup.set_defaults(action='backup') # or even pass the backup function itself, vs a string
    parser_backup.add_argument('dest', help='Where to backup to') # where to backup to

    parser_check = subparsers.add_parser('consistency_check', help='Run a consistency check')
    parser_check.set_defaults(action='consistency_check')
    parser_check.add_argument('source', help='What file to check for consistency')

    return parser

…用法为:

^{pr2}$

……或者。。。在

# here, action='consistency_check' and source='/path/to/content/to/check'
yourtool -d db -t 15 consistency_check /path/to/content/to/check

我认为使用动态选项解析器使动作成为位置参数是一个不错的选择:

if __name__ == "__main__":

    action = sys.argv[1]

    parser = create_parser(action)
    args = parser.parse_args()

相关问题 更多 >