argparse

Configure argparse to accept quoted arguments

巧了我就是萌 提交于 2019-12-11 08:42:24
问题 I am writing a program which, among other things, allows the user to specify through an argument a module to load (and then use to perform actions). I am trying to set up a way to easily pass arguments through to this inner module, and I was attempting to use ArgParse's action='append' to have it build a list of arguments that I would then pass through. Here is a basic layout of the arguments that I am using parser.add_argument('-M', '--module', help="Module to run on changed files - should

Handling indefinite paired arguments with argparse

北慕城南 提交于 2019-12-11 08:15:34
问题 In my project, I need to define a syntax like mcraw recipe add COUNT ID COUNT_1 ID_1 [COUNT_2 ID_2 ..] and argparse seems to be the best tool for the general job. How can I instruct Python and its argparse to construct a dictionary like this? { ID_1: COUNT_1, ID_2: COUNT_2, ... } 回答1: Read your arguments in pairs: argdict = {args[i + 1]: args[i] for i in xrange(0, len(args), 2)} argparse has otherwise no special handling for this kind of input. 回答2: I think you may have the wrong approach

Argparse: parse multiple subcommands

守給你的承諾、 提交于 2019-12-11 05:05:56
问题 Did some research, but couldn't find any working solution. I'm trying to parse the following command line, where 'test' and 'train' are two independent subcommands each having distinct arguments: ./foo.py train -a 1 -b 2 ./foo.py test -a 3 -c 4 ./foo.py train -a 1 -b 2 test -a 3 -c 4 I've been trying using two subparsers ('test','train') but it seems like only one can be parsed at the time. Also it would be great to have those subparsers parents of the main parser such that, e.g. command '-a'

argparse.REMAINDER changes the behavior of positional arguments

笑着哭i 提交于 2019-12-11 04:37:42
问题 Without argparse.REMAINDER , optional arguments can be in front of or after positional arguments: import argparse parser = argparse.ArgumentParser() parser.add_argument('-a') parser.add_argument('b') print(parser.parse_args('-a 1 2'.split())) # Namespace(a='1', b='2') print(parser.parse_args('2 -a 1'.split())) # Namespace(a='1', b='2') But with argparse.REMAINDER , optional arguments must be in front: parser.add_argument('c', nargs=argparse.REMAINDER) print(parser.parse_args('-a 1 2 3'.split(

Python multiple user arguments to a list

。_饼干妹妹 提交于 2019-12-11 04:31:59
问题 I've got not words to thank you all of you for such great advice. Now everything started to make sense. I apologize for for my bad variable naming. It was just because I wanted to quickly learn and I wont carry out such practices when I write the final script with my own enhancements which will be posted here. I want to go an another step further by passing the values we've isolated (ip,port,and name) to a template. I tried but couldn't get it right even though I feel close. The text I want

Python argparse store_true and store optional option in one argument [duplicate]

本秂侑毒 提交于 2019-12-11 04:18:07
问题 This question already has answers here : Option accepted with and without value (2 answers) Closed 6 years ago . I need to recognise if was given argument alone or with optional string or neither parser.add_argument(???) options = parser.parse_args() so ./prog.py --arg should store '' into options.arg, ./prog.py --arg=lol stores 'lol' into options.arg and ./prog.py left options.arg as None now I have: parser.add_argument("--arg", nargs="?",type=str,dest="arg") but when I run myprogram as .

Python argparse, run one or more sub-commands

前提是你 提交于 2019-12-11 03:39:09
问题 I'm trying to write a program that is able to execute multiple sub-commands. The argparse module is very helpful, but I think it is lacking the ability to specify more than one sub-command. For example, if I have the following code: parser = argparse.ArgumentParser(prog='My Prog') sub_parsers = parser.add_subparsers() subcommand_a = sub_parsers.add_parser('subcommand_a', help='a help') subcommand_a.add_argument('req1', help='required argument 1 help') subcommand_a.add_argument('--opt1', help=

Using argparse arguments as keyword arguments

送分小仙女□ 提交于 2019-12-11 02:54:03
问题 Let's say I have an args namespace after parsing my command line with argparse. Now, I want to use this to create some objects like this: foo = Foo(bar=args.bar) Unfortunately, I have the restriction that if a keyword argument is set, it must not be None . Now, I need to check if args.bar is set and act accordingly: if args.bar: foo = Foo(bar=args.bar) else: foo = Foo() This is unwieldy and doesn't scale for more arguments. What I'd like to have, is something like this: foo = Foo(**args._

How to create an argument that is optional?

青春壹個敷衍的年華 提交于 2019-12-11 02:47:46
问题 Instead of the user having to use script.py --file c:/stuff/file.txt is there a way to let the user optionally use the --file ? So instead, it would look like script.py c:/stuff/file.txt but the parser would still know that the user is referring to the --file argument (because it's implied). 回答1: Try this import argparse class DoNotReplaceAction(argparse.Action): def __call__(self, parser, namespace, values, option_string=None): if not getattr(namespace, self.dest): setattr(namespace, self

python argparse add_argument_group required

我们两清 提交于 2019-12-11 02:24:34
问题 In this question argparse: require either of two arguments I find a reference to the solution I want, but it isn't right. I need at least 1 of 2 options to be present, option1, option2 or both... The add_argument_group function doesn't have a required argument. The add_mutually_exclusive function has it, but it forces me to choose between the 2 options, which is not what I want. rds, 回答1: argument_group just controls the help display. It does not affect the parsing or check for errors.