argparse: identify which subparser was used [duplicate]

馋奶兔 提交于 2019-11-26 23:55:17

问题


I think this must be easy but I do not get it.

Assume I have the following arparse parser:

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')

How can I identify, which subparser was used? calling:

print parser.parse_args(["all"])

gives me an empty namespace:

Namespace()

回答1:


Edit: Please see quornian's answer to this question, which is better than mine and should be the accepted answer.

According to the argparse documentation the result of parser.parse_args(...) will "only contain attributes for the main parser and the sub parser that was selected". Unfortunately this may not be enough information to determine which sub parser was used. The documentation recommends using the set_defaults(...) method on the sub parser to solve this problem.

For example, I've added calls to set_defaults() to your code:

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')
all_parser.set_defaults(which='all')

# 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')
app_parser.set_defaults(which='app')

Now if you run

print parser.parse_args(["all"])

The result is

Namespace(which='all')

Check out the add_subparsers() documentation for more information and another example.




回答2:


A simpler solution is to add dest to the add_subparsers call. This is buried a bit further down in the documentation:

[...] If it is necessary to check the name of the subparser that was invoked, the dest keyword argument to the add_subparsers() call will work

In your example replace:

subparsers = parser.add_subparsers(help='commands')

with:

subparsers = parser.add_subparsers(help='commands', dest='command')

Now if you run:

print parser.parse_args(["all"])

you will get

Namespace(command='all')


来源:https://stackoverflow.com/questions/8250010/argparse-identify-which-subparser-was-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!