argparse

python argparse optional positional argument with detectable switch

折月煮酒 提交于 2019-12-11 14:16:51
问题 I would like to invoce my programm like program -s <optional value> . I would like to assign a default value, but would also like to be able to detect if the -s switch was given. What I have: max_entries_shown = 10 import argparse parser = argparse.ArgumentParser() parser.add_argument("-s", nargs = '?', default = max_entries_shown) args = parser.parse_args() This gives me a value of 10 for args.s if I don't give -s on the command line, and None if I specify -s without a value. What I want is

Argparse nargs=“+” is eating positional argument

时光怂恿深爱的人放手 提交于 2019-12-11 12:59:13
问题 Here's a subsection of my parser configuration parser.add_argument( 'infile', help="The file to be imported", type=argparse.FileType('r'), default=sys.stdin ) parser.add_argument( '--carpark', nargs='+', dest='CarparkID', type=int, default=[], help="One or many carpark IDs" ) However, the --carpark argument seems to be too greedy and eats anything that follows it: $ mycommand --carpark 17 ~/path-to-file mycommand: error: argument --carpark: invalid int value: '/home/oli/path-to-file' What's a

Short form of argparse option permisive of many characters

本小妞迷上赌 提交于 2019-12-11 12:51:58
问题 I was checking this python file where it seems to use a short argument with 2 characters. parser.add_argument( '-gt', '--gtfolder', dest='gtFolder', metavar='', help='folder containing your ground truth bounding boxes') I thought short arguments were meant to contain only a single character and that came as a surprise to me. Indeed in the documentation it states that short options (options only one character long)... . On the other hand the code seems to work (at least as regards argparse

argparse - how pass to a method with kwargs or argv

本秂侑毒 提交于 2019-12-11 10:52:08
问题 I've been looking for a way to use **kwargs or *argv with argparse . I will from hard code to a dynamic way. Here is my hard code and a example how I will use it. def get_parser(): parser = argparse.ArgumentParser() parser.add_argument("-r", "--range", dest="r", nargs=8, help="AddRange Parameters") parser.add_argument("-p", "--parameters", dest="p", nargs=8, help="SetDefaults as Parameters") parser.add_argument("-r", "--range", dest="r", nargs=8, help="AddRange Parameters") return parser ""

python的argparse模块(转)

雨燕双飞 提交于 2019-12-11 10:27:41
3 # -*- coding: utf-8 -*- 4 5 import argparse 6 7 args = "-f hello.txt -n 1 2 3 -x 100 -y b -z a -q hello @args.txt i_am_bar -h".split() 8 # 使用@args.txt要求fromfile_prefix_chars="@" 9 # args.txt文件中应该一行一个参数,想改变行为参考convert_arg_line_to_args() 10 11 12 # ArgumentParser参数的简单说明 13 ## description - 命令行帮助的开始文字,大部分情况下,我们只会用到这个参数 14 # epilog - 命令行帮助的结尾文字 15 # prog - (default: sys.argv[0])程序的名字,一般不需要修改,另外,如果你需要在help中使用到程序的名字,可以使用%(prog)s 16 # prefix_chars - 命令的前缀,默认是-,例如-f/--file。有些程序可能希望支持/f这样的选项,可以使用prefix_chars="/" 17 # fromfile_prefix_chars - (default: None)如果你希望命令行参数可以从文件中读取,就可能用到。例如,如果fromfile_prefix

Argparse optional argument with different default if specified without a value

孤街浪徒 提交于 2019-12-11 09:52:15
问题 Consider three different runs of a program: python3 prog.py python3 prog.py --x python3 prog.py --x 2 Is it possible to use argparse such that, for example, in the first case, x==None , in the second case, x==1 , and in the third, x==2 ? 回答1: nargs'?' with a const parameter handles this 3-way input nicely.. In [2]: parser = argparse.ArgumentParser() In [3]: parser.add_argument('-x','--x', nargs='?', type=int, const=1) ... In [4]: parser.parse_args([]) Out[4]: Namespace(x=None) In [5]: parser

Python argparse; if argument 1 not present, how to continue and set required next arguments

本小妞迷上赌 提交于 2019-12-11 09:48:33
问题 I have 3 arguments in my python script. Two are required for running the script and argument 1 shows only a list of options. I want that if argument 1 is set, that argument 2 and 3 are not required anymore, and if argument 1 is not used, that argument 2 and 3 are required. The arguments are as follows: parser = argparse.ArgumentParser() parser.add_argument("--1", help="list of options", action="store_true", default=False) parser.add_argument("--2", help="level", required=True) parser.add

Is it possible to set argparse's optional argument to a default value based on other optional argument?

被刻印的时光 ゝ 提交于 2019-12-11 09:45:14
问题 I am using argparse and I have two optional arguments: parser.add_argument('-a', '--arg1', default=1, type=int) parser.add_argument('-b', '--arg2', action='store_true', default=False) Is there a way to set arg1 default value of "1" if only if arg2 is set True ? In other word, I only want to do the following only if arg2 is set to True : parser.add_argument('-a', '--arg1', default=1, type=int) Otherwise, it will be set to: parser.add_argument('-a', '--arg1', type=int) 回答1: A test after parsing

Argparse - accessing multiple arguments at once

不想你离开。 提交于 2019-12-11 09:03:46
问题 my application uses a database, so when adding a new element (from command line) I want to check if this one is already in the database, what I do with the "type" parameter of add_argument: def check_uniq(project_name): if Project.exists(project_name): raise argparse.ArgumentTypeError( return project_name this is working just fine, however to make think easier to the final user I'd like to add a --force option to my arguments so this variable is tested and delete before to add and in this

cannot have multiple subparser arguments

Deadly 提交于 2019-12-11 08:47:01
问题 So i'm trying to build an CLI with the following pattern: cli.py api new --config config.json or cli.py api del [api_name] To achieve the api i've added it as sup parser if __name__ == '__main__': parser = argparse.ArgumentParser(prog='my prog') subparsers = parser.add_subparsers(title='api', help='available actions') api_parser = subparsers.add_parser('api') from here i thought that we might want to add two new subparsers to handle the new and del subcommands: if __name__ == '__main__':