argparse

argparse: identify which subparser was used [duplicate]

馋奶兔 提交于 2019-11-28 03:02:53
This question already has an answer here: Get selected subcommand with argparse 2 answers 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.add_argument('appname', action='store', help='name of app to process') How can I identify, which subparser was used? calling:

Argparse: Way to include default values in '--help'?

别来无恙 提交于 2019-11-28 02:45:00
Suppose I have the following argparse snippet: diags.cmdln_parser.add_argument( '--scan-time', action = 'store', nargs = '?', type = int, default = 5, help = "Wait SCAN-TIME seconds between status checks.") Currently, --help returns: usage: connection_check.py [-h] [--version] [--scan-time [SCAN_TIME]] Test the reliability/uptime of a connection. optional arguments: -h, --help show this help message and exit --version show program's version number and exit --scan-time [SCAN_TIME] Wait SCAN-TIME seconds between status checks. I would prefer something like: --scan-time [SCAN_TIME] Wait SCAN-TIME

Use argparse to run 1 of 2 functions in my script

走远了吗. 提交于 2019-11-28 02:21:14
问题 I currently have 2 functions in my .py script. #1 connects to the database and does some processing. #2 does some other processing on files Currently before I run the script, I have to manually comment/uncomment the function I want to run in my main if statement block. How can I use argparse, so it asks me which function to run when I run my script? 回答1: It is possible to tell ArgumentParser objects about the function or object that has your desired behavior directly , by means of action=

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

╄→гoц情女王★ 提交于 2019-11-27 23:55:41
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 line for line in csv.reader(open(args.infile)): My code: import csv import pprint pp = pprint

argparse choices structure of allowed values

北城余情 提交于 2019-11-27 23:52:50
问题 Using argparse in relation to Python dependencies between groups using argparse, I have an argument part of some parser group of a parser - for example: group_simulate.add_argument('-P', help='simulate FC port down', nargs=1, metavar='fc_port_name', dest='simulate') How it's possible to use the choices to limit the choices to a list of parameters of the next structure: 1:m:"number between 1 and 10":p:"number between 1 and 4" I have tried to use the range option but I couldn't find a way to

argparse - Combining parent parser, subparsers and default values

与世无争的帅哥 提交于 2019-11-27 23:41:06
问题 I wanted to define different subparsers in a script, with both inheriting options from a common parent, but with different defaults. It doesn't work as expected, though. Here's what I did: import argparse # this is the top level parser parser = argparse.ArgumentParser(description='bla bla') # this serves as a parent parser base_parser = argparse.ArgumentParser(add_help=False) base_parser.add_argument('-n', help='number', type=int) # subparsers subparsers = parser.add_subparsers() subparser1=

Python argparse : mutually exclusive arguments with optional and positional argument

…衆ロ難τιáo~ 提交于 2019-11-27 19:33:17
问题 I would like to get this with argparse library : PROG --yesterday | begin-date [end-date] I tried to combine mutual exclusion and argument groups but I didn't succeed. This program should only accept that : PROG --yesterday PROG 2015-11-12 PROG 2015-11-12 2015-11-15 Is it possible to do this with argparse ? Thanks hpaulj . See the final result : import argparse from datetime import datetime import pytz def argument_date(str_date): try: return datetime.strptime(str_date, "%Y-%m-%d").replace

Call function based on argparse

半世苍凉 提交于 2019-11-27 19:21:47
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 also have a def showtop20(): ..... and def listapps(): .... How can I call the function (and only this

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

自古美人都是妖i 提交于 2019-11-27 19:16:51
This question already has an answer here: type=dict in argparse.add_argument() 7 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 dictionary to the script ./argp.py -m "{'key1': 'value1'}" usage: argp.py [-h] [-m MYDICT] argp.py:

Python argparse: Lots of choices results in ugly help output

天涯浪子 提交于 2019-11-27 19:15:29
I have this code which I am generally pleased with: import argparse servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer", "SovServer" ] parser = argparse.ArgumentParser(description="A program to update components on servers.") group = parser.add_mutually_exclusive_group() group.add_argument('-l', '--list', dest="update", action='store_false', default=False, help='list server components') group.add_argument('-u', '--updatepom', dest="update", action='store_true', help='update server components')