argparse

Argparse with required subparser

故事扮演 提交于 2019-11-27 01:06:35
I'm using Python 3.4, I'm trying to use argparse with subparsers, and I want to have a similar behavior to the one in Python 2.x where if I don't supply a positional argument (to indicate the subparser/subprogram) I'll get a helpful error message. I.e., with python2 I'll get the following error message: $ python2 subparser_test.py usage: subparser_test.py [-h] {foo} ... subparser_test.py: error: too few arguments I'm setting the required attribute as suggested in https://stackoverflow.com/a/22994500/3061818 , however that gives me an error with Python 3.4.0: TypeError: sequence item 0:

Python argparse and bash completion

拜拜、爱过 提交于 2019-11-26 23:56:36
问题 I would like to get auto-completion on my python scripts also in the arguments. I had never really understood how the bash_completion worked (for arguments), but after I digged in I understood that: it uses "complete" to bind a completing function to a command every completing function basically is a copy of the argument parser The second point in particular is not great, because I would like to have it automatically generated. The best thing would be that the shell asks to my program at

argparse: identify which subparser was used [duplicate]

馋奶兔 提交于 2019-11-26 23:55:17
问题 This question already has answers here : Get selected subcommand with argparse (2 answers) Closed 6 years ago . I think this must be easy but I do not get it. Assume I have the following arparse parser: import argparse parser = argparse.ArgumentParser( version='pyargparsetest 1.0' ) subparsers = parser.add_subparsers(help='commands') # all all_parser = subparsers.add_parser('all', help='process all apps') # app app_parser = subparsers.add_parser('app', help='process a single app') app_parser

Python argparse: default value or specified value

白昼怎懂夜的黑 提交于 2019-11-26 21:36:51
I would like to have a optional argument that will default to a value if only the flag is present with no value specified, but store a user-specified value instead of the default if the user specifies a value. Is there already an action available for this? An example: python script.py --example # args.example would equal a default value of 1 python script.py --example 2 # args.example would equal a default value of 2 I can create an action, but wanted to see if there was an existing way to do this. import argparse parser = argparse.ArgumentParser() parser.add_argument('--example', nargs='?',

TypeError: coercing to Unicode: need string or buffer, list found

两盒软妹~` 提交于 2019-11-26 21:35:42
问题 I'm trying to get a data parsing script up and running. It works as far as the data manipulation is concerned. What I'm trying to do is set this up so I can enter multiple user defined CSV's with a single command. e.g. > python script.py One.csv Two.csv Three.csv If you have any advice on how to automate the naming of the output CSV so that if input = test.csv , output = test1.csv , I'd appreciate that as well. Getting TypeError: coercing to Unicode: need string or buffer, list found for the

Argparse: Required argument 'y' if 'x' is present

穿精又带淫゛_ 提交于 2019-11-26 19:51:51
I have a requirement as follows: ./xyifier --prox --lport lport --rport rport for the argument prox , I use action='store_true' to check if it is present or not. I do not require any of the arguments. But, if --prox is set I require rport and lport as well. Is there an easy way of doing this with argparse without writing custom conditional coding. More Code: non_int.add_argument('--prox', action='store_true', help='Flag to turn on proxy') non_int.add_argument('--lport', type=int, help='Listen Port.') non_int.add_argument('--rport', type=int, help='Proxy port.') No, there isn't any option in

Accepting a dictionary as an argument with argparse and python [duplicate]

被刻印的时光 ゝ 提交于 2019-11-26 19:49:09
问题 This question already has an answer here: type=dict in argparse.add_argument() 8 answers I'm trying to accept an argument of type=dict with argparse but no matter the input it gives an error of invalid dict value. #!/usr/bin/env python import argparse MYDICT = {'key': 'value'} parser = argparse.ArgumentParser() parser.add_argument("-m", "--mydict", action="store", required=False, type=dict, default=MYDICT) args = parser.parse_args() print args.mydict This is what happens when I try and pass a

Call function based on argparse

风流意气都作罢 提交于 2019-11-26 19:43:23
问题 I'm new to python and currently playing with it. I have a script which does some API Calls to an appliance. I would like to extend the functionality and call different functions based on the arguments given when calling the script. Currently I have the following: parser = argparse.ArgumentParser() parser.add_argument("--showtop20", help="list top 20 by app", action="store_true") parser.add_argument("--listapps", help="list all available apps", action="store_true") args = parser.parse_args() I

Python argparse: Is there a way to specify a range in nargs?

萝らか妹 提交于 2019-11-26 19:24:08
问题 I have an optional argument that supports a list of arguments itself. I mean, it should support: -f 1 2 -f 1 2 3 but not: -f 1 -f 1 2 3 4 Is there a way to force this within argparse ? Now I'm using nargs="*", and then checking the list length. Edit: As requested, what I needed is being able to define a range of acceptable number of arguments. I mean, saying (in the example) 2 or 3 args is right, but not 1 or 4 or anything that's not inside the range 2..3 回答1: You could do this with a custom

In Python, using argparse, allow only positive integers

扶醉桌前 提交于 2019-11-26 18:59:59
问题 The title pretty much summarizes what I'd like to have happen. Here is what I have, and while the program doesn't blow up on a nonpositive integer, I want the user to be informed that a nonpositive integer is basically nonsense. import argparse parser = argparse.ArgumentParser() parser.add_argument("-g", "--games", type=int, default=162, help="The number of games to simulate") args = parser.parse_args() And the output: python simulate_many.py -g 20 Setting up... Playing games... .............