argparse

Python argparse: Insert blank line between help entries

妖精的绣舞 提交于 2019-12-03 12:24:06
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" -p EXECUTABLE, --player EXECUTABLE The program to use to play sounds. Default: "play" -s, --silent If

Print program usage example with argparse module

 ̄綄美尐妖づ 提交于 2019-12-03 10:31:45
问题 I am trying to learn how to use python's argparse module. Currently my python script is: parser = argparse.ArgumentParser(description='My first argparse attempt', add_help=True) parser.add_argument("-q", action ="store", dest='argument', help="First argument") output = parser.parse_args() And it gives the output as : usage: test.py [-h] [-q ARGUMENT] My first argparse attempt optional arguments: -h, --help show this help message and exit -q ARGUMENT First argument Now, lets suppose I want my

What does metavar and action mean in argparse in Python?

别说谁变了你拦得住时间么 提交于 2019-12-03 10:27:12
问题 I am reading through argparse module. I got stuck as what to metavar and action means >>> 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=max, ... help='sum the integers (default: find the max)') I might have missed but from what I read, I could not find definitions for metavar and action (action="store_const", etc) . what do they

Python: argparse optional arguments without dashes

旧时模样 提交于 2019-12-03 10:06:06
I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2 FILE2 using parser.add_argument('--file1',type=file) parser.add_argument('--file2',type=file) however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument... In other words, is it possible to specifically tell argparse whether an argument is optional or positional so that I can have optional parameters without the dashes?

python argparse: How can I display help automatically on error?

半腔热情 提交于 2019-12-03 09:51:23
Currently when I enter invalid options or omit positional arguments, argparse kicks me back to the prompt and displays the usage for my app. This is ok, but I would rather automatically display the full help listing (that explains the options, etc) than require the user to type ./myscript.py -h Thanks! Jamie This thread over at Google groups has the following code snippet which seems to do the trick (modified slightly). class DefaultHelpParser(argparse.ArgumentParser): def error(self, message): sys.stderr.write('error: %s\n' % message) self.print_help() sys.exit(2) To print help you might want

Python argparse fails to parse hex formatting to int type

女生的网名这么多〃 提交于 2019-12-03 09:50:32
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 correctly handle this? devanl argparse is looking to create a callable type conversion from the 'type'

How to make a short and long version of a required argument using Python Argparse?

ⅰ亾dé卋堺 提交于 2019-12-03 09:44:05
I want to specify a required argument called inputdir but I also would like to have a shorthand version of it called i . I don't see a concise solution to do this without making both optional arguments and then doing my own check. Is there a preferred practice for this that I'm not seeing or the only way is to make both optional and do my own error-handling? Here is my code: import argparse parser = argparse.ArgumentParser() parser.add_argument("inputdir", help="Specify the input directory") parser.parse_args() For flags (options starting with - or -- ) pass in options with the flags. You can

TypeError: __init__() got an unexpected keyword argument 'type' in argparse

╄→尐↘猪︶ㄣ 提交于 2019-12-03 09:42:47
Hey so I'm using argparse to try and generate a quarterly report. This is what the code looks like: parser = argparse.ArgumentParser() parser.add_argument('-q', "--quarter", action='store_true', type=int, help="Enter a Quarter number: 1,2,3, or 4 ") parser.add_argument('-y', "--year", action='store_true',type=str,help="Enter a year in the format YYYY ") args = parser.parse_args() the error I receive is: TypeError: init () got an unexpected keyword argument 'type' as far as I can tell from the argparse documentation type is one of the parameters of the add_argument function. I tried removing

Argparse“ArgumentError: argument -h/--help: conflicting option string(s): -h, --help”

﹥>﹥吖頭↗ 提交于 2019-12-03 09:27:38
Recently, I am learning argparse module, Argument error occurred below the code import argparse import sys class ExecuteShell(object): def create(self, args): """aaaaaaa""" print('aaaaaaa') return args def list(self, args): """ccccccc""" print('ccccccc') return args def delete(self, args): """ddddddd""" print('ddddddd') return args class TestShell(object): def get_base_parser(self): parser = argparse.ArgumentParser() parser.add_argument('-h', '--help', action='store_true', help=argparse.SUPPRESS) parser.add_argument('-c', action='store', dest='create_value', help='create a file') parser.add

Python dependencies between groups using argparse

我的梦境 提交于 2019-12-03 09:03:49
I started to learn Python, and now I'm learning the great benefits of argparse . Using argparse , I have created two groups of arguments: group_list and group_simulate . Each of the groups has its own arguments -- the user can specify only one argument in each group (achieved using parser.add_mutually_exclusive_group() ). And now my target is present a syntax error if the user specified arguments from both groupgs and not from only one of them -- I want to achieve this by using the capabilities of argparse and not by writing a method that asks if this and this was specified print syntax error.