argparse

Python ArgumentParser nested arguments

那年仲夏 提交于 2019-12-05 14:31:33
I want to create argument parser with following signature: ./myapp [-a [-b BVAL] | -c] In other words, user could provide argument -b BVAL only in case if he provided argument -a . It's quite easy to create mutually exclusive group of -a and -c , but I can't figure out how to create relationship allow -b only if -a provided You could inherit from ArgumentParser to add some custom functionality. Here I am raising an exception, but you could modify this to implement whatever you would like. Just change the on_dependency_error() method to suit your needs. from argparse import ArgumentParser class

why args.add_argument works when given in two separate statements but not one in python?

谁都会走 提交于 2019-12-05 13:28:36
I am trying to use argparse module to parse the arguments in command line. here is the sample code import argparse parser = argparse.ArgumentParser() parser.add_argument('bar') parser.add_argument('-foo') args=parser.parse_args() print args running this python argparsing.py "hello" Namespace(bar='hello', foo=None) however, this does not work import argparse parser = argparse.ArgumentParser() parser.add_argument('bar','--foo') #parser.add_argument('-foo') args=parser.parse_args() print args running this gives error python argparsing.py "hello" Traceback (most recent call last): File "argparsing

Single dash for argparse long options

不问归期 提交于 2019-12-05 13:18:43
Is it possible to make it so that --longoption is represented as -longoption using argparse? argparse.prefix_chars doesn't work, as it is assumed that the prefix char would be repeated for a long option. I'm thinking perhaps there is a way to turn off short options and allow long options to use a single dash instead of a double dash. Something like this: parser = argparse.ArgumentParser() parser.turn_off_short_opts() Can this be done? If not, what can I use to accomplish this? Single dash long arguments aren't a problem: In [250]: p=argparse.ArgumentParser() In [251]: p.add_argument('

Handle invalid arguments with argparse in Python

白昼怎懂夜的黑 提交于 2019-12-05 13:01:09
I am using argparse to parse command line arguments and by default on receiving invalid arguments it prints help message and exit. Is it possible to customize the behavior of argparse when it receives invalid arguments? Generally I want to catch all invalid arguments and do stuff with them. I am looking for something like: parser = argparse.ArgumentParser() # add some arguments here try: parser.parse_args() except InvalidArgvsError, iae: print "handle this invalid argument '{arg}' my way!".format(arg=iae.get_arg()) So that I can have: >> python example.py --invalid some_text handle this

argparse: some mutually exclusive arguments in required group

邮差的信 提交于 2019-12-05 12:24:49
问题 I have a set of arguments that can logically be separated in 2 groups: Actions: A1 , A2 , A3 , etc. Informations: I1 , I2 , I3 , etc. At least one of these arguments is required for the program to start, but "information" args can be used with "action" args. So At least one in Actions or Informations is required All Actions are mutually exclusive I can't find how to do it using argparse. I know about add_mutually_exclusive_group and its required argument, but I can't use it on "Actions"

Python argparse conditional requirements

ぐ巨炮叔叔 提交于 2019-12-05 12:00:14
问题 How do I set up argparse as follows: if -2 is on the command line, no other arguments are required if -2 is not on the command line, -3 and -4 arguments are required For example, -2 [good] -3 a -4 b [good] -3 a [not good, -4 required] -2 -5 c [good] -2 -3 a [good] There are a number of similar questions here, but either they don't address this situation or I don't understand. Python 2.7 if that matters. 回答1: A subparser (as suggested in comments) might work. Another alternative (since

Why does argparse give me a list-in-a-list?

穿精又带淫゛_ 提交于 2019-12-05 11:59:36
问题 I just noticed a behavior in argparse that puzzled me (guess I'd never used it for a dumb list of files before): import argparse parser = argparse.ArgumentParser() parser.add_argument('multi', action='append', nargs='+') print(parser.parse_args()) This gives me the output: ~$ ./testargs.py foo bar baz Namespace(multi=[['foo', 'bar', 'baz']]) ~$ I expected multi to be ['foo', 'bar', 'baz'] , not a list within a list. As-is, I'll have to grab args.multi[0] before processing, which isn't a big

Can argparse in python 2.7 be told to require a minimum of TWO arguments?

∥☆過路亽.° 提交于 2019-12-05 11:04:24
My application is a specialized file comparison utility and obviously it does not make sense to compare only one file, so nargs='+' is not quite appropriate. nargs=N only excepts a maximum of N arguments, but I need to accept an infinite number of arguments as long as there are at least two of them. Short answer is you can't do that because nargs doesn't support something like '2+'. Long answer is you can workaround that using something like this: parser = argparse.ArgumentParser(usage='%(prog)s [-h] file file [file ...]') parser.add_argument('file1', nargs=1, metavar='file') parser.add

Argparse custom help from text file

梦想的初衷 提交于 2019-12-05 10:33:09
I want to use the argparse library because of its flexibility, but I am having trouble disabling the default help dialogue to show a custom one from a text file. All I want to do is display the text from the text file when the "-h" or "--help" option is passed. Here is an example of how I am trying this: parser = argparse.ArgumentParser(add_help=False) parser.add_argument("file", type=str, nargs='+') parser.add_argument("-xmin", type=float) parser.add_argument("-xmax", type=float) parser.add_argument("-ymin", type=float) parser.add_argument("-ymax", type=float) parser.add_argument("-h", "-

Python3之命令行参数处理

核能气质少年 提交于 2019-12-05 09:47:49
sys模块 常用单元 getopt模块 optparse模块 argparse模块 toc sys模块 sys模块代表了Python解释器,主要用于获取和Python解释器相关的信息,其中 sys.argv 可以获取命令行参数 在Python交互式解释器中可以先导入sys模块 import sys ,再输入 dir(sys) 查看sys模块所包含的全部程序单元(包括变量、函数等),或者 [i for i in dir(sys) if not i.startswith('_')] 过滤掉隐藏的单元 常用单元 sys.argv :以列表的方式获取运行 Python 程序的命令行参数存放其中。其中 sys.argv[0] 通常就是指该 Python程序本身, sys.argv[1] 代表第一个参数, sys.argv[2] 代表第二个参数,以此类推。类似于 Shell 中的 $0、$1、$2 import sys print('程序名称为:{},第一个参数为:{},第二个参数为:{}'.format(sys.argv[0], sys.argv[1], sys.argv[2])) sys.byteorder :显示本地字节序的指示符。如果本地字节序的大端模式,则该属性返回big,否则返回little sys.copyright :该属性返回与 Python 解释器有关的版权信息 sys