argparse

Why does this argparse code behave differently between Python 2 and 3?

Deadly 提交于 2019-11-27 03:17:09
问题 The following code, using argparse's subparsers, fails on Python 3 but runs as expected in Python 2. After comparing the docs, I still can't tell why. #!/usr/bin/env python from __future__ import print_function from argparse import ArgumentParser def action(args): print(args) if __name__ == '__main__': std = ArgumentParser(add_help=False) std.add_argument('standard') ap = ArgumentParser() sp = ap.add_subparsers() cmd = sp.add_parser('subcommand', parents=[std], description='Do subcommand')

How to parse multiple nested sub-commands using python argparse?

Deadly 提交于 2019-11-27 03:02:44
I am implementing a command line program which has interface like this: cmd [GLOBAL_OPTIONS] {command [COMMAND_OPTS]} [{command [COMMAND_OPTS]} ...] I have gone through the argparse documentation . I can implement GLOBAL_OPTIONS as optional argument using add_argument in argparse . And the {command [COMMAND_OPTS]} using Sub-commands . From the documentation it seems I can have only one sub-command. But as you can see I have to implement one or more sub-commands. What is the best way to parse such command line arguments useing argparse ? Vikas @mgilson has a nice answer to this question. But

How to parse positional arguments with leading minus sign (negative numbers) using argparse

非 Y 不嫁゛ 提交于 2019-11-27 02:42:22
问题 I would like to parse a required, positional argument containing a comma-separated list of integers. If the first integer contains a leading minus ('-') sign, argparse complains: import argparse parser = argparse.ArgumentParser() parser.add_argument('positional') parser.add_argument('-t', '--test', action='store_true') opts = parser.parse_args() print opts $ python example.py --test 1,2,3,4 Namespace(positional='1,2,3,4', test=True) $ python example.py --test -1,2,3,4 usage: example.py [-h] [

Does argparse (python) support mutually exclusive groups of arguments?

混江龙づ霸主 提交于 2019-11-27 02:40:03
问题 If I have the arguments '-a', '-b', '-c', '-d' , with the add_mutually_exclusive_group() function my program will have to use just one of them. Is there a way to combine that, so that the program will accept only either '-a 999 -b 999' or '-c 999 -d 999' ? Edit: adding a simple program for more clarity: >>> parser = argparse.ArgumentParser() >>> group = parser.add_mutually_exclusive_group() >>> group.add_argument('-a') >>> group.add_argument('-b') >>> group.add_argument('-c') >>> group.add

How to iterate over arguments

对着背影说爱祢 提交于 2019-11-27 02:07:04
问题 I have such script: import argparse parser = argparse.ArgumentParser( description='Text file conversion.' ) parser.add_argument("inputfile", help="file to process", type=str) parser.add_argument("-o", "--out", default="output.txt", help="output name") parser.add_argument("-t", "--type", default="detailed", help="Type of processing") args = parser.parse_args() for arg in args: print(arg) But it doesnt work. I get error: TypeError: 'Namespace' object is not iterable How to iterate over

Python argparse custom actions with additional arguments passed

爱⌒轻易说出口 提交于 2019-11-27 01:50:45
问题 import argparse class customAction(argparse.Action): def __call__(self, parser, args, values, option_string=None): setattr(args, self.dest, values) parser = argparse.ArgumentParser() parser.add_argument('-e', '--example', action=customAction) I want to pass additional arguments to customAction when the option -e is triggered, e.g. a instance of another class. How can I do this? Everything I have tried has errored out. 回答1: def make_action(additional_arg): class customAction(argparse.Action):

argparse optional subparser (for --version)

二次信任 提交于 2019-11-27 01:50:36
问题 I have the following code (using Python 2.7): # shared command line options, like --version or --verbose parser_shared = argparse.ArgumentParser(add_help=False) parser_shared.add_argument('--version', action='store_true') # the main parser, inherits from `parser_shared` parser = argparse.ArgumentParser(description='main', parents=[parser_shared]) # several subcommands, which can't inherit from the main parser, since # it would expect subcommands ad infinitum subparsers = parser.add_subparsers

Is it possible to use argparse to capture an arbitrary set of optional arguments?

房东的猫 提交于 2019-11-27 01:49:17
问题 Is it possible to use argparse to capture an arbitrary set of optional arguments? For example both the following should be accepted as inputs: python script.py required_arg1 --var1 value1 --var2 value2 --var3 value3 python script.py required_arg1 --varA valueA --var2 value2 --varB valueB a priori I don't know what optional arguments would be specified receive but would handle them accordingly. 回答1: This is kind of a hackish way, but it works very well: Check, which arguments are not added and

How to use argparse subparsers correctly?

岁酱吖の 提交于 2019-11-27 01:44:08
问题 I've been searching through allot of the subparser examples on here and in general but can't seem to figure this seemingly simple thing out. I have two var types of which one has constraints so thought subparser was the way to go. e.g. -t allows for either "A" or "B". If the user passes "A" then they are further required to also specify if it is either "a1" or "a2". If they pass just "B" then nothing. Can I do this and have argparse return me what type of "A" was passed or if it was just "B"?

Creating hidden arguments with Python argparse

*爱你&永不变心* 提交于 2019-11-27 01:26:03
问题 Is it possible to add an Argument to an python argparse.ArgumentParser without it showing up in the usage or help ( script.py --help )? 回答1: Yes, you can set the help option to add_argument to argparse.SUPPRESS . Here's an example from the argparse documentation: >>> parser = argparse.ArgumentParser(prog='frobble') >>> parser.add_argument('--foo', help=argparse.SUPPRESS) >>> parser.print_help() usage: frobble [-h] optional arguments: -h, --help show this help message and exit 回答2: I do it by