argparse

Handle spaces in argparse input

孤者浪人 提交于 2019-11-30 05:58:52
Using python and argparse, the user could input a file name with -d as the flag. parser.add_argument("-d", "--dmp", default=None) However, this failed when the path included spaces. E.g. -d C:\SMTHNG\Name with spaces\MORE\file.csv NOTE: the spaces would cause an error (flag only takes in 'C:SMTHNG\Name' as input). error: unrecognized arguments: with spaces\MORE\file.csv Took me longer than it should have to find the solution to this problem... (did not find a Q&A for it so I'm making my own post) For those who can't parse arguments and still get "error: unrecognized arguments:" I found a

Python: Typehints for argparse.Namespace objects

陌路散爱 提交于 2019-11-30 05:52:20
Is there a way to have Python static analyzers (e.g. in PyCharm, other IDEs) pick up on Typehints on argparse.Namespace objects? Example: parser = argparse.ArgumentParser() parser.add_argument('--somearg') parsed = parser.parse_args(['--somearg','someval']) # type: argparse.Namespace the_arg = parsed.somearg # <- Pycharm complains that parsed object has no attribute 'somearg' If I remove the type declaration in the inline comment, PyCharm doesn't complain, but it also doesn't pick up on invalid attributes. For example: parser = argparse.ArgumentParser() parser.add_argument('--somearg') parsed

python argparse - either both optional arguments or else neither one

自作多情 提交于 2019-11-30 04:49:56
I have a program that uses a default name and password. I'm using argparse to allow the user to specify command line options, and I would like to enable the user to provide the program with a different name and password to use. So I have the following: parser.add_argument( '-n', '--name', help='the login name that you wish the program to use' ) parser.add_argument( '-p', '--password', help='the password to log in with.' ) But it doesn't make any sense to specify only the name or only the password, but it would make sense to specify neither one. I noticed that argparse does have the ability to

Python argparse : How can I get Namespace objects for argument groups separately?

孤者浪人 提交于 2019-11-30 03:23:57
问题 I have some command line arguments categorized in groups as follows: cmdParser = argparse.ArgumentParser() cmdParser.add_argument('mainArg') groupOne = cmdParser.add_argument_group('group one') groupOne.add_argument('-optA') groupOne.add_argument('-optB') groupTwo = cmdParser.add_argument_group('group two') groupTwo.add_argument('-optC') groupTwo.add_argument('-optD') How can I parse the above, such that I end up with three different Namespace objects? global_args - containing all the

Using 'argparse.ArgumentError' in Python

核能气质少年 提交于 2019-11-30 03:07:46
I'd like to use the ArgumentError exception in the argparse module in Python, but I can't figure out how to use it. The signature says that it should be called as ArgumentError(argument, message) , but I can't figure out what argument should be. I think it should be some part of the parser object, but I couldn't find any documentation for it. From the source documentation : ArgumentError: The exception raised by ArgumentParser objects when there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages.

Multiple files for one argument in argparse Python 2.7

我的未来我决定 提交于 2019-11-30 03:06:12
Trying to make an argument in argparse where one can input several file names that can be read. In this example, i'm just trying to print each of the file objects to make sure it's working correctly but I get the error: error: unrecognized arguments: f2.txt f3.txt . How can I get it to recognize all of them? my command in the terminal to run a program and read multiple files python program.py f1.txt f2.txt f3.txt Python script import argparse def main(): parser = argparse.ArgumentParser() parser.add_argument('file', nargs='?', type=file) args = parser.parse_args() for f in args.file: print f

directory path types with argparse

孤者浪人 提交于 2019-11-30 02:37:41
My python script needs to read files from a directory passed on the command line. I have defined a readable_dir type as below to be used with argparse for validating that the directory passed on the command line is existent and readable. Additionally, a default value (/tmp/non_existent_dir in the example below) has also been specified for the directory argument. The problem here is that argparse invokes readable_dir() on the default value even in a situation where a directory argument is explicitly passed in on the command line. This causes the script to crap out as the default path /tmp/non

Check if argparse optional argument is set or not

偶尔善良 提交于 2019-11-30 02:32:40
I would like to check whether an optional argparse argument has been set by the user or not. Can I safely check using isset? Something like this: if(isset(args.myArg)): #do something else: #do something else Does this work the same for float / int / string type arguments? I could set a default parameter and check it (e.g., set myArg = -1, or "" for a string, or "NOT_SET"). However, the value I ultimately want to use is only calculated later in the script. So I would be setting it to -1 as a default, and then updating it to something else later. This seems a little clumsy in comparison with

Python argparse positional arguments and sub-commands

混江龙づ霸主 提交于 2019-11-30 02:23:50
问题 I'm working with argparse and am trying to mix sub-commands and positional arguments, and the following issue came up. This code runs fine: import argparse parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() parser.add_argument('positional') subparsers.add_parser('subpositional') parser.parse_args('subpositional positional'.split()) The above code parses the args into Namespace(positional='positional') , however when I change the positional argument to have nargs='?' as

python, argparse: enable input parameter when another one has been specified

坚强是说给别人听的谎言 提交于 2019-11-30 01:49:11
问题 In my python script, I want to be able to use an optional input parameter only when another optional parameter has been specified. Example: $ python myScript.py --parameter1 value1 $ python myScript.py --parameter1 value1 --parameter2 value2 But NOT: $ python myScript.py --parameter2 value2 How do I do this with argparse? Thanks! 回答1: Use a custom action: import argparse foo_default=None class BarAction(argparse.Action): def __call__(self,parser,namespace,values,option_string=None): didfoo