Python argparse 示例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20923640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python argparse example?
提问by JChris
I'm trying to learn argparse in order to use it in my program, the syntax should be like this:
我正在尝试学习 argparse 以便在我的程序中使用它,语法应该是这样的:
-a --aLong <String> <String>
-b --bLong <String> <String> <Integer>
-c --cLong <String>
-h --help
I have this code:
我有这个代码:
#!/usr/bin/env python
#coding: utf-8
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Lorem Ipsum')
parser.add_argument('-a','--aLong', help='Lorem Ipsum', required=False)
parser.add_argument('-b','--bLong', help='Lorem Ipsum', required=False)
parser.add_argument('-c','--cLong', help='Lorem Ipsum', required=False)
parser.add_argument('-h','--help', help='Lorem Ipsum', required=False)
parser.parse_args()
The question is, I read in the official doc, saw YouTube videos, etc, but I couldn't understand how can I determine the number of "sub-arguments" of the "main-argument"?
问题是,我阅读了官方文档,看过 YouTube 视频等,但我不明白如何确定“主参数”的“子参数”数量?
Example: myApp.py -b Foobar 9000, how can I set that -bmust have two "sub-arguments", and how can I get the values, Foobarand 9000?
示例:myApp.py -b Foobar 9000,如何设置-b必须有两个“子参数”,以及如何获取值,Foobar以及9000?
And another doubt, I know I can set an argument to be requiredor not, but I wanted to make my program only executes when at least oneargument is passed, any of the four mentioned.
还有一个疑问,我知道我可以设置一个参数为required或不,但我想让我的程序只在至少传递一个参数时执行,提到的四个参数中的任何一个。
Maybe it's a stupid question, but sorry, I can't understand it, and hopefully there is someone here with "teacher powers" to explain it.
也许这是一个愚蠢的问题,但是对不起,我无法理解,希望这里有“教师权力”的人来解释它。
采纳答案by FMc
import argparse
# Use nargs to specify how many arguments an option should take.
ap = argparse.ArgumentParser()
ap.add_argument('-a', nargs=2)
ap.add_argument('-b', nargs=3)
ap.add_argument('-c', nargs=1)
# An illustration of how access the arguments.
opts = ap.parse_args('-a A1 A2 -b B1 B2 B3 -c C1'.split())
print(opts)
print(opts.a)
print(opts.b)
print(opts.c)
# To require that at least one option be supplied (-a, -b, or -c)
# you have to write your own logic. For example:
opts = ap.parse_args([])
if not any([opts.a, opts.b, opts.c]):
ap.print_usage()
quit()
print("This won't run.")
回答by user3366598
The key to this is to define a required, mutually exclusive group.
这样做的关键是定义一个必需的、互斥的组。
import argparse
# Use nargs to specify how many arguments an option should take.
ap = argparse.ArgumentParser()
group = ap.add_mutually_exclusive_group(required=True)
group.add_argument('-a', nargs=2)
group.add_argument('-b', nargs=3)
group.add_argument('-c', nargs=1)
# Grab the opts from argv
opts = ap.parse_args()
# This line will not be reached if none of a/b/c are specified.
# Usage/help will be printed instead.
print(opts)
print(opts.a)
print(opts.b)
print(opts.c)

