argparse

ArgumentParser epilog and description formatting in conjunction with ArgumentDefaultsHelpFormatter

泪湿孤枕 提交于 2019-11-28 22:20:43
问题 I'm using argparse to take in command line input and also to produce help text. I want to use ArgumentDefaultsHelpFormatter as the formatter_class , however this prevents me from also using RawDescriptionHelpFormatter which would allow me to add custom formatting to my description or epilog. Is there a sensible method of achieving this aside from writing code to produce text for default values myself? According to the argparse docs, all internals of ArgumentParser are considered

python argparse - optional append argument with choices

喜欢而已 提交于 2019-11-28 21:22:31
I have a script where I ask the user for a list of pre-defined actions to perform. I also want the ability to assume a particular list of actions when the user doesn't define anything. however, it seems like trying to do both of these together is impossible. when the user gives no arguments, they receive an error that the default choice is invalid acts = ['clear','copy','dump','lock'] p = argparse.ArgumentParser() p.add_argument('action', nargs='*', action='append', choices=acts, default=[['dump', 'clear']]) args = p.parse_args([]) >>> usage: [-h] [{clear,copy,dump,lock} [{clear,copy,dump,lock

Using 'argparse.ArgumentError' in Python

一曲冷凌霜 提交于 2019-11-28 20:51:05
问题 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. 回答1: From the source documentation: ArgumentError: The exception raised by ArgumentParser objects when there are errors with the parser's actions.

argparse subcommands with nested namespaces

末鹿安然 提交于 2019-11-28 20:49:18
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. Example: import argparse # Main parser main_parser = argparse.ArgumentParser() main_parser.add_argument("

How does argparse (and the deprecated optparse) respond to 'tab' keypress after python program name, in bash?

霸气de小男生 提交于 2019-11-28 20:48:41
I have tested optcomplete working with the optparse module. Its example is a simple file so I could get that working. I also tested it using the argparse module as the prior one is deprecated. But I really do not understand how and by whom the python program gets called on tab presses. I suspect bash together with the shebang line and the argparse (or optparse ) module are involved in some way. I have been trying to figure this out (now gonna read the source code). I have a little more complex program structure, which includes a wrapper around the piece of code which handles the arguments. Its

Using the same option multiple times in Python's argparse

痴心易碎 提交于 2019-11-28 20:02:37
I'm trying to write a script that accepts multiple input sources and does something to each one. Something like this ./my_script.py \ -i input1_url input1_name input1_other_var \ -i input2_url input2_name input2_other_var \ -i input3_url input3_name # notice inputX_other_var is optional But I can't quite figure out how to do this using argparse . It seems that it's set up so that each option flag can only be used once. I know how to associate multiple arguments with a single option ( nargs='*' or nargs='+' ), but that still won't let me use the -i flag multiple times. How do I go about

Setting options from environment variables when using argparse

不羁岁月 提交于 2019-11-28 18:38:33
I have a script which has certain options that can either be passed on the command line, or from environment variables. The CLI should take precedence if both are present, and an error occur if neither are set. I could check that the option is assigned after parsing, but I prefer to let argparse to do the heavy lifting and be responsible for displaying the usage statement if parsing fails. I have come up with a couple of alternative approaches to this (which I will post below as answers so they can be discussed separately) but they feel pretty kludgey to me and I think that I am missing

I want Python argparse to throw an exception rather than usage

梦想与她 提交于 2019-11-28 18:36:38
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. You can subclass ArgumentParser and override the error method to do something different when an error occurs: class ArgumentParserError(Exception): pass class ThrowingArgumentParser(argparse

Having options in argparse with a dash

こ雲淡風輕ζ 提交于 2019-11-28 18:01:57
问题 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? 回答1: 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 -

Argparse: how to handle variable number of arguments (nargs='*')

烂漫一生 提交于 2019-11-28 17:24:54
I thought that nargs='*' was enough to handle a variable number of arguments. Apparently it's not, and I don't understand the cause of this error. The code: p = argparse.ArgumentParser() p.add_argument('pos') p.add_argument('foo') p.add_argument('--spam', default=24, type=int, dest='spam') p.add_argument('vars', nargs='*') p.parse_args('1 2 --spam 8 8 9'.split()) I think the resulting namespace should be Namespace(pos='1', foo='2', spam='8', vars=['8', '9']) . Instead, argparse gives this error: usage: prog.py [-h] [--spam SPAM] pos foo [vars [vars ...]] error: unrecognized arguments: 9 8