argparse

how to add_argument_group to add_mutually_exclusive_group with python argparse

爷,独闯天下 提交于 2019-12-20 20:12:28
问题 I am trying to implement the following: $ prog.py -h usage: prog.py [-h] [-s | -m] [[-y [year]] | [[-1 | -3] [month] [year]]] However, no matter how I played with add_argument_group and add_mutually_exclusive_group, #!/usr/bin/env python import argparse def main(opt): print(opt) if __name__ == '__main__': parser = argparse.ArgumentParser() bar = parser.add_mutually_exclusive_group() bar.add_argument('-s', action='store_true', default=True) bar.add_argument('-m', action='store_true', default

How to code argparse combinational options in python

旧巷老猫 提交于 2019-12-20 18:20:14
问题 I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result. Requirement: test2.py [-c/-v] -f Usage or Rules: -c (compare) takes 2 parameter. -v (verify) takes 1 parameter. Either of these two must be present, but not both . -f is a mandatory parameter (output file name). Output: I am able to get the desired output as shown below kp@kp:~/Study/scripts$ ./test.py -c P1 P2 -f p kp@kp:~/Study/scripts$ ./test.py -v P1 -f

How to code argparse combinational options in python

此生再无相见时 提交于 2019-12-20 18:19:30
问题 I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result. Requirement: test2.py [-c/-v] -f Usage or Rules: -c (compare) takes 2 parameter. -v (verify) takes 1 parameter. Either of these two must be present, but not both . -f is a mandatory parameter (output file name). Output: I am able to get the desired output as shown below kp@kp:~/Study/scripts$ ./test.py -c P1 P2 -f p kp@kp:~/Study/scripts$ ./test.py -v P1 -f

Multiple positional arguments with Python and argparse

女生的网名这么多〃 提交于 2019-12-20 10:34:23
问题 I'm trying to use argparse to parse the command line arguments for a program I'm working on. Essentially, I need to support multiple positional arguments spread within the optional arguments, but cannot get argparse to work in this situation. In the actual program, I'm using a custom action (I need to store a snapshot of the namespace each time a positional argument is found), but the problem I'm having can be replicated with the append action: >>> import argparse >>> parser = argparse

Python Argparse: Issue with optional arguments which are negative numbers

安稳与你 提交于 2019-12-20 10:19:31
问题 I'm having a small issue with argparse . I have an option xlim which is the xrange of a plot. I want to be able to pass numbers like -2e-5 . However this does not work - argparse interprets this is a positional argument. If I do -0.00002 it works: argparse reads it as a negative number. Is it possible to have able to read in -2e-3 ? The code is below, and an example of how I would run it is: ./blaa.py --xlim -2.e-3 1e4 If I do the following it works: ./blaa.py --xlim -0.002 1e4 The code:

Getting the remaining arguments in argparse

…衆ロ難τιáo~ 提交于 2019-12-20 08:56:16
问题 I want to get all the remaining unused arguments at once. How do I do it? parser.add_argument('-i', action='store', dest='i', default='i.log') parser.add_argument('-o', action='store', dest='o', default='o.log') 回答1: Another option is to add a positional argument to your parser. Specify the option without leading dashes, and argparse will look for them when no other option is recognized. This has the added benefit of improving the help text for the command: >>> parser.add_argument(

ArgumentParser parsing optional arguments, not defined in Parser [duplicate]

这一生的挚爱 提交于 2019-12-20 07:32:56
问题 This question already has answers here : Python argparse ignore unrecognised arguments (3 answers) Create variable key/value pairs with argparse (python) (4 answers) Can argparse accept argument value as key=val pairs (2 answers) Parse non-pre-defined argument (3 answers) Closed 12 months ago . I have the following lines to parse command line arguments: ... parser = argparse.ArgumentParser(description="Arguments for Creation of delivery report") parser.add_argument('tag', help="GIT Tag of the

Subparser — show help for both parser and subparser

妖精的绣舞 提交于 2019-12-20 04:19:56
问题 I've looked through dozens of similar SO questions but haven't find suitable solutions so please forgive me in case of a dublicate. I have a problem similar to this one I want to have a parser+subparser pair, with --help option causing help being shown for the both if subparser is "activated". The only way I was able to get full (parser + subparser) help is: common_parser = argparse.ArgumentParser(add_help=False) common_parser.add_argument('-c', required = True) parser = argparse

Parse non-pre-defined argument

淺唱寂寞╮ 提交于 2019-12-20 04:08:01
问题 Is there any library that can parse random key value pairs in sys.argv in Python? For example: python run.py --v1 k1 --v2 k2 --v3 k3 Should return me a dictionary like {v1->k1, v2->k2, v3->k3}. and at compile time I don't know what those 'v' will be. Thanks! Erben 回答1: It's kind of hacky, but you do have this: import argparse import collections parser = argparse.ArgumentParser() known, unknown_args = parser.parse_known_args() unknown_options = collections.defaultdict(list) key = None for arg

argparse conflict resolver for options in subcommands turns keyword argument into positional argument

送分小仙女□ 提交于 2019-12-19 19:01:25
问题 I have a Python script that runs two sub-commands who accept the same option, --config . I would like to create a third sub-command that can run the first two subcommands together, sequentially. Using argparse, I've created a subparser for each sub-command, as well as a third subparser, whose parents are the two sub-commands. Just to clarify: subcommand1 = subparsers.add_parser('subcommand1') subcommand1.add_argument('--config', help="The config") subcommand2 = subparsers.add_parser(