argparse

Support global arguments before or after sub-command in argparse

Deadly 提交于 2021-02-05 09:37:09
问题 Setting the parents argument with a parser will allow for sharing common arguments between parsers (e.g. parents and sub-commands). But, applying a base parser to both the parent and sub-command appears to overwrite the value from the parent parser with the value from the sub-command parser when using an argument that has specified the value attribute to with a dest keyword, whether or not the invocation has specified the argument in the sub-command. How can I use argparse module to merge the

Python argparse with subparsers and optional positional arguments

浪子不回头ぞ 提交于 2021-02-05 08:36:06
问题 I would like to have a program with subparsers that handles specific arguments while also keep some positional and optional arguments to the previous parsers (In fact what I really want is only one option, I mean, a valid subparser OR a valid local argument). Example of something I wish to have: Program [{sectionName [{a,b}]}] [{c,d}] . Being c/d incompatible if sectionName was provided and viceversa. However, the best I could achieve is this test.py [-h] {sectionName} ... [{c,d}] . This

access “implicit” metavar value in argument help string

ぐ巨炮叔叔 提交于 2021-02-05 06:46:05
问题 When you call add_argument on an argparse.ArgumentParser() without an explicit action, you get the "store" action. In the auto-generated --help output you get the uppercase of the long option, unless you set metavar : import argparse parser = argparse.ArgumentParser() parser.add_argument('--version', metavar='X.Y.Z') parser.add_argument('--date'), parser.parse_args(['--help']) displays: usage: try.py [-h] [--version X.Y.Z] [--date DATE] optional arguments: -h, --help show this help message

Python argparse: default argument stored as string, not list

若如初见. 提交于 2021-02-04 14:32:11
问题 I cannot figure out this behaviour of argparse from the documentation: import argparse parser.add_argument("--host", metavar="", dest="host", nargs=1, default="localhost", help="Name of host for database. Default is 'localhost'.") args = parser.parse_args() print(args) Here is the output with and without an argument for "--host": >> python demo.py Namespace(host='localhost') >> python demo.py --host host Namespace(host=['host']) In particular: why does the argument to "--host" get stored in a

Python argparse creates incorrect usage string when using nested sub-parsers

你说的曾经没有我的故事 提交于 2021-01-29 14:57:11
问题 I would like to build a (complicated) command-line argument parser using argparse module facilities. The 'main' script can accept sub-commands, and some of the sub-commands also have their own sub-commands. Here is a MWE: #!/usr/bin/env python3 import argparse def arg_parser(): parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(required=True, dest="cmd") p = subparsers.add_parser("c1", help="Command 1") p.add_argument("foo", help="foo argument") p.add_argument("bar", help=

Issue with automatic usage string from argparse with nargs='*' and metavar with optional plural “(s)”

雨燕双飞 提交于 2021-01-29 14:02:23
问题 I'm writing a program that uses a positional argument that takes in "the rest" of the arguments, like this import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( 'filenames', nargs='*', metavar="filename(s)", ) args = parser.parse_args() Here I have used the metavar parameter to get a nicer help text. But the issue is the usage string: >python test_argparse_plural.py -h usage: test_argparse_plural.py [-h] [filenames) [filename(s ...]] positional

Argparse: nested optional argument, force --arg=val over --arg val, single occurrence, optional argument order

本秂侑毒 提交于 2021-01-29 09:21:58
问题 The program should be executed as follows: ./interpret.py --help | --source=FILE1 [--stats=FILE2 [--vars] [--insts]] the rules are: --help has to be the only argument if passed --source=FILE1 has to be provided and can't be passed like --source FILE1 if --vars or --insts is provided, then --stats=FILE2 has to be provided the order of --vars and --insts matters, therefore has to be stored multiple occurrence of an argument is forbidden I have read multiple tutorials and SO answers, but they

Run python script with argparse in another script

匆匆过客 提交于 2021-01-29 06:23:00
问题 I have a python script which uses some input parameters via argparse. something like this: **hello.py** import argparse parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", required=True, help="name of the user") arguments = parser.parse_args() print(f'Hello, {arguments.name}') I wonder if this could be ran out in another .py script. **another.py** names = ['Lu', 'Li', 'La'] for name in names: import hello.py -n name #this is how to run script from console Is there any

How can you run a python script as a batch job after passing an argument using argparse?

允我心安 提交于 2021-01-28 07:41:47
问题 I was wondering whether it is possible to run a python script as a bash job after using argparse? I tried doing this by first passing an argument in the python script file using argparse and then I used the command: bash bash1.sh to run the bash file that will run the python script file. This resulted in an error message_script.py: error: too few arguments This error resulted from the fact that the argparse argument wasn't recognised. Is there any way I can get to pass the argument using

Python argparse requiring option, depending on the defined flags

…衆ロ難τιáo~ 提交于 2021-01-28 04:21:19
问题 I have a small python script, which uses argparse to let the user define options. It uses two flags for different modes and an argument to let the user define a file. See the simplified example below: #!/usr/bin/python3 import argparse from shutil import copyfile def check_file(f): # Mock function: checks if file exists, else "argparse.ArgumentTypeError("file not found")" return f def main(): aFile = "/tmp/afile.txt" parser = argparse.ArgumentParser(description="An example",formatter_class