具有多个标志和参数的 argparse
argparse with multiple flags and arguments
我希望我的代码能够根据标志调用不同的函数,然后将标志后传递的参数作为函数的输入。
预期输出示例:
$ python3 test.py -a 2
4
$ python3 test.py -a human123
<Error message>
$ python3 test.py -h human123
Hello, human123
这是我的示例代码:
class Test:
def __init__(self):
pass
def add(self, a):
return a+a
def hello(self, name):
return f"Hello, {name}"
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--add', dest='command', action='store_consts', const='add', nargs=1, help='add a to itself')
parser.add_argument('-h', '--hello', dest='command', action='store_consts', const='hello', nargs=1, help='hello!')
args = parser.parse_args()
t = Test()
if args.command=='add':
print(t.add(args.add))
elif args.command=='sub':
print(t.hello(args.hello))
这个示例代码目前正在做我想要实现的事情。我尝试了很多方法来解决这个问题,包括删除 'consts'、将操作更改为 'store'、将 nargs 的值更改为 '?' 等,但是,它总是给我不同类型的错误比如TypeError等
通常我使用add_argument
的dest
参数来指定变量名。
例如:
parser.add_argument("-a", "--add", dest="add", required=False, type=str help="some help")
可以访问:
args = parser.parse_args()
print(args.add == "something")
我相信每个参数需要一个唯一的 dest
。
此外,-h
保留用于帮助。您可能希望将其更改为 -e
或其他内容。
parser.add_argument('-h', '--hello', ...)
简化你的论点:
import argparse
class Test:
def __init__(self):
pass
def add(self, a):
return a+a
def hello(self, name):
return f"Hello, {name}"
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--add', help='add a to itself')
parser.add_argument('-b', '--hello', help='hello!') # -h is already taken
args = parser.parse_args()
print(args)
t = Test()
if args.add: # or 'args.add is not None:'
print(t.add(args.add))
elif args.hello:
print(t.hello(args.hello))
测试运行:
1936:~/mypy$ python3 stack62702524.py -a testing
Namespace(add='testing', hello=None)
testingtesting
1937:~/mypy$ python3 stack62702524.py -b other
Namespace(add=None, hello='other')
Hello, other
1937:~/mypy$ python3 stack62702524.py
Namespace(add=None, hello=None)
===
您没有显示的错误:{ 当您遇到错误时,不要只是举起手来随意尝试替代方案。阅读文档并尝试理解错误。
parser.add_argument('-c', action='store_consts', const='hello')
ValueError: unknown action "store_consts"
parser.add_argument('-c', action='store_const', const='hello', nargs=1)
TypeError: __init__() got an unexpected keyword argument 'nargs'
'store_consts'是名字错误; with 'store_const' nargs
固定为0;你无法改变这一点。
如果我添加 3 个参数 - 两个 store_const 和一个位置参数:
parser.add_argument('-c', dest='command', action='store_const', const='add')
parser.add_argument('-d', dest='command', action='store_const', const='hello')
parser.add_argument('foo')
注意两个新的 command
和 foo
属性,您可以在函数调用中使用它们:
1945:~/mypy$ python3 stack62702524.py -c bar
Namespace(add=None, command='add', foo='bar', hello=None)
1945:~/mypy$ python3 stack62702524.py -d bar
Namespace(add=None, command='hello', foo='bar', hello=None)
我希望我的代码能够根据标志调用不同的函数,然后将标志后传递的参数作为函数的输入。
预期输出示例:
$ python3 test.py -a 2
4
$ python3 test.py -a human123
<Error message>
$ python3 test.py -h human123
Hello, human123
这是我的示例代码:
class Test:
def __init__(self):
pass
def add(self, a):
return a+a
def hello(self, name):
return f"Hello, {name}"
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--add', dest='command', action='store_consts', const='add', nargs=1, help='add a to itself')
parser.add_argument('-h', '--hello', dest='command', action='store_consts', const='hello', nargs=1, help='hello!')
args = parser.parse_args()
t = Test()
if args.command=='add':
print(t.add(args.add))
elif args.command=='sub':
print(t.hello(args.hello))
这个示例代码目前正在做我想要实现的事情。我尝试了很多方法来解决这个问题,包括删除 'consts'、将操作更改为 'store'、将 nargs 的值更改为 '?' 等,但是,它总是给我不同类型的错误比如TypeError等
通常我使用add_argument
的dest
参数来指定变量名。
例如:
parser.add_argument("-a", "--add", dest="add", required=False, type=str help="some help")
可以访问:
args = parser.parse_args()
print(args.add == "something")
我相信每个参数需要一个唯一的 dest
。
此外,-h
保留用于帮助。您可能希望将其更改为 -e
或其他内容。
parser.add_argument('-h', '--hello', ...)
简化你的论点:
import argparse
class Test:
def __init__(self):
pass
def add(self, a):
return a+a
def hello(self, name):
return f"Hello, {name}"
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--add', help='add a to itself')
parser.add_argument('-b', '--hello', help='hello!') # -h is already taken
args = parser.parse_args()
print(args)
t = Test()
if args.add: # or 'args.add is not None:'
print(t.add(args.add))
elif args.hello:
print(t.hello(args.hello))
测试运行:
1936:~/mypy$ python3 stack62702524.py -a testing
Namespace(add='testing', hello=None)
testingtesting
1937:~/mypy$ python3 stack62702524.py -b other
Namespace(add=None, hello='other')
Hello, other
1937:~/mypy$ python3 stack62702524.py
Namespace(add=None, hello=None)
===
您没有显示的错误:{ 当您遇到错误时,不要只是举起手来随意尝试替代方案。阅读文档并尝试理解错误。
parser.add_argument('-c', action='store_consts', const='hello')
ValueError: unknown action "store_consts"
parser.add_argument('-c', action='store_const', const='hello', nargs=1)
TypeError: __init__() got an unexpected keyword argument 'nargs'
'store_consts'是名字错误; with 'store_const' nargs
固定为0;你无法改变这一点。
如果我添加 3 个参数 - 两个 store_const 和一个位置参数:
parser.add_argument('-c', dest='command', action='store_const', const='add')
parser.add_argument('-d', dest='command', action='store_const', const='hello')
parser.add_argument('foo')
注意两个新的 command
和 foo
属性,您可以在函数调用中使用它们:
1945:~/mypy$ python3 stack62702524.py -c bar
Namespace(add=None, command='add', foo='bar', hello=None)
1945:~/mypy$ python3 stack62702524.py -d bar
Namespace(add=None, command='hello', foo='bar', hello=None)