如何使用 argparse 添加标志?

How to add flags using argparser?

我有这样的功能

def add(x,y):
    print x+y

def square(a):
    print a**2

我可以像

一样使用 argparse 为这些函数制作标志吗
./hello.py -a add 2 3
./hello.py -s sqare 3

现在我试过这个代码

#! /usr/bin/python


import argparse
# Create Parser and Subparser
parser = argparse.ArgumentParser(description="Example ArgumentParser")
subparser = parser.add_subparsers(help="commands")

# Make Subparsers
hello_parser = subparser.add_parser('hello', help='hello func')
hello_parser.add_argument("arg",help="string to print")
hello_parser.set_defaults(func='hello')

add_parser = subparser.add_parser('add', help="add func")
add_parser.add_argument("x",type=float,help='first number')
add_parser.add_argument("y",type=float,help='second number')
add_parser.set_defaults(func='add')

square_parser = subparser.add_parser('square', help="square func")
square_parser.add_argument("a",type=float,help='number to square')
square_parser.set_defaults(func='square')

args = parser.parse_args()

def hello(arg):
  print arg

def add(x,y):
  print x + y

def square(a):
  print a**2

if args.func == 'hello':
  hello(args.arg)
elif args.func == 'add':
  add(args.x,args.y)
elif args.func == 'square':
  square(args.a)

我可以在同一代码中添加标志吗??

import argparse

def add(x,y):
    print x+y

def square(a):
    print a**2

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='This is a parser.')
    parser.add_argument('command', help='The command to execute (add or square).')
    parser.add_argument('integers', metavar='N', type=int, nargs='+', help='The arguments to the command.')
    args = parser.parse_args()

    if args.command == 'add':
        add(*args.integers)
    elif args.command == 'square':
        square(*args.integers)
    else:
        print 'Command not supported.'

输出:

$ python hello.py add 2 3
5
$ python hello.py square 2
4

https://docs.python.org/3/library/argparse.html#sub-commands 的末尾有一个像你的例子,但是有像 add_parser.set_defaults(func=add) 这样的表达式(使用实际函数而不是名称)。这让他们可以用 args.func(args).

替换 if/else 堆栈

但是如果您想使用 flagged/optionals 风格的输入而不是子解析器,我建议如下:

import argparse

def add(x,y):
    print x+y

def square(a):
    print a**2

parser = argparse.ArgumentParser()
parser.add_argument('--hello')
parser.add_argument('-a', '--add', nargs=2, type=int)
parser.add_argument('-s', '--square', type=int)

args = parser.parse_args()
print(args)           # good debugging tool

if args.add is not None:
   add(*args.add)   # * expands the list into the 2 arguments 
if args.square is not None:
   square(args.square)
if args.hello is not None:
   print ("hello "+args.hello)

产生

1014:~/mypy$ python stack43776406.py -a 10 11
Namespace(add=[10, 11], hello=None, square=None)
21
1014:~/mypy$ python stack43776406.py --hello world -a 1 2 -s 1000
Namespace(add=[1, 2], hello='world', square=1000)
3
1000000
hello world

如果“-a”标志已经表示 add,则不需要包含额外的字符串。我还使用了 nargs=2 而不是子解析器所需的单独的 'x'、'y' 参数。

is Noneis not None 是测试是否已提供标记值的简便方法。用户不能输入None,所以测试这个默认的default是万无一失的。

1014:~/mypy$ python stack43776406.py --help
usage: stack43776406.py [-h] [--hello HELLO] [-a ADD ADD] [-s SQUARE]

optional arguments:
  -h, --help            show this help message and exit
  --hello HELLO
  -a ADD ADD, --add ADD ADD
  -s SQUARE, --square SQUARE