如何使用argparser添加标志?

时间:2017-05-04 07:21:56

标签: python argparse flags

我有这样的功能

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

Rightnow我尝试过这段代码

#! /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)

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

2 个答案:

答案 0 :(得分:1)

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

但是如果你想使用flagged / optionals样式的输入而不是subparsers,我建议如下:

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而不是subparser所需的单独的'x','y'参数。

is Noneis not None是测试是否已提供标记值的便捷方法。用户无法输入None,因此测试此默认默认值是万无一失的。

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

答案 1 :(得分:0)

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