argparse

Multiple invocation of the same subcommand in a single command line

老子叫甜甜 提交于 2019-12-18 08:54:07
问题 I'm trying to figure out how to use argparser to do the following: $ python test.py executeBuild --name foobar1 executeBuild --name foobar2 .... getBuild itself is a sub-command. My goal is to have the script have the capability to chain a series of sub-command ( executeBuild being one of them) and execute them in order. In the example above, it would execute a build, then setup the environment, then execute build again. How can I accomplish this with argparse? I've tried the following: main

Correct way to get allowed arguments from ArgumentParser

倾然丶 夕夏残阳落幕 提交于 2019-12-18 05:57:08
问题 Question: What is the intended / official way of accessing possible arguments from an existing argparse.ArgumentParser object? Example: Let's assume the following context: import argparse parser = argparse.ArgumentParser() parser.add_argument('--foo', '-f', type=str) Here I'd like to get the following list of allowed arguments: ['-h', '--foo', '--help', '-f'] I found the following workaround which does the trick for me parser._option_string_actions.keys() But I'm not happy with it, as it

Python argparse optional sub-arguments

我的未来我决定 提交于 2019-12-18 04:42:25
问题 I'd like to have an argument to my program that has some required parameters along with some optional parameters. Something like this: [--print text [color [size]] so you could pass it any of these: mycommand --print hello mycommand --print hello blue mycommand --print hello red 12 There could be multiple of these so it has to be a single add_argument. For example: [--print text [color]] [--output filename [overwrite]] I can achieve arguments that are close to what I want: >>> parser =

Don't parse options after the last positional argument

一笑奈何 提交于 2019-12-18 04:34:24
问题 I'm writing a wrapper around the ssh command line client. After the first positional argument that's part of command , all further options should also be treated as positional arguments. Under optparse , I believe this would be done with disable_interspersed_args. Presently I have something like this: parser = argparse.ArgumentParser() parser.add_argument('--parallel', default=False, action='store_true') # maybe allow no command? this would ssh interactively into each machine... parser.add

argparse module not working in Python

眉间皱痕 提交于 2019-12-18 03:50:16
问题 I'm trying to get the argparse module working in Python. My problem is that on a fresh install, I get the following: File "test.py", line 3, in <module> import argparse File "/home/jon/Pythons/realmine/argparse.py", line 3, in <module> parser = argparse.ArgumentParser(description='Short sample app') AttributeError: 'module' object has no attribute 'ArgumentParser' test.py is: import argparse Clearly, I'm missing something. Can anyone help? 回答1: Usually this symptom is the result of shadowing

argparse: flatten the result of action='append'

只谈情不闲聊 提交于 2019-12-18 03:34:05
问题 I'd like to make a script that supports an argument list of the form ./myscript --env ONE=1,TWO=2 --env THREE=3 Here's my attempt: import argparse parser = argparse.ArgumentParser() parser.add_argument( '--env', type=lambda s: s.split(','), action='append', ) options = parser.parse_args() print options.env $ ./myscript --env ONE=1,TWO=2 --env THREE=3 [['ONE=1', 'TWO=2'], ['THREE=3']] Sure I can fix this in postprocessing: options.env = [x for y in options.env for x in y] but I'm wondering if

argparse: flatten the result of action='append'

风格不统一 提交于 2019-12-18 03:34:04
问题 I'd like to make a script that supports an argument list of the form ./myscript --env ONE=1,TWO=2 --env THREE=3 Here's my attempt: import argparse parser = argparse.ArgumentParser() parser.add_argument( '--env', type=lambda s: s.split(','), action='append', ) options = parser.parse_args() print options.env $ ./myscript --env ONE=1,TWO=2 --env THREE=3 [['ONE=1', 'TWO=2'], ['THREE=3']] Sure I can fix this in postprocessing: options.env = [x for y in options.env for x in y] but I'm wondering if

argparse subcommands with nested namespaces

旧街凉风 提交于 2019-12-17 23:08:07
问题 Does argparse provide built-in facilities for having it parse groups or parsers into their own namespaces? I feel like I must be missing an option somewhere. Edit : This example is probably not exactly what I should be doing to structure the parser to meet my goal, but it was what I worked out so far. My specific goal is to be able to give subparsers groups of options that are parsed into namespace fields. The idea I had with parent was simply to use common options for this same purpose.

I want Python argparse to throw an exception rather than usage

[亡魂溺海] 提交于 2019-12-17 22:36:34
问题 I don't think this is possible, but I want to handle exceptions from argparse myself. For example: import argparse parser = argparse.ArgumentParser() parser.add_argument('--foo', help='foo help', required=True) try: args = parser.parse_args() except: do_something() When I run it: $ myapp.py usage: myapp --foo foo myapp: error: argument --foo is required But I want it to fall into the exception instead. 回答1: You can subclass ArgumentParser and override the error method to do something

argparse - disable same argument occurrences

跟風遠走 提交于 2019-12-17 20:36:06
问题 I'm trying to disable same argument occurences within one command line, using argparse ./python3 --argument1=something --argument2 --argument1=something_else which means this should raise an error, because value of argument1 is overriden, by default, argparse just overrides the value and continues like nothing happened... Is there any smart way how to disable this behaviour? 回答1: I don't think there is a native way to do it using argparse , but fortunately, argparse offers methods to report