2017-05-04 58 views
0

我有这样如何使用argparser添加标志?

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

def square(a): 
    print a**2 

我可以使用​​这些功能的标志一样

./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) 

我可以在添加标记功能相同的代码?

+0

你读过[DOC](https://docs.python.org/2/howto/argparse.html)? – Anzel

+0

我已阅读,但我无法实时应用。你能用一个例子来解释我吗?谢谢。 – sowji

+0

使用['click'](http://click.pocoo.org/6/api/#click.confirmation_option),人 –

回答

1

https://docs.python.org/3/library/argparse.html#sub-commands端部具有像您的例子,但与像add_parser.set_defaults(func=add)表达式(使用的实际功能,而不是名称)。这让他们用args.func(args)代替if/else堆栈。

但是,如果你想使用的输入,而不是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而不是分区器所需的单独'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 
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 
相关问题