argparse

python argparse extra args

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 01:36:21
问题 i would like to get extra args using argparse but without known what are they. for example, in maven we can add parameters in the form: -Dmaven.test.skip=true or -Dcmd=compiler:compile i would like to get the same thing in python using argparse , and get some kind of dict with all the args.. i know i can use: aparser.parse_known_args() but then i need to parse me extra args (remove the -D and split by = ). Was wondering if there is something out of the box? Thanks! 回答1: You can use parser.add

Simulating argparse command line arguments input while debugging

。_饼干妹妹 提交于 2019-12-21 20:53:35
问题 This thread is an extension from the previous that can be found here. Say, I have a code that serve two purpose, 1) print a max number from a list of integer; 2) make a new dir. import argparse import os parser = argparse.ArgumentParser() parser.add_argument('integers', metavar='N', type=int, nargs='+', help='an integer for the accumulator') parser.add_argument('--sum', dest='accumulate', action='store_const', const=sum, default=sum, help='sum the integers (default: find the max)') parser.add

How to add optional or once arguments?

老子叫甜甜 提交于 2019-12-21 18:05:11
问题 How can I add an argument that is optional and must not be specified multiple times? Valid: $ ./my.py $ ./my.py --arg MyArgValue Invalid: $ ./my.py --arg MyArgValue --arg ThisIsNotValid If I add an argument like: parser.add_argument('--arg', type=str) The invalid example results in a string ThisIsNotValid . I would expect a parser error. 回答1: Create a custom action that raises an exception if the same argument is seen twice. When the parser catches the exception, it prints the usage and a

named argument in argparse [duplicate]

落花浮王杯 提交于 2019-12-21 11:25:35
问题 This question already has answers here : Simple argparse example wanted: 1 argument, 3 results (10 answers) Closed 3 years ago . I want to send arguments to script by their name (something like kwargs). I tried something like this but it's not doing what I want: (let's say it's written in script.py) import argparse parser = argparse.ArgumentParser() parser.add_argument("name") args = parser.parse_args() and then writing in commant line: script.py name = david another thing, let's say I have

Python doctest for shell scripts that test argument parsing without polluting docstring with os.popen()

混江龙づ霸主 提交于 2019-12-21 10:17:14
问题 Is there a way to write a python doctest string to test a script intended to be launched from the command line (terminal) that doesn't pollute the documentation examples with os.popen calls? #!/usr/bin/env python # filename: add """ Example: >>> import os >>> os.popen('add -n 1 2').read().strip() '3' """ if __name__ == '__main__': from argparse import ArgumentParser p = ArgumentParser(description=__doc__.strip()) p.add_argument('-n',type = int, nargs = 2, default = 0,help = 'Numbers to add.')

Passing a tuple as command line argument [duplicate]

为君一笑 提交于 2019-12-21 07:55:14
问题 This question already has answers here : converting string to tuple (3 answers) Closed 4 years ago . My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)' . I tried by giving type=tuple for argparse.add_argument , but is of no use here. Do I have to add a new type class and pass that to type argument of add_argument ? Update I tried the ast.literal_eval based on

Passing a tuple as command line argument [duplicate]

天涯浪子 提交于 2019-12-21 07:51:20
问题 This question already has answers here : converting string to tuple (3 answers) Closed 4 years ago . My requirement is to pass a tuple as command line argument like --data (1,2,3,4) I tried to use the argparse module, but if I pass like this it is receiving as the string '(1,2,3,4)' . I tried by giving type=tuple for argparse.add_argument , but is of no use here. Do I have to add a new type class and pass that to type argument of add_argument ? Update I tried the ast.literal_eval based on

Python’s argh library: preserve docstring formatting in help message

爱⌒轻易说出口 提交于 2019-12-21 04:25:25
问题 While searching for faster ways to parse command-line arguments in my scripts I came across the argh library. I really like the features of argh but I’ve encountered one drawback that stops me from using it, and this has to do with the default help message that gets displayed if I’m invoking the —help option: per default the function’s docstring is displayed on top of the arguments list. This is great, however the initial formatting is lost. See, for example, the following example script

Python argparse: Insert blank line between help entries

牧云@^-^@ 提交于 2019-12-21 04:07:25
问题 When using argparse, passing --help to the program generates help text. Unfortunately, it's hard to read because there are no blank lines between options. Here's an excerpt to illustrate: optional arguments: -h, --help show this help message and exit -u FILENAME, --up-sound FILENAME The sound to play when the network comes up. Default: "/path/to/some/sound/file.wav" -d FILENAME, --down-sound FILENAME The sound to play when the network goes down. Default: "/path/to/some/other/sound/file.wav"

Python argparse fails to parse hex formatting to int type

馋奶兔 提交于 2019-12-21 03:17:41
问题 I have the following code which attempts to get the DUT VID from the invoked command line: parser = argparse.ArgumentParser(description='A Test', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) group.add_argument("--vid", type=int, help="vid of DUT") options = parser.parse_args() Consider the command line "python test.py --vid 0xabcd" I notice that argparse is raising an exception on this as it fails to complete the call int('0xabcd') because it is base 16. How do I get argparse to