我认为这一定很容易,但我不明白.
假设我有以下arparse解析器:
import argparse
parser = argparse.ArgumentParser( version='pyargparsetest 1.0' )
subparsers = parser.add_subparsers(help='commands')
# all
all_parser = subparsers.add_parser('all', help='process all apps')
# app
app_parser = subparsers.add_parser('app', help='process a single app')
app_parser.add_argument('appname', action='store', help='name of app to process')
Run Code Online (Sandbox Code Playgroud)
我怎样才能确定使用了哪个subparser?电话:
print parser.parse_args(["all"])
Run Code Online (Sandbox Code Playgroud)
给我一个空命名空间:
Namespace()
Run Code Online (Sandbox Code Playgroud) 我有一个需要多个参数的程序,例如
breakfast.py --customer=vikings eggs sausage bacon
Run Code Online (Sandbox Code Playgroud)
其中“鸡蛋”、“香肠”和“培根”可以从特定选项列表中指定。
现在我喜欢这样的输出breakfast.py --help:
usage: breakfast.py [-h] [--customer CUSTOMER] INGREDIENT [INGREDIENT ...]
positional arguments:
your choice of ingredients:
bacon Lovely bacon
egg The runny kind
sausage Just a roll
spam Glorious SPAM
tomato Sliced and diced
optional arguments:
-h, --help show this help message and exit
--customer CUSTOMER salutation for addressing the customer
Run Code Online (Sandbox Code Playgroud)
我尝试了两种方法,但到目前为止都失败了。
使用参数选择:
import argparse
parser = argparse.ArgumentParser()
toppings = {
'bacon': "Lovely bacon",
'egg': 'The runny kind',
'sausage': 'Just a …Run Code Online (Sandbox Code Playgroud)