argparse

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

跟風遠走 提交于 2019-11-28 10:01:12
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') cmd.add_argument('arg') cmd.set_defaults(do=action) args = ap.parse_args() args.do(args) The output from

How to show help for all subparsers in argparse?

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:03:03
问题 I have made a Python script that is doing a lot of actions, so it has many options, so I divided it to subparsers that also use parent parsers for common options grouping. I want a help option that will show the help for all commands with their options, is it possible without overriding the format_help method? I saw a similar question, but the grouping is not critical for me, I just want the options there. For example: general_group = argparse.ArgumentParser(formatter_class=argparse

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

廉价感情. 提交于 2019-11-28 09:01:16
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_argument('-d') Then only ./app.py -a | ./app.py -b | ./app.py -c | ./app.py -d can be called. Is it

Sphinx and argparse - autodocumenting command line scripts?

…衆ロ難τιáo~ 提交于 2019-11-28 08:57:45
I'm building a Python package, and using Sphinx to create the docs. Aside from my package code, I also include a lot of command line Python scripts, which use argparse. I was wondering if there is a way to get Sphinx to autodocument these scripts? The end goal would be a pretty-printed list of scripts, with the associated help print, arguments and options. And to be clear, I'm looking for a pre-existing way to do this, not a way to implement this myself. This isn't as specific of a question as I usually ask on S.O., if there is a more appropriate S.E. site to post this question, please let me

How to handle ampersand as part a command line argument in python

这一生的挚爱 提交于 2019-11-28 08:26:08
问题 I have a program in python 2.7 which accepts command line parameters using argparse , however if I try to enter a string containing an ampersand I lose the characters after that ampersand. For example: I have a simple python program just to test the input of command line arguments and simply prints out what was entered for a single command line parameter. Essentially: print args.where When I run the program with an argument like this: $ python args.py -a http://www.website.com?optionone=one

How can I use python's argparse with a predefined argument string?

浪子不回头ぞ 提交于 2019-11-28 08:25:10
I want to use the pythons argparse module to parse my cli parameter string. This works for the parameters a pass from terminal, but not with a given string. import argparse parser = argparse.ArgumentParser(description='Argparse Test script') parser.add_argument("param", help='some parameter') argString = 'someTestFile' print(argString) args = parser.parse_args(argString) If I run this script I get this output: ~/someTestFile usage: argparsetest.py [-h] param argparsetest.py: error: unrecognized arguments: o m e T e s t F i l e The ~/someTestFile is somehow transformed in o m e T e s t F i l e

How to iterate over arguments

落爺英雄遲暮 提交于 2019-11-28 07:58:28
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 arguments and their value? Please add 'vars' if you wanna iterate over namespace object: for arg in vars(args):

Creating hidden arguments with Python argparse

蹲街弑〆低调 提交于 2019-11-28 07:54:05
Is it possible to add an Argument to an python argparse.ArgumentParser without it showing up in the usage or help ( script.py --help )? 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 来源: https://stackoverflow.com/questions/11114589/creating-hidden-arguments-with-python-argparse

argparse optional subparser (for --version)

穿精又带淫゛_ 提交于 2019-11-28 07:32:08
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('db', parents=[parser_shared]) ... args = parser.parse_args() Now I would like to be able to call this

Python Argparse conditionally required arguments

别等时光非礼了梦想. 提交于 2019-11-28 07:19:42
I have done as much research as possible but I haven't found the best way to make certain cmdline arguments necessary only under certain conditions, in this case only if other arguments have been given. Here's what I want to do at a very basic level: p = argparse.ArgumentParser(description='...') p.add_argument('--argument', required=False) p.add_argument('-a', required=False) # only required if --argument is given p.add_argument('-b', required=False) # only required if --argument is given From what I have seen, other people seem to just add their own check at the end: if args.argument and