argparse

Changing the metavar value in argparse only in argument listing and not in its Usage

流过昼夜 提交于 2019-12-01 11:12:09
问题 My question is similar to argparse help without duplicate ALLCAPS question. Though i would explain in brief what that question was and what my question is: I'd like to display argparse help for my options the same way the default -h,--help is, without the ALLCAPS text after each option, or at least without the duplicated CAPS. For example, with the following code: #filename=temp.py import argparse p = argparse.ArgumentParser() p.add_argument('-i', '--ini', help="use alternate ini file") print

Python argparse with nargs behaviour incorrect

六月ゝ 毕业季﹏ 提交于 2019-12-01 11:00:49
Here is my argparse sample say sample.py import argparse parser = argparse.ArgumentParser() parser.add_argument("-p", nargs="+", help="Stuff") args = parser.parse_args() print args Python - 2.7.3 I expect that the user supplies a list of arguments separated by spaces after the -p option. For example, if you run $ sample.py -p x y Namespace(p=['x', 'y']) But my problem is that when you run $ sample.py -p x -p y Namespace(p=['y']) Which is neither here nor there. I would like one of the following Throw an exception to the user asking him to not use -p twice instead just supply them as one

Python argparse with nargs behaviour incorrect

萝らか妹 提交于 2019-12-01 08:50:40
问题 Here is my argparse sample say sample.py import argparse parser = argparse.ArgumentParser() parser.add_argument("-p", nargs="+", help="Stuff") args = parser.parse_args() print args Python - 2.7.3 I expect that the user supplies a list of arguments separated by spaces after the -p option. For example, if you run $ sample.py -p x y Namespace(p=['x', 'y']) But my problem is that when you run $ sample.py -p x -p y Namespace(p=['y']) Which is neither here nor there. I would like one of the

argparse optional positional argument and subparsers arguments

非 Y 不嫁゛ 提交于 2019-12-01 08:11:19
问题 I have a python script that takes in an optional positional argument and has a few subcommands. Some of these subcommands require the positional argument, some don't. The problem I have appears when I try to use a subcommand that does not require the positional argument. Consider the following test file: import argparse argp = argparse.ArgumentParser() argp.add_argument('inputfile', type=str, nargs='?', help='input file to process') argp.add_argument('--main_opt1', type=str, help='global

Python; argparse; how to specify position of positional arguments

烈酒焚心 提交于 2019-12-01 07:31:01
How do I specify the position of a positional argument? In my example below, the SCR & SCV arguments appear at the end of the optional arguments, I want them to appear at the beginning. #!/usr/bin/python import argparse ### Parse arguments ### parser = argparse.ArgumentParser() parser.add_argument("SCR",type=int) parser.add_argument("SCV",type=int) parser.add_argument("--itemid",nargs='?') parser.add_argument("--tkt",nargs='?') parser.add_argument("--rfc",nargs='?') parser.add_argument("--state",nargs='?') parser.add_argument("--vendor",nargs='?') parser.add_argument("--application",nargs='?')

Python 2.x optionnal subparsers - Error too few arguments

心已入冬 提交于 2019-12-01 07:22:43
问题 I have been trying to set up a main parser with two subs parser so that when called alone, the main parser would display a help message. def help_message(): print "help message" import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='sp') parser_a = subparsers.add_parser('a') parser_a.required = False #some options... parser_b = subparsers.add_parser('b') parser_b.required = False #some options.... args = parser.parse_args([]) if args.sp is None: help

How to test Python classes that depend on argparse?

爱⌒轻易说出口 提交于 2019-12-01 07:07:53
The below paste contains relevant snippets from three separate Python files. The first is a script called from the command line which instantiates CIPuller given certain arguments. What happens is that the script gets called with something like: script.py ci (other args to be swallowed by argparse). The second is part of a subclass called Puller . The third is part of a subclass of Puller called CIPuller . This works wonderfully, as the correct subclass is called, and any user using the wrong other args gets to see the correct args for their given subclass, plus the generic arguments from the

ArgumentParser: Optional argument with optional value

血红的双手。 提交于 2019-12-01 05:22:16
If I have an optional argument with optional argument value, is there a way to validate if the argument is set when the value is not given? For instance: parser = argparse.ArgumentParser() parser.add_argument('--abc', nargs='?') args = parser.parse_args() Would correctly give me: optional arguments: --abc [ABC] How do I distinguish between 1 and 2 below? '' => args.abc is None '--abc' => args.abc is still None '--abc something' => args.abc is something ... Update: Found a trick to solve this problem: you can use "nargs='*'" instead of "nargs='?'". This way #1 would return None, and #2 would

Python argparse argument with quotes

我与影子孤独终老i 提交于 2019-12-01 04:16:47
Is there any way I can tell argparse to not eat quotation marks? For example, When I give an argument with quotes, argparse only takes what's inside of the quotes as the argument. I want to capture the quotation marks as well (without having to escape them on the command line.) pbsnodes -x | xmlparse -t "interactive-00" produces interactive-00 I want "interactive-00" wim I think it is the shell that eats them, so python will actually never see them. Escaping them on the command line may be your only option. If it's the \"backslash\" style escaping you don't like for some reason, then this way

How to have sub-parser arguments in separate namespace with argparse?

…衆ロ難τιáo~ 提交于 2019-12-01 03:44:19
I have the following test-code import argparse parser = argparse.ArgumentParser() parser.add_argument("--verbose", default = 0, type=int) subparsers = parser.add_subparsers(dest = "parser_name") parser_lan = subparsers.add_parser('car') parser_lan.add_argument("--boo") parser_lan.add_argument("--foo") parser_serial = subparsers.add_parser('bus') parser_serial.add_argument("--fun") print parser.parse_args() which defines two sub-parsers, having a different set of arguments. When I call the testcode as tester.py --verbose 3 car --boo 1 --foo 2 I get the expected result Namespace(boo='1', foo='2'