argparse

How to make python argparse mutually exclusive group arguments without prefix?

a 夏天 提交于 2019-11-27 19:08:19
问题 Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups: parser = argparse.ArgumentParser(prog='mydaemon') action = parser.add_mutually_exclusive_group(required=True) action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon') action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon') action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon') $ mydaemon -h usage: mydaemon [-h] (--start

In Python, using argparse, allow only positive integers

血红的双手。 提交于 2019-11-27 17:33:24
The title pretty much summarizes what I'd like to have happen. Here is what I have, and while the program doesn't blow up on a nonpositive integer, I want the user to be informed that a nonpositive integer is basically nonsense. import argparse parser = argparse.ArgumentParser() parser.add_argument("-g", "--games", type=int, default=162, help="The number of games to simulate") args = parser.parse_args() And the output: python simulate_many.py -g 20 Setting up... Playing games... .................... Output with a negative: python simulate_many.py -g -2 Setting up... Playing games... Now,

How to make python argparse mutually exclusive group arguments without prefix?

天涯浪子 提交于 2019-11-27 17:19:49
Python2.7 argparse only accepts optional arguments (prefixed) in mutually exclusive groups: parser = argparse.ArgumentParser(prog='mydaemon') action = parser.add_mutually_exclusive_group(required=True) action.add_argument('--start', action='store_true', help='Starts %(prog)s daemon') action.add_argument('--stop', action='store_true', help='Stops %(prog)s daemon') action.add_argument('--restart', action='store_true', help='Restarts %(prog)s daemon') $ mydaemon -h usage: mydaemon [-h] (--start | --stop | --restart) optional arguments: -h, --help show this help message and exit --start Starts

Case insensitive argparse choices

 ̄綄美尐妖づ 提交于 2019-11-27 17:04:37
问题 Is it possible to check argparse choices in case-insensitive manner? import argparse choices = ["win64", "win32"] parser = argparse.ArgumentParser() parser.add_argument("-p", choices=choices) print(parser.parse_args(["-p", "Win32"])) results in: usage: choices.py [-h] [-p {win64,win32}] choices.py: error: argument -p: invalid choice: 'Win32' (choose from 'win64','win32') 回答1: Transform the argument into lowercase by using type = str.lower for the -p switch. This solution was pointed out by

How do I avoid the capital placeholders in python's argparse module?

喜欢而已 提交于 2019-11-27 16:47:34
问题 There was a question that asked where they come from, and the accepted answer was a bunch of links to tutorials and source code. Explanation for argparse python modul behaviour: Where do the capital placeholders come from? None of it was helpful to me, I want to either get rid of them, or know their purpose. For example, a line like this: parser.add_argument('-c', '--chunksize', type=int, help='chunk size in bits') produces garbage like this: optional arguments: -h, --help show this help

importing a python script from another script and running it with arguments

对着背影说爱祢 提交于 2019-11-27 16:25:05
问题 I have a python script which has been packaged up as a command line script (dbtoyaml.py in Pyrseas since you ask). I am running another python script from which I want to call this script. Is there no way to import the module and artificially populate the required arguments from my second script to avoid changing any of the pyrseas code at all? from pyrseas import dbtoyaml -- My initial script, which also takes arguments dbtoyaml.main(['-m','-H MYHOSTNAME' .... other options]) Hasn't yet

python argparse help message, disable metavar for short options?

谁都会走 提交于 2019-11-27 15:17:59
I want to construct a argparser help message that looks like: -i, --input=INPUT help for input -o, --output=output help for output My current code: arg_parser = argparse.ArgumentParser arg_parser.add_argument('-i', '--input', dest='input', metavar='=INPUT', help='help for input') arg_parser.add_argument('-o', '--output', dest='output', metavar='=OUTPUT', help='help for output') arg_parser.print_help() is giving me -i =INPUT, --input =INPUT help for input -o =INPUT, --output =output help for output I just want to know how to get rid of the things in between short and long options. hpaulj Don't

Python argparse required=True but --version functionality?

心已入冬 提交于 2019-11-27 14:31:47
问题 In all my scripts I use the standard flags --help and --version , however I cannot seem to figure out how to make a --version with parser.add_argument(..., required=True) . import sys, os, argparse parser = argparse.ArgumentParser(description='How to get --version to work?') parser.add_argument('--version', action='store_true', help='print version information') parser.add_argument('-H', '--hostname', dest='hostname', required=True, help='Host name, IP Address') parser.add_argument('-d', '-

type=dict in argparse.add_argument()

三世轮回 提交于 2019-11-27 14:19:46
I'm trying to set up a dictionary as optional argument (using argparse); the following line is what I have so far: parser.add_argument('-i','--image', type=dict, help='Generate an image map from the input file (syntax: {\'name\': <name>, \'voids\': \'#08080808\', \'0\': \'#00ff00ff\', \'100%%\': \'#ff00ff00\'}).') But running the script: $ ./script.py -i {'name': 'img.png','voids': '#00ff00ff','0': '#ff00ff00','100%': '#f80654ff'} script.py: error: argument -i/--image: invalid dict value: '{name:' Even though, inside the interpreter, >>> a={'name': 'img.png','voids': '#00ff00ff','0': '

python argparse - optional append argument with choices

偶尔善良 提交于 2019-11-27 13:47:45
问题 I have a script where I ask the user for a list of pre-defined actions to perform. I also want the ability to assume a particular list of actions when the user doesn't define anything. however, it seems like trying to do both of these together is impossible. when the user gives no arguments, they receive an error that the default choice is invalid acts = ['clear','copy','dump','lock'] p = argparse.ArgumentParser() p.add_argument('action', nargs='*', action='append', choices=acts, default=[[