argparse

python argparse mutually_exclusive_group and add_argument_group in a parent processor?

a 夏天 提交于 2020-01-01 16:45:31
问题 I'm not sure if I've found an argparse bug, or if I'm being obtuse. I researched #45602511 which is how I found it "works" in the top-level case. I'm trying to use an argument_group to separate out some formatting options (which happen to be mutually exclusive). If things are at the top level of the parser structure, I can nest a mutually_exclusive_group under an argument_group and I get the help behavior I'm expecting: The following code works fine in Python 3.6.2: import argparse parser =

Optional arguments across all subparsers

断了今生、忘了曾经 提交于 2020-01-01 14:34:09
问题 I'm currently testing an argparse usage, but it's not working as expected. I have a couple of subparsers and optional arguments, being called the following way: python3 myprogram.py positional argument --optional something # Outcome Namespace(optional='something') The program works as expected if the optional is the last, but if it is in any other order, it is discarded . python3 myprogram.py positional --optional argument python3 myprogram.py --optional positional argument # Outcome

argparse 常用操作

时光总嘲笑我的痴心妄想 提交于 2020-01-01 13:54:48
目录 一. 实例 二. 参数解析 一. 实例 # -*- coding: UTF-8 -*- import argparse def main(): parser = argparse.ArgumentParser(description="This is a example program") # 字符串类型 parser.add_argument('-f', '--file', action='store', help='just a test', dest='fore_name') # 不带参数 parser.add_argument('-v', '--verbose', action='store_false', default = True) # 带多个参数 parser.add_argument('-m', '--mag', nargs='+') # 创建两个互斥的参数,如果同时出现,则会报异常。 group = parser.add_mutually_exclusive_group() group.add_argument("-a", "--aa", action="store_true") group.add_argument("-b", "--bb", action="store_true") # 限定某个参数范围 parser.add_argument('-c'

Cause Python's argparse to execute action for default

笑着哭i 提交于 2020-01-01 08:01:12
问题 I am using argparse's action to add various data to a class. I would like to use that action on the default value if that arg is not provided at the command line. Is this possible? Thanks! 回答1: argparse does not use the action when applying the default . It just uses setattr . It may use the type if the default is a string. But you can invoke the action directly. Here I use a custom action class borrowed from the documentation. In the first parse_args nothing happens. Then I create a new

ImportError: No module named argparse

放肆的年华 提交于 2020-01-01 07:29:10
问题 I am trying to run a Python program but get the error ImportError: No module named argparse I found the question “argparse Python modules in cli” here on StackOverflow and tried the first comment, i.e. running the command python -c "import argparse; print argparse" which resulted in <module 'argparse' from '/usr/lib/python2.7/argparse.pyc'> For me it seems like there is Python 2.7 installed on the machine (of which I am not administrator) and the argparse module is present as well. So I

argparse mutually exclusive group title and description in help message

ぐ巨炮叔叔 提交于 2020-01-01 05:50:12
问题 Why I can't I have an argparse mutually exclusive group with a title or description , so that it appears as a separate category under the --help message? I have an options group with a name and a description: import argparse parser = argparse.ArgumentParser() group = parser.add_argument_group( 'foo options', 'various (mutually exclusive) ways to do foo') group.add_argument('--option_a', action='store_true', help='option a') group.add_argument('--option_b', action='store_true', help='option b'

Argparse“ArgumentError: argument -h/--help: conflicting option string(s): -h, --help”

久未见 提交于 2020-01-01 01:10:35
问题 Recently, I am learning argparse module, Argument error occurred below the code import argparse import sys class ExecuteShell(object): def create(self, args): """aaaaaaa""" print('aaaaaaa') return args def list(self, args): """ccccccc""" print('ccccccc') return args def delete(self, args): """ddddddd""" print('ddddddd') return args class TestShell(object): def get_base_parser(self): parser = argparse.ArgumentParser() parser.add_argument('-h', '--help', action='store_true', help=argparse

python argparse store --foo=bar as args.key='foo', args.value='bar'

让人想犯罪 __ 提交于 2019-12-31 04:40:05
问题 I'd like to parse a command line that has a mutually exclusive group of options. Normally, I'd just use --foo bar which would produce, in the namespace, args.foo = 'bar' However, since all of these options are mutually exclusive, and I'm interested in both the option name and the argument passed to the option, and I have several of these options that need to be fed downstream, what I'd really like is to get back args.option_name = 'foo', args.option_value = 'bar' in my namespace instead of

Optional positional arguments with Python's argparse

ε祈祈猫儿з 提交于 2019-12-31 01:39:06
问题 Trying to parse optional positional arguments I ran into following issue: Example: import argparse parser = argparse.ArgumentParser() parser.add_argument('infile') parser.add_argument('outfile', nargs='?') parser.add_argument('-v', action='store_true') print(parser.parse_args()) Output: $ ./x.py -v in out Namespace(infile='in', outfile='out', v=True) $ ./x.py in out -v Namespace(infile='in', outfile='out', v=True) $ ./x.py in -v out usage: x.py [-h] [-v] infile [outfile] x.py: error:

How to obtain argparse subparsers from a parent parser (to inspect defaults)

不打扰是莪最后的温柔 提交于 2019-12-30 14:15:11
问题 Suppose that I create a parser with a default value for an argument, and then give it a subparser with a further default value for an argument. In [1]: parser = argparse.ArgumentParser(description='test') In [2]: parser.add_argument("--test", dest="test", default="hello") Out[2]: _StoreAction(option_strings=['--test'], dest='test', nargs=None, const=None, default='hello', type=None, choices=None, help=None, metavar=None) In [3]: parser.get_default("test") Out[3]: 'hello' In [4]: subparsers =