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

argparse - Different mandatory / available parameters per action

我正在寻找具有以下结构的参数解析器:

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 模块实现可选参数,具体取决于作为另一个命令行参数所做的选择。

我想让 ArgParse 失败,例如如果命令行参数是

--d database_name --a backup --what_file_to_consistency_check /var/tmp/file.bak

这是我到目前为止所得到的(我知道它很少,但如果我从一开始就没有得到正确的结果,我不想完全错误地使用子解析器)

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

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

if __name__ == "__main__":

    action = sys.argv[1]

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

执行此操作的常规方法如下所示:

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

...用法为:

# here, action='backup' and dest='/path/to/dest'
yourtool -d db -t 15 backup /path/to/dest

...或者...

# here, action='consistency_check' and source='/path/to/content/to/check'
yourtool -d db -t 15 consistency_check /path/to/content/to/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', 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 参数。或者让它成为一个位置。否则请考虑如果用户不提供它您将做什么。 IE。如果 args.db is None?是否有可以使用的默认数据库?

看起来所有的操作选择都需要一个文件或目录参数——备份或检查的目标。用户称它为“--where_to_backup_to”还是“--what_file_to_consistency_check”有关系吗?通过在此处使用位置,我要求他们提供某种名称,但您可以根据 'action'.

来解释它

看起来force只是clean的加强版。如果用户指定 --force 而不是 --clean,您认为他们想要什么?在这里,我接受两者并让您的代码选择最有意义的。

我的理念是解析器的主要目标是弄清楚用户想要什么。错误检查在防止模糊输入时最有用。但它不应该挑剔。简单的解析器设计通常比过于复杂的解析器设计要好。