首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用argparse的Python中的条件命令行参数

问使用argparse的Python中的条件命令行参数
EN

Stack Overflow用户
提问于 2012-03-01 04:13:15
回答 4查看 26.9K关注 0票数 37

我希望有一个带有--action=标志的程序,其中有效的选项是dump和upload,upload是缺省值。如果(且仅当)选择了dump,我希望还有一个--dump-format=选项。有没有办法用argparse来表达这一点,或者我需要接受所有的参数,然后自己做逻辑。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-01 04:39:05

您可以使用parser.error

代码语言:javascript
复制
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--action', choices=['upload', 'dump'], default='dump')
parser.add_argument('--dump-format')
args = parser.parse_args()
if args.action != 'dump' and args.dump_format:
    parser.error('--dump-format can only be set when --action=dump.')
票数 23
EN

Stack Overflow用户

发布于 2012-12-04 23:37:52

argparse模块提供了一种方法来实现这一点,而无需实现自己的需求检查。下面的例子使用“子解析器”或“子命令”。我为"dump“实现了一个子解析器,为"format”实现了一个子解析器。

代码语言:javascript
复制
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('file', help='The file you want to act on.')
subparsers = parser.add_subparsers(dest='subcommand')
subparsers.required = True  # required since 3.7

#  subparser for dump
parser_dump = subparsers.add_parser('dump')
# add a required argument
parser_dump.add_argument(
    'format',
    choices=['csv', 'json'],
    help='Dump the file in this format.')

#  subparser for upload
parser_upload = subparsers.add_parser('upload')
# add a required argument
parser_upload.add_argument(
    'server',
    choices=['amazon', 'imgur'],
    help='Upload the file to this service.')

args = parser.parse_args()
print args
if args.subcommand == 'dump':
    print 'I will now dump "%s" in the %s format' % (args.file, args.format)
if args.subcommand == 'upload':
    print 'I will now upload "%s" to %s' % (args.file, args.server)

在命令行中如下所示:

代码语言:javascript
复制
$ python ap.py 
usage: ap.py [-h] file {upload,dump} ...
ap.py: error: too few arguments
$ python ap.py tmp.txt 
usage: ap.py [-h] file {upload,dump} ...
ap.py: error: too few arguments
$ python ap.py tmp.txt upload
usage: ap.py file upload [-h] {amazon,imgur}
ap.py file upload: error: too few arguments
$ python ap.py tmp.txt upload amazo
usage: ap.py file upload [-h] {amazon,imgur}
ap.py file upload: error: argument server: invalid choice: 'amazo' (choose from 'amazon', 'imgur')
$ python ap.py tmp.txt upload amazon
Namespace(file='tmp.txt', server='amazon', subcommand='upload')
I will now upload "tmp.txt" to amazon
$ python ap.py tmp.txt upload imgur
Namespace(file='tmp.txt', server='imgur', subcommand='upload')
I will now upload "tmp.txt" to imgur
$ python ap.py tmp.txt dump
usage: ap.py file dump [-h] {csv,json}
ap.py file dump: error: too few arguments
$ python ap.py tmp.txt dump csv
Namespace(file='tmp.txt', format='csv', subcommand='dump')
I will now dump "tmp.txt" in the csv format
$ python ap.py tmp.txt dump json
Namespace(file='tmp.txt', format='json', subcommand='dump')
I will now dump "tmp.txt" in the json format

更多信息:http://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.add_subparsers

票数 69
EN

Stack Overflow用户

发布于 2012-03-01 04:53:32

另一种解决问题的方法是使用subcommands (a‘’la git),并将"action“作为第一个参数:

代码语言:javascript
复制
script dump --dump-format="foo"
script upload
票数 43
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9505898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档