argparse

Sort argparse help alphabetically

我的未来我决定 提交于 2019-11-29 16:43:49
问题 I am using Python's (2.7) argparse facility and would like to automatically sort the help it produces alphabetically by option. By default help entries are sorted in the order they are added*, as in: p = argparse.ArgumentParser(description='Load duration curves and other plots') p.add_argument('--first', '-f', type=int, default=1, help='First Hour') p.add_argument('--dur', '-d', type=int, default=-1, help='Duration in Hours. Use -1 for all') p.add_argument('--title', '-t', help='Plot Title

Argparse - How to Specify a Default Subcommand

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 16:35:53
问题 I am using the argparse package of Python 2.7 to write some option-parsing logic for a command-line tool. The tool should accept one of the following arguments: "ON": Turn a function on. "OFF": Turn a function off. [No arguments provided]: Echo the current state of the function. Looking at the argparse documentation led me to believe that I wanted two--possibly three--subcommands to be defined, since these three states are mutually exclusive and represent different conceptual activities. This

How to pass on argparse argument to function as kwargs?

我只是一个虾纸丫 提交于 2019-11-29 16:04:40
问题 I have a class defined as follows class M(object): def __init__(self, **kwargs): ...do_something and I have the result of argparse.parse_args() , for example: > args = parse_args() > print args Namespace(value=5, message='test', message_type='email', extra="blah", param="whatever") I want to pass on the values of this namespace (except message_type ) to create an instance of the class M . I have tried M(args) but got an error TypeError: __init__() takes exactly 1 argument (2 given) which I do

How to show help for all subparsers in argparse?

*爱你&永不变心* 提交于 2019-11-29 15:03:35
I have made a Python script that is doing a lot of actions, so it has many options, so I divided it to subparsers that also use parent parsers for common options grouping. I want a help option that will show the help for all commands with their options, is it possible without overriding the format_help method? I saw a similar question , but the grouping is not critical for me, I just want the options there. For example: general_group = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,add_help=False) general_group.add_argument('--threads', action='store_true',

How to handle ampersand as part a command line argument in python

眉间皱痕 提交于 2019-11-29 14:47:26
I have a program in python 2.7 which accepts command line parameters using argparse , however if I try to enter a string containing an ampersand I lose the characters after that ampersand. For example: I have a simple python program just to test the input of command line arguments and simply prints out what was entered for a single command line parameter. Essentially: print args.where When I run the program with an argument like this: $ python args.py -a http://www.website.com?optionone=one&numbertwo=two The only text printed to the screen is: http://www.website.com?optionone=one I have

argparse default option based on another option

99封情书 提交于 2019-11-29 13:12:20
Suppose I have an argparse python script: import argparse parser = argparse.ArgumentParser() parser.add_argument("--foo", required=True) Now I want to add another option --bar, which would default to appending "_BAR" to whatever was specified by --foo argument. My goal: >>> parser.parse_args(['--foo', 'FOO']) >>> Namespace(foo='FOO', bar="FOO_BAR") AND >>> parser.parse_args(['--foo', 'FOO', '--bar', 'BAR']) >>> Namespace(foo='FOO', bar="BAR") I need something like this: parser.add_argument("--bar", default=get_optional_foo + "_BAR") Here's another attempt at writing a custom Action class

How to stop Python program compiled in py2exe from displaying ImportError: No Module names 'ctypes'

折月煮酒 提交于 2019-11-29 12:38:28
I was wondering if this might be a compilation error or if there is something I can do to stop it from displaying. I have made an argparse program for cmd. I compiled it with py2exe and when I run it, it exacutes the program properly but always gives this error before running the code: Traceback (most recent call last): File "boot_common.py", line 46, in <module> ImportError: No module named 'ctypes' If it is something in my code, here is my script: import argparse import zipfile import os from contextlib import closing def parse_args(): parser = argparse.ArgumentParser('ziputil '+\ '-m <mode>

Don't show long options twice in print_help() from argparse

与世无争的帅哥 提交于 2019-11-29 12:18:10
问题 I have the following code: parser = argparse.ArgumentParser(description='Postfix Queue Administration Tool', prog='pqa', usage='%(prog)s [-h] [-v,--version]') parser.add_argument('-l', '--list', action='store_true', help='Shows full overview of all queues') parser.add_argument('-q', '--queue', action='store', metavar='<queue>', dest='queue', help='Show information for <queue>') parser.add_argument('-d', '--domain', action='store', metavar='<domain>', dest='domain', help='Show information

Python argparse type and choice restrictions with nargs > 1

狂风中的少年 提交于 2019-11-29 11:39:57
问题 The title pretty much says it all. If I have nargs greater than 1, is there any way I can set restrictions (such as choice/type) on the individual args parsed? This is some example code: parser = argparse.ArgumentParser() parser.add_argument('-c', '--credits', nargs=2, help='number of credits required for a subject') For the -c argument I need to specify a subject and how many credits are required. The subject should be limited to a predefined list of subjects, and the number of credits

Passing arguments into os.system

▼魔方 西西 提交于 2019-11-29 11:33:01
I need to execute the following command through python. rtl2gds is a tool which reads in 2 parameters: Path to a file and a module name rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn I am reading in the path to the file and module name from the user through argparse as shown below: parser = argparse.ArgumentParser(description='Read in a file..') parser.add_argument('fileread', type=argparse.FileType('r'), help='Enter the file path') parser.add_argument('-e', help='Enter the module name', dest='module_name') args = parser.parse_args() os.system("rtl2gds -rtl=args.fileread -rtl