argparse

Move “help” to a different Argument Group in python argparse

*爱你&永不变心* 提交于 2019-11-29 11:30:18
问题 Currently I'm creating a directory reader program using Python. I'm using 'argparse' to parse the arguments from command line. I have the following code: parser = argparse.ArgumentParser(prog = "LS.py", usage = "%(prog)s [options] [path1 [path2 [...pathN]]]\nThe paths are optional; if not given . is used.") group = parser.add_argument_group("Options") group.add_argument("-path", default = ".", help = argparse.SUPPRESS, metavar = "") group.add_argument("-m", "--modified", default = False, help

Argparse unit tests: Suppress the help message

五迷三道 提交于 2019-11-29 11:14:06
I'm writing test cases for argparse implementation. I intend to test '-h' feature. The following code does it. But it also outputs the usage for the script. Is there a way to suppress that? self.assertRaises(SystemExit, arg_parse_obj.parse_known_args, ['-h']) Also, can we check for the exception number thrown? For example '-h' throws SystemExit: 0 , while invalid or insufficient args throw SystemExit: 2 . Is there a way to check the numeric code? When testing for exception codes, use self.assertRaises() as a context manager ; this gives you access to the raised exception, letting you test the

Create variable key/value pairs with argparse (python)

五迷三道 提交于 2019-11-29 10:47:54
I'm using argparse module to set my command line options. I'm also using a dict as a config in my application. Simple key/value store. What I'm looking for is a possibility to override JSON options using command line arguments, without defining all possible arguments in advance. Something like --conf-key-1 value1 --conf-key-2 value2 , which would create a dict {'key_1': 'value1','key_2': 'value2'} ('-' in the argument is replaced by '_' in the dict). Then I can combine this dict with my JSON config (dict). So basically I would like to define --conf-* as an argument, where * can be any key and

python argparse set behaviour when no arguments provided

老子叫甜甜 提交于 2019-11-29 09:37:36
问题 I'm fairly new to python and I'm stuck on how to structure my simple script when using command line arguments. The purpose of the script is to automate some daily tasks in my job relating to sorting and manipulating images. I can specify the arguments and get them to call the relevant functions, but i also want to set a default action when no arguments are supplied. Here's my current structure. parser = argparse.ArgumentParser() parser.add_argument("-l", "--list", help="Create CSV of images",

Disable abbreviation in argparse

我只是一个虾纸丫 提交于 2019-11-29 09:25:11
argparse uses per default abbreviation in unambiguous cases. I don't want abbreviation and I'd like to disable it. But didn't find it in the documentation . Is it possible? Example: import argparse parser = argparse.ArgumentParser() parser.add_argument('--send', action='store_true') parser.parse_args(['--se']) # returns Namespace(send=True) But I want it only to be true when the full parameter is supplied. To prevent user errors. UPDATE: I created a ticket at python bugtracker after Vikas answer. And it already has been processed. As of Python 3.5.0 you can disable abbreviations by initiating

How to Set a Default Subparser using Argparse Module with Python 2.7

安稳与你 提交于 2019-11-29 08:55:45
I'm using Python 2.7 and I'm trying to accomplish a shell like behavior using argparse. My issue, in general, that I cannot seem to find a way, in Python 2.7, to use argparse's subparsers as optional. It's kind of hard to explain my issue so I'll describe what I require from my program. The program has 2 modes of work: Starting the program with a given command (each command has it's own additional arguments) and additional arguments will run a specific task. Starting the program without a command will start a shell-like program that can take a line of arguments and process them as if the

Arguments that are dependent on other arguments with Argparse

梦想的初衷 提交于 2019-11-29 07:34:20
问题 I want to accomplish something like this: -LoadFiles -SourceFile "" -DestPath "" -SourceFolder "" -DestPath "" -GenericOperation -SpecificOperation -Arga "" -Argb "" -OtherOperation -Argc "" -Argb "" -Argc "" A user should be able to run things like: -LoadFiles -SourceFile "somePath" -DestPath "somePath" or -LoadFiles -SourceFolder "somePath" -DestPath "somePath" Basically, if you have -LoadFiles, you are required to have either -SourceFile or -SourceFolder after. If you have -SourceFile, you

how to get argparse to read arguments from a file with an option rather than prefix

扶醉桌前 提交于 2019-11-29 06:48:53
问题 I would like to know how to use python's argparse module to read arguments both from the command line and possibly from text files. I know of argparse's fromfile_prefix_chars but that's not exactly what I want. I want the behavior, but I don't want the syntax. I want an interface that looks like this: $ python myprogram.py --foo 1 -A somefile.txt --bar 2 When argparse sees -A, it should stop reading from sys.argv or whatever I give it, and call a function I write that will read somefile.text

argparse choices structure of allowed values

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 06:32:07
Using argparse in relation to Python dependencies between groups using argparse , I have an argument part of some parser group of a parser - for example: group_simulate.add_argument('-P', help='simulate FC port down', nargs=1, metavar='fc_port_name', dest='simulate') How it's possible to use the choices to limit the choices to a list of parameters of the next structure: 1:m:"number between 1 and 10":p:"number between 1 and 4" I have tried to use the range option but I couldn't find a way to create a list of choices that are acceptable examples: legal parameters: test.py -P 1:m:4:p:2 not legal