argparse

argparse: let the same required argument be positional OR optional

不想你离开。 提交于 2020-01-05 06:44:26
问题 I would like to have a required command line argument passed as a positional argument or an optional argument. For example, I would like the same action be performed from any of the following invocations: prog 10 prog -10 prog -n 10 prog --num 10 Is this possible with argparse? 回答1: With a mutually exclusive group I can create a reasonable approximation: In an interactive session: In [10]: parser=argparse.ArgumentParser() In [11]: grp=parser.add_mutually_exclusive_group(required=True) In [12]

Python argparse Optional argument only works when it's entered in the right positions

丶灬走出姿态 提交于 2020-01-05 03:58:06
问题 In my script I have 3 positional arguments and 1 optional argument. One of the three positional arguments is required and the rest is optional (as specified using narg='?' ). My optional argument doesn't pass any other arguments (action ='store_true') and is just there to enable sorting which will be implemented at a later time. However, my problem is that my optional argument only works when it is the first or last argument in the script call. Below is my script so far: parser = argparse

Python argparse conditional arguments

不打扰是莪最后的温柔 提交于 2020-01-05 03:34:25
问题 I am creating a program that requires conditional arguments using argparse. I would like to generate new arguments in my code depending on if a previous argument has been entered or not. Here is a basic example of how I would like my code to look import argparse parser = argparse.ArgumentParser() parser.add_argument("-bowtie",action = "store_true",help="use to run bowtie") args = parser.parse_args() if args.bowtie: parser.add_argument( add some new argument here ) args = parser.parse_args()

Python argparse: how to handle MacOSX `-psn` parameter

喜夏-厌秋 提交于 2020-01-04 12:17:39
问题 MacOSX launchd passes the -psn... parameter to applications. How can I tell argparse how to parse the parameter? It's basically -psn_([0-9]+)_([0-9]+) but I'm fine if it just parses -psn(\S*) . Is that possible at all? (See here for some documentation about the Process Serial Number (PSN). And here is my question about what to do with the parameter.) 回答1: Looking at the structure of the psn argument, it's not possible to parse it with argparse without interfering with a possible "-p" argument

Parsing “python foo.py -DVAR1=9 -DVAR2=Off” with argparse

微笑、不失礼 提交于 2020-01-04 05:49:12
问题 Looking at this answer, I could do this: parser=argparse.ArgumentParser() parser.add_argument('-D',action='append',help='FOO=BAR') options = parser.parse_args('-DVAR1=9 -DVAR2=Off'.split()) And I get: Namespace(D=['VAR1=9', 'VAR2=Off']) So then saying: [o.split('=') for o in options.D] Results in: [['VAR1', '9'], ['VAR2', 'Off']] This is basically what I need, but I feel this is a common action that might already have an implementation within the ArgParse package. Is there a more Pythonesque

argparse: don't show usage on -h

浪子不回头ぞ 提交于 2020-01-04 03:15:11
问题 The code from argparse import ArgumentParser p = ArgumentParser(description = 'foo') p.add_argument('-b', '--bar', help = 'a description') p.parse_args() ...results in the output: $ python argparsetest.py -h usage: argparsetest.py [-h] [-b BAR] foo optional arguments: -h, --help show this help message and exit -b BAR, --bar BAR a description What I'd like is: $ python argparsetest.py -h foo optional arguments: -h, --help show this help message and exit -b BAR, --bar BAR a description e.g., no

Extract values from parse.add_argument in Argparse - python

六眼飞鱼酱① 提交于 2020-01-03 05:35:18
问题 I am using Argparse as a means to command line utility execution. I have various arguments defined ( couple of them shown below ). I have a requirement to store argument name, help, type in the database in their respective columns. I am not sure how to extract these three from each parse.add_argument and save it in some array/list. If you can share any inputs, that would be helpful. parser.add_argument("num",help="The fibnocacci number to calculate:", type=int) # how to take the strings on

Parse multiple subcommands in python simultaneously or other way to group parsed arguments

柔情痞子 提交于 2020-01-02 19:51:14
问题 I am converting Bash shell installer utility to Python 2.7 and need to implement complex CLI so I am able to parse tens of parameters (potentially up to ~150). These are names of Puppet class variables in addition to a dozen of generic deployment options, which where available in shell version. However after I have started to add more variables I faced are several challenges: 1. I need to group parameters into separate dictionaries so deployment options are separated from Puppet variables. If

Parse multiple subcommands in python simultaneously or other way to group parsed arguments

风流意气都作罢 提交于 2020-01-02 19:51:11
问题 I am converting Bash shell installer utility to Python 2.7 and need to implement complex CLI so I am able to parse tens of parameters (potentially up to ~150). These are names of Puppet class variables in addition to a dozen of generic deployment options, which where available in shell version. However after I have started to add more variables I faced are several challenges: 1. I need to group parameters into separate dictionaries so deployment options are separated from Puppet variables. If

argparse option of options

安稳与你 提交于 2020-01-01 17:08:12
问题 I am trying to add option of options in argparse . Currently I have: group = parser.add_mutually_exclusive_group() group.add_argument("--md", help="Create xyz file for each ionic step for" " visualization", action='store_true') group.add_argument("--force", help="See which atom has maximum force", action='store_true') group.add_argument("--opt", help="grep string from file", nargs=2, metavar=("str", "file")) parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)" "