argparse

Using 'argparse' as opposed to sys.argv

情到浓时终转凉″ 提交于 2019-12-22 18:34:17
问题 My script currently uses sys.argv to check for an input file provided to the program. I am trying to utilise argparse instead but I cant seem to get it to work. I was able to set it up and add an argument, but when I parse an argument, and print that parsed argument, I get a namespace. How can I get a string? Basically, I want to take the argument as a string, and open a file with that name. Currently, my sys.argv is: filename = sys.argv[1] f = open(filename, 'r') My argparse prints out a

Python模块学习 - Argparse

不打扰是莪最后的温柔 提交于 2019-12-22 15:03:19
argparse模块   在Python中,argparse模块是标准库中用来解析命令行参数的模块,用来替代已经过时的optparse模块。argparse模块能够根据程序中的定义从sys.argv中解析出来这些参数,并自动生成帮助和使用信息。 ArgumentParse解析器   使用argparse解析命令行参数时,首先需要创建一个解析器: import argparse parser = argparse.ArgumentParser()   ArgumentParser类的初始化函数有多个参数,其中比较常用的是description,它是程序的描述信息,即帮助信息前面的文字。 添加参数选项   为应用程序添加参数选项需要使用ArgrmentParser对象的add_argument方法,该方法的格式如下: add_argument(name or flag...[, action ] [, nargs ] [, const ] [, default ] [, type ] [, choices ] [, required ] [,help ] [,metavar ] [, dest ]   各个参数含义如下: name/flags:参数的名字   action:遇到参数时的动作,默认值时store。   nargs:参数的个数,可以时具体的数字,或者是”+“号或者”*“号

python argparse

梦想的初衷 提交于 2019-12-22 14:18:18
参考https://www.cnblogs.com/lindaxin/p/7975697.html # python3中argparse模块 # 简单用法 import argparse parser = argparse.ArgumentParser() parser.add_argument('test') # 必选参数 add_argument()指定程序可以接受的命令行选项 parser.add_argument('--test_a') # 可选参数 args = parser.parse_args() # parse_args()从指定的选项中返回一些数据 print(args) print(args.test) print(args.test_a) # cmd执行: python tests.py 1 --test 2 # 输出: Namespace(test='1', test_a='2') 1 2 混合使用 定位参数和选项参数可以混合使用,看下面一个例子,给一个整数序列,输出它们的和或最大值(默认): import argparse parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('integers', metavar='N',

argparse doesn't check for positional arguments

半城伤御伤魂 提交于 2019-12-22 11:33:47
问题 I'm creating a script that takes both positional and optional arguments with argparse. I have gone through Doug's tutorial and the python Docs but can't find an answer. parser = argparse.ArgumentParser(description='script to run') parser.add_argument('inputFile', nargs='?', type=argparse.FileType('rt'), parser.add_argument('inputString', action='store', nargs='?') parser.add_argument('-option1', metavar='percent', type=float, action='store') parser.add_argument('-option2', metavar='outFile1',

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

穿精又带淫゛_ 提交于 2019-12-22 08:38:46
问题 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

Python ArgumentParser nested arguments

不羁的心 提交于 2019-12-22 08:11:58
问题 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 回答1: 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

How to pass command line arguments in IPython jupyter notebook

白昼怎懂夜的黑 提交于 2019-12-22 08:03:42
问题 I a new to Ipython. Currently i have installed Ipython using Anaconda and writing a code to plot chart using jupyter notebook UI. I want to pass few arguments to my working script with the help of argparse module. below is the code.. import argparse parser = argparse.ArgumentParser(description = 'Process display arguments') parser.add_argument('-t', "--test_name", help="Mandatory test name directory path", type=str) parser.add_argument('-s', "--symbolSet", nargs = '?', help="Optional

python argparser for multiple arguments for partial choices

老子叫甜甜 提交于 2019-12-22 06:46:20
问题 I create a argparser like this: parser = argparse.ArgumentParser(description='someDesc') parser.add_argument(-a,required=true,choices=[x,y,z]) parser.add_argument( ... ) However, only for choice "x" and not for choices "y,z", I want to have an additional REQUIRED argument. For eg. python test -a x // not fine...needs additional MANDATORY argument b python test -a y // fine...will run python test -a z // fine...will run python test -a x -b "ccc" // fine...will run How can I accomplish that

Python argparse parse_args into global namespace (or a reason this is a bad idea)

霸气de小男生 提交于 2019-12-22 04:26:07
问题 I've mostly used argparse for making command-line scripts in python, and the idiom I generally use is that I assign the arguments as attributes of an object, then parse them individually to a variable that matches their attribute name. This seems a little repetitive. Is there a way to assign them all into the global namespace and cut out the assignment step; or as is often the case when some python behavior seems counter-intuitive to me, can some wise, python expert point out that there a

Python argparse regex expression

眉间皱痕 提交于 2019-12-22 03:59:42
问题 Is it possible to use a regex expression for parsing an argument? For example, I want to accept an argument if only it is a 32 length hex (i.e. matches /[a-f0-9A-F]{32}/ ) I tried p.add_argument('hex', type=str, nargs="[a-f0-9A-F]{32}") without success 回答1: This is what the type kwarg is used for: it can take any callable that takes a single string argument and returns the converted value. import argparse import re from uuid import uuid4 def my_regex_type(arg_value, pat=re.compile(r"^[a-f0-9A