argparse

Python argparse integer condition (>=12)

感情迁移 提交于 2019-11-30 01:46:42
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 bandwidth is 12" sys.exit(1) I wonder if there is a way to obtain this result directly with some

How to modify the metavar for a positional argument in pythons argparse?

て烟熏妆下的殇ゞ 提交于 2019-11-30 01:37:37
问题 In the argparse package the metavar parameter modifies the displayed help message of a program. The following program is not intended to work, it is simply used to demonstrate the behavior of the metavar parameter. import argparse if __name__ == '__main__': parser = argparse.ArgumentParser(description = "Print a range.") parser.add_argument("-range1", nargs = 3, type = int, help = "Specify range with: start, stop, step.", metavar = ("start", "stop", "step")) parser.add_argument("-range2",

argparse: setting optional argument with value of mandatory argument

半腔热情 提交于 2019-11-30 01:31:34
问题 With Python's argparse, I would like to add an optional argument that, if not given, gets the value of another (mandatory) argument. parser.add_argument('filename', metavar = 'FILE', type = str, help = 'input file' ) parser.add_argument('--extra-file', '-f', metavar = 'ANOTHER_FILE', type = str, default = , help = 'complementary file (default: FILE)' ) I could of course manually check for None after the arguments are parsed, but isn't there a more pythonic way of doing this? 回答1: As far as I

python argparse file extension checking

一曲冷凌霜 提交于 2019-11-29 23:11:33
问题 can argparse be used to validate filename extensions for a filename cmd line parameter? e.g. if i have a python script i run from the cmd line: $ script.py file.csv $ script.py file.tab $ script.py file.txt i would like argparse to accept the 1st two filename cmd line options but reject the 3rd i know you can do something like this: parser = argparse.ArgumentParser() parser.add_argument("fn", choices=["csv","tab"]) args = parser.parse_args() to specify two valid choices for a cmd line option

Having options in argparse with a dash

萝らか妹 提交于 2019-11-29 22:34:36
I want to have some options in argparse module such as --pm-export however when I try to use it like args.pm-export I get the error that there is not attribute pm . How can I get around this issue? Is it possible to have - in command line options? Thomas Orozco As indicated in the argparse docs : For optional argument actions, the value of dest is normally inferred from the option strings. ArgumentParser generates the value of dest by taking the first long option string and stripping away the initial -- string . Any internal - characters will be converted to _ characters to make sure the

Custom tab completion in python argparse

僤鯓⒐⒋嵵緔 提交于 2019-11-29 21:22:01
How to get shell tab completion cooperating with argparse in a Python script? #!/usr/bin/env python import argparse def main(**args): pass if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('positional', choices=['spam', 'eggs']) parser.add_argument('--optional', choices=['foo1', 'foo2', 'bar']) args = parser.parse_args() main(**vars(args)) With an executable flag set on the .py file, the expected results should be something like: $ ./example.py sp<tab> -> completes to "./example.py spam" $ ./example.py --op<tab> -> completes to "./example.py --optional" $ .

Optional stdin in Python with argparse

試著忘記壹切 提交于 2019-11-29 21:18:23
I found the very useful syntax parser.add_argument('-i', '--input-file', type=argparse.FileType('r'), default='-') for specifying an input file or using stdin—both of which I want in my program. However, the input file is not always required. If I'm not using -i or redirecting input with one of $ someprog | my_python_prog $ my_python_prog < inputfile I don't want my Python program to wait for input. I want it to just move along and use default values. The standard library documentation for argparse suggests this solution to allow optional input/output files: >>> parser = argparse

python argh/argparse: How can I pass a list as a command-line argument?

时光怂恿深爱的人放手 提交于 2019-11-29 20:33:04
I'm trying to pass a list of arguments to a python script using the argh library. Something that can take inputs like these: ./my_script.py my-func --argA blah --argB 1 2 3 4 ./my_script.py my-func --argA blah --argB 1 ./my_script.py my-func --argA blah --argB My internal code looks like this: import argh @argh.arg('--argA', default="bleh", help='My first arg') @argh.arg('--argB', default=[], help='A list-type arg--except it\'s not!') def my_func(args): "A function that does something" print args.argA print args.argB for b in args.argB: print int(b)*int(b) #Print the square of each number in

argparse 命令行解析模块初级指南

喜夏-厌秋 提交于 2019-11-29 19:10:59
我在学习这个模块时,参考了 https://www.jianshu.com/p/00425f6c0936 这个篇文章,为了方便日后复盘,所以自己做一个总结。官方文档: https://docs.python.org/3/howto/argparse.html#introducing-positional-arguments 首先,要明白argparse的作用:它是命令行解析模块,目的是在不修改代码的前提下,能够修改程序的参数,提高代码的通用性。 接着就是说明argparse模块的使用,第一步: ---恢复内容结束--- 第一步:导入argparse import argparse 第二步:打开命令行解析 parser = argparse. ArgumentParser () 第三步: parser.add_argument("echo"), 用来指定程序将要接受哪些命令行参数的方法。并且与parser.add_argument("--echo")不同。 第四步: args = parser . parse_args (), 用来返回命令行的参数。 示例:1) import argparse parser = argparse.ArgumentParser() parser.add_argument("echo",type=int,default=100) args =

Python内置的一个用于命令项选项与参数解析的模块argparse

橙三吉。 提交于 2019-11-29 19:08:09
一、argparse简单使用 我们先来看一个简单示例。主要有三个步骤: 创建 ArgumentParser() 对象 调用 add_argument() 方法添加参数 使用 parse_args() 解析添加的参数 示例: import argparse parser = argparse.ArgumentParser() parser.add_argument('integer', type=int, help='display an integer') args = parser.parse_args() print(args.integer) 将上面的代码保存为文件 argparse_usage.py ,在终端运行,结果如下: λ python argparse_usage.py usage: argparse_usage.py [-h] integer argparse_usage.py: error: the following arguments are required: integer λ python argparse_usage.py abcd usage: argparse_usage.py [-h] integer argparse_usage.py: error: argument integer: invalid int value: 'abcd' λ