argparse

List of arguments with argparse

旧时模样 提交于 2019-12-19 18:56:46
问题 I'm trying to pass a list of arguments with argparse but the only way that I've found involves rewriting the option for each argument that I want to pass: What I currently use: main.py -t arg1 -a arg2 and I would like: main.py -t arg1 arg2 ... Here is my code: parser.add_argument("-t", action='append', dest='table', default=[], help="") 回答1: Use nargs: ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The nargs keyword argument

How to test Python classes that depend on argparse?

对着背影说爱祢 提交于 2019-12-19 08:59:15
问题 The below paste contains relevant snippets from three separate Python files. The first is a script called from the command line which instantiates CIPuller given certain arguments. What happens is that the script gets called with something like: script.py ci (other args to be swallowed by argparse). The second is part of a subclass called Puller . The third is part of a subclass of Puller called CIPuller . This works wonderfully, as the correct subclass is called, and any user using the wrong

Python; argparse; how to specify position of positional arguments

大城市里の小女人 提交于 2019-12-19 08:17:45
问题 How do I specify the position of a positional argument? In my example below, the SCR & SCV arguments appear at the end of the optional arguments, I want them to appear at the beginning. #!/usr/bin/python import argparse ### Parse arguments ### parser = argparse.ArgumentParser() parser.add_argument("SCR",type=int) parser.add_argument("SCV",type=int) parser.add_argument("--itemid",nargs='?') parser.add_argument("--tkt",nargs='?') parser.add_argument("--rfc",nargs='?') parser.add_argument("-

Python: Extract variables out of namespace

房东的猫 提交于 2019-12-19 06:11:21
问题 I'm using argparse in python to parse commandline arguments: parser = ArgumentParser() parser.add_argument("--a") parser.add_argument("--b") parser.add_argument("--c") args = parser.parse_args() Now I want to do some calculations with a , b , and c . However, I find it tiresome to write args.a + args.b + args.c all the time. Therefore, I'm extracting those variables: a, b, c = [args.a, args.b, args.c] Such that I can write a + b + c . Is there a more elegant way of doing that? Manual

Python argparse argument with quotes

柔情痞子 提交于 2019-12-19 05:53:34
问题 Is there any way I can tell argparse to not eat quotation marks? For example, When I give an argument with quotes, argparse only takes what's inside of the quotes as the argument. I want to capture the quotation marks as well (without having to escape them on the command line.) pbsnodes -x | xmlparse -t "interactive-00" produces interactive-00 I want "interactive-00" 回答1: I think it is the shell that eats them, so python will actually never see them. Escaping them on the command line may be

argparse argument order

核能气质少年 提交于 2019-12-18 18:51:29
问题 I have a little problem. I use argparse to parse my arguments, and it's working very well. To have the args, I do : p_args = parser.parse_args(argv) args = dict(p_args._get_kwargs()) But the problem with p_args is that I don't know how to get these arguments ordered by their position in the command line, because it's a dict. So is there any possibility to have the arguments in a tuple/list/ordered dict by their order in the command line? 回答1: To keep arguments ordered, I use a custom action

Python: argparse subcommand subcommand?

旧时模样 提交于 2019-12-18 16:49:58
问题 I have a program that has many available options. For example a configuration option to change settings. ./app config -h gives me the help using normal argparse subcommands now i would like to add another subcommand to the config subcommand called list to list config values ./app config list additionally that command should accept another option so that i could say ./app config list CATEGORY only to list the config of one category my code right now is basically this just with more commands >>

Check if argparse optional argument is set or not

 ̄綄美尐妖づ 提交于 2019-12-18 11:14:12
问题 I would like to check whether an optional argparse argument has been set by the user or not. Can I safely check using isset? Something like this: if(isset(args.myArg)): #do something else: #do something else Does this work the same for float / int / string type arguments? I could set a default parameter and check it (e.g., set myArg = -1, or "" for a string, or "NOT_SET"). However, the value I ultimately want to use is only calculated later in the script. So I would be setting it to -1 as a

Python argparse integer condition (>=12)

僤鯓⒐⒋嵵緔 提交于 2019-12-18 11:06:05
问题 I need to request that an argument is >= 12 using argparse . I cannot find a way to obtain this result using argparse , it seems there's no way to set rules to a given value but only full sets of accepted values like choices=['rock', 'paper', 'scissors']. My code is: import sys, argparse parser = argparse.ArgumentParser() parser.add_argument("-b", "--bandwidth", type=int, help="target bandwidth >=12") args = parser.parse_args() if args.bandwidth and args.bandwidth < 12: print "ERROR: minimum

Argparse: Check if any arguments have been passed

爷,独闯天下 提交于 2019-12-18 10:14:04
问题 My script should start a demo mode, when the no parameters are given. I tried this: args = parser.parse_args() if len(args) == 0: run_demo() else: # evaluate args Which gives a *** TypeError: object of type 'Namespace' has no len() as args is no list. How would I achieve what I want? 回答1: If your goal is to detect when no argument has been given to the command, then doing this via argparse is the wrong approach (as Ben has nicely pointed out). Think simple! :-) I believe that argparse does