argparse python模块的文档虽然非常棒,但对于我这个初学者来说,现在还难以理解。我不需要在命令行上做数学运算,也不需要在屏幕上修改格式行或更改选项字符。我想做的就是“如果arg是A,做这个,如果B做那个,如果以上都没有显示帮助并退出”。


当前回答

注意Python HOWTOs中的Argparse教程。它从最基本的例子开始,比如这个:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
args = parser.parse_args()
print(args.square**2)

然后发展到不那么基本的。

这里有一个预定义选项的例子,就像问的那样:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("square", type=int,
                    help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],
                    help="increase output verbosity")
args = parser.parse_args()
answer = args.square**2
if args.verbosity == 2:
    print("the square of {} equals {}".format(args.square, answer))
elif args.verbosity == 1:
    print("{}^2 == {}".format(args.square, answer))
else:
    print(answer)

其他回答

由于你没有澄清参数“A”和“B”是位置的还是可选的,我将两者混合。

默认情况下,位置参数是必需的。如果没有给出一个将抛出'Few arguments given'这不是可选参数的情况下,以他们的名字。默认情况下,此程序将接受一个数字并返回其方框,如果使用cube选项,则将返回其方框。

import argparse
parser = argparse.ArgumentParser('number-game')
parser.add_argument(
    "number",
    type=int,
    help="enter a number"
   )
parser.add_argument(
   "-c", "--choice",
   choices=['square','cube'],
   help="choose what you need to do with the number"
)

# all the results will be parsed by the parser and stored in args
args = parser.parse_args()

# if square is selected return the square, same for cube 
if args.c == 'square':
    print("{} is the result".format(args.number**2))
elif args.c == 'cube':
    print("{} is the result".format(args.number**3))
else:
    print("{} is not changed".format(args.number))

使用

$python3 script.py 4 -c square
16

这里的可选参数是有值的,如果你只是想把它当作一个标志来使用,你也可以。因此,通过对square使用-s,对cube使用-c,我们可以通过添加action = "store_true"来改变行为。仅在使用时才更改为true。

parser.add_argument(
    "-s", "--square",
    help="returns the square of number",
    action="store_true"
    )
parser.add_argument(
    "-c", "--cube",
    help="returns the cube of number",
    action="store_true"
    )

所以条件块可以更改为,

if args.s:
    print("{} is the result".format(args.number**2))
elif args.c:
    print("{} is the result".format(args.number**3))
else:
    print("{} is not changed".format(args.number))

使用

$python3 script.py 4 -c
64

也可以使用plac (argparse的包装器)。

作为奖励,它生成整洁的帮助说明-见下文。

示例脚本:

#!/usr/bin/env python3
def main(
    arg: ('Argument with two possible values', 'positional', None, None, ['A', 'B'])
):
    """General help for application"""
    if arg == 'A':
        print("Argument has value A")
    elif arg == 'B':
        print("Argument has value B")

if __name__ == '__main__':
    import plac
    plac.call(main)

示例输出:

没有提供参数- example.py:

usage: example.py [-h] {A,B}
example.py: error: the following arguments are required: arg

提供了意外的参数- example.py

usage: example.py [-h] {A,B}
example.py: error: argument arg: invalid choice: 'C' (choose from 'A', 'B')

提供正确的参数- example.py

Argument has value A

完整帮助菜单(自动生成)- example.py -h:

usage: example.py [-h] {A,B}

General help for application

positional arguments:
  {A,B}       Argument with two possible values

optional arguments:
  -h, --help  show this help message and exit

简短说明:

实参的名称通常等于形参名称(arg)。

arg参数后的元组注释的含义如下:

描述(有两个可能值的参数) 参数类型- 'flag', 'option'或'positional' (positional)之一 缩写(无) 参数值的类型-例如。float, string (None) 限制选项集(['A', 'B'])

文档:

要了解更多关于使用placa的知识,请查看它的伟大文档:

Plac:解析命令行的简单方法

Matt正在询问argparse中的位置参数,我同意Python文档在这方面是缺乏的。在大约20多页的文章中,没有一个完整的例子同时展示了解析和使用位置参数。

这里的其他答案都没有显示位置参数的完整示例,所以这里有一个完整的示例:

# tested with python 2.7.1
import argparse

parser = argparse.ArgumentParser(description="An argparse example")

parser.add_argument('action', help='The action to take (e.g. install, remove, etc.)')
parser.add_argument('foo-bar', help='Hyphens are cumbersome in positional arguments')

args = parser.parse_args()

if args.action == "install":
    print("You asked for installation")
else:
    print("You asked for something other than installation")

# The following do not work:
# print(args.foo-bar)
# print(args.foo_bar)

# But this works:
print(getattr(args, 'foo-bar'))

让我困惑的是argparse将命名参数“——foo-bar”转换为“foo_bar”,但是名为“foo-bar”的位置参数仍然是“foo-bar”,这使得在程序中如何使用它变得不那么明显。

注意示例末尾的两行——这两行都不能获取foo-bar位置参数的值。第一个显然是错误的(它是一个算术表达式。Foo - bar),但第二个也不行:

AttributeError: 'Namespace' object has no attribute 'foo_bar'

如果希望使用foo-bar属性,必须使用getattr,如示例的最后一行所示。疯狂的是,如果你试图使用dest=foo_bar将属性名更改为更容易访问的名称,你会得到一个非常奇怪的错误消息:

ValueError: dest supplied twice for positional argument

下面是上面例子的运行方式:

$ python test.py
usage: test.py [-h] action foo-bar
test.py: error: too few arguments

$ python test.py -h
usage: test.py [-h] action foo-bar

An argparse example

positional arguments:
  action      The action to take (e.g. install, remove, etc.)
  foo-bar     Hyphens are cumbersome in positional arguments

optional arguments:
  -h, --help  show this help message and exit

$ python test.py install foo
You asked for installation
foo

argparse文档相当不错,但遗漏了一些可能不明显的有用细节。(@Diego Navarro已经提到了一些,但我会试着稍微扩展一下他的答案。)基本用法如下:

parser = argparse.ArgumentParser()
parser.add_argument('-f', '--my-foo', default='foobar')
parser.add_argument('-b', '--bar-value', default=3.14)
args = parser.parse_args()

从parse_args()返回的对象是一个“Namespace”对象:其成员变量以命令行参数命名的对象。Namespace对象是你访问参数和与它们相关的值的方式:

args = parser.parse_args()
print (args.my_foo)
print (args.bar_value)

(请注意,argparse在命名变量时用下划线替换参数名中的'-'。)

在许多情况下,您可能希望将参数简单地用作不带值的标志。你可以像这样在argparse中添加它们:

parser.add_argument('--foo', action='store_true')
parser.add_argument('--no-foo', action='store_false')

上面的代码将分别创建值为True的变量'foo'和值为False的变量'no_foo':

if (args.foo):
    print ("foo is true")

if (args.no_foo is False):
    print ("nofoo is false")

还要注意,你可以在添加参数时使用"required"选项:

parser.add_argument('-o', '--output', required=True)

这样,如果你在命令行中忽略了这个参数,argparse会告诉你它缺失了,并停止脚本的执行。

最后,请注意,可以使用vars函数创建参数的dict结构,如果这使您的生活更容易的话。

args = parser.parse_args()
argsdict = vars(args)
print (argsdict['my_foo'])
print (argsdict['bar_value'])

如你所见,vars返回一个dict,参数名作为键,值作为值。

还有许多其他选项和可以做的事情,但这应该涵盖最基本的常见使用场景。

代码:

import argparse
parser=argparse.ArgumentParser()
parser.add_argument('-A', default=False, action='store_true')
parser.add_argument('-B', default=False, action='store_true')
args=parser.parse_args()
if args.A:
  print('do this')
elif args.B:
  print('do that')
else:
  print('help')

运行结果:

$ python3 test.py
help

$ python3 test.py -A
do this

$ python3 test.py -B
do that

$ python3 test.py -C
usage: test.py [-h] [-A] [-B]
test.py: error: unrecognized arguments: -C

对于最初的请求(如果A ....),我将使用argv来解决它,而不是使用argparse:

import sys

if len(sys.argv)==2:
  if sys.argv[1] == 'A':
    print('do this')
  elif sys.argv[1] == 'B':
    print('do that')
  else:
    print('help')
else:
  print('help')