argparse

Python argparse mutual exclusive group

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:06:40
What I need is: pro [-a xxx | [-b yyy -c zzz]] I tried this but does not work. Could someone help me out? group= parser.add_argument_group('Model 2') group_ex = group.add_mutually_exclusive_group() group_ex.add_argument("-a", type=str, action = "store", default = "", help="test") group_ex_2 = group_ex.add_argument_group("option 2") group_ex_2.add_argument("-b", type=str, action = "store", default = "", help="test") group_ex_2.add_argument("-c", type=str, action = "store", default = "", help="test") Thanks! add_mutually_exclusive_group doesn't make an entire group mutually exclusive. It makes

argparse模块的使用 | python

萝らか妹 提交于 2019-11-26 15:59:28
argparse模块的使用 | python 用于接收python命令行下的命令行参数,并进行多种功能解析的模块 基本使用步骤: 定义一个文件名为test_parse.py的文件,内容如下: from argparse import ArgumentParser # 其中最常用的一个模块:ArgumentParser # 定义一个命令行参数解析器对象 parser = ArgumentParser() # 为命令行添加一个参数,参数名为argname1 parser.add_argument("argname1") # 返回命令行参数的集合的某种数据结构对象,args里存放了所有添加的命令行参数 args = parser.parse_args() # 打印测试命令行参数输入是否有效 print("输入的命令行参数为", args.argname1) 命令行输入: python test_parse.py 你好 运行结果: 输入的命令行参数为你好 为命令行参数添加帮助信息和数据类型 定义一个文件名为test_parse.py的文件,内容如下: from argparse import ArgumentParser # 其中最常用的一个模块:ArgumentParser # 定义一个命令行参数解析器对象 parser = ArgumentParser() # 为命令行添加一个参数

Why use argparse rather than optparse?

南笙酒味 提交于 2019-11-26 15:37:27
I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse . Why has yet another command-line parsing module been created? Why should I use it instead of optparse ? Are there new features that I should know about? Nicholas Knight As of python 2.7 , optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page ( https://code.google.com/archive/p/argparse/ ): handling positional arguments supporting sub-commands allowing alternative

Specify format for input arguments argparse python

為{幸葍}努か 提交于 2019-11-26 15:36:33
问题 I have a python script that requires some command line inputs and I am using argparse for parsing them. I found the documentation a bit confusing and couldn't find a way to check for a format in the input parameters. What I mean by checking format is explained with this example script: parser.add_argument('-s', "--startdate", help="The Start Date - format YYYY-MM-DD ", required=True) parser.add_argument('-e', "--enddate", help="The End Date format YYYY-MM-DD (Inclusive)", required=True)

Can't get argparse to read quoted string with dashes in it?

大憨熊 提交于 2019-11-26 14:36:56
Is there a way to make argparse recognize anything between two quotes as a single argument? It seems to keep seeing the dashes and assuming that it's the start of a new option I have something like: mainparser = argparse.ArgumentParser() subparsers = mainparser.add_subparsers(dest='subcommand') parser = subparsers.add_parser('queue') parser.add_argument('-env', '--extraEnvVars', type=str, help='String of extra arguments to be passed to model.') ...other arguments added to parser... But when I run: python Application.py queue -env "-s WHATEVER -e COOL STUFF" It gives me: Application.py queue:

Argparse - do not catch positional arguments with `nargs`.

时光毁灭记忆、已成空白 提交于 2019-11-26 14:35:54
问题 I am trying to write a function wo which you can parse a variable amount of arguments via argparse - I know I can do this via nargs="+" . Sadly, the way argparse help works (and the way people generally write arguments in the CLI) puts the positional arguments last. This leads to my positional argument being caught as part of the optional arguments. #!/usr/bin/python import argparse parser = argparse.ArgumentParser() parser.add_argument("positional", help="my positional arg", type=int) parser

argparse subparser monolithic help output

允我心安 提交于 2019-11-26 14:08:05
问题 My argparse has only 3 flags (store_true) on the top level, everything else is handled through subparsers. When I run myprog.py --help , the output shows a list of all subcommands like normal, {sub1, sub2, sub3, sub4, ...} . So, the default is working great... I usually can't remember the exact subcommand name I need, and all of its options. So I end up doing 2 help lookups: myprog.py --help myprog.py sub1 --help I do this so often, I decided to cram this into one step. I would rather have my

argparse option for passing a list as option

为君一笑 提交于 2019-11-26 11:58:56
I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option? parser.add_argument('-l', '--list', type=list, action='store', dest='list', help='<Required> Set flag', required=True) Script is called like below python test.py -l "265340 268738 270774 270817" TL;DR Use the nargs option or the 'append' setting of the action option (depending on how you want the user interface to behave). nargs parser.add_argument('-l','--list', nargs='+', help='<Required> Set flag', required=True) # Use like: # python arg.py -l 1234 2345 3456 4567 nargs=

Simple argparse example wanted: 1 argument, 3 results

最后都变了- 提交于 2019-11-26 11:26:16
The documentation for the argparse python module , while excellent I'm sure, is too much for my tiny beginner brain to grasp right now. I don't need to do math on the command line or meddle with formatting lines on the screen or change option characters. All I want to do is "If arg is A, do this, if B do that, if none of the above show help and quit" . My understanding of the original question is two-fold. First, in terms of the simplest possible argparse example, I'm surprised that I haven't seen it here. Of course, to be dead-simple, it's also all overhead with little power, but it might get

How to parse multiple nested sub-commands using python argparse?

时光毁灭记忆、已成空白 提交于 2019-11-26 10:19:34
问题 I am implementing a command line program which has interface like this: cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...] I have gone through the argparse documentation. I can implement GLOBAL_OPTIONS as optional argument using add_argument in argparse . And the {command [COMMAND_OPTS]} using Sub-commands. From the documentation it seems I can have only one sub-command. But as you can see I have to implement one or more sub-commands. What is the best way to parse