argparse

python 3 argparse call a function

南楼画角 提交于 2019-12-13 01:25:27
问题 I wanted to create a commandline-like / shell-like interface in python3. Argparse seems to do the job of parsing and displaying the help/error messages. According to the python3 documentation of argparse, there is a func= argument that can be used to get your function called by argparse . # sub-command functions def foo(args): print(args.x * args.y) def bar(args): print('((%s))' % args.z) # create the top-level parser parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() #

Pythons argparse default value doesn't work

微笑、不失礼 提交于 2019-12-12 21:08:21
问题 I'm using python 2.7.13. My goal is to have three possible arguments, with default values being set if no arguments are given by the user: parser.add_argument("-r", nargs=3, default=(0, 1000, 50), type=int, help="Useful help text") This doesn't work for me, and I can't find anywhere if it is possible to use default in such a way as above. When running it as program.py -r I get a an error: expected 3 argument(s) But I also tried removing nargs completely and only having one default value:

python argparse default value for optional argument

99封情书 提交于 2019-12-12 15:29:49
问题 usage: [-h] [--foo FOO] bar How do I make sure that default value of FOO is abc if I execute my script like below ./myscript.py --foo bar --> bar is positional argument here. but args.foo is considering bar as argument of '--foo' . I want args.foo to be abc and bar to be the positional argument 回答1: You can't, not without reworking your arguments. You should use two switches here; one to switch on the behaviour, and one to override the default value picked. You can make the second switch

argparse - Build back command line

做~自己de王妃 提交于 2019-12-12 10:33:47
问题 In Python, how can I parse the command line, edit the resulting parsed arguments object and generate a valid command line back with the updated values? For instance, I would like python cmd.py --foo=bar --step=0 call python cmd.py --foo=bar --step=1 with all the original --foo=bar arguments, potentially without extra arguments added when default value is used. Is it possible with argparse ? 回答1: You can use argparse to parse the command-line arguments, and then modify those as desired. At the

Argparse: defaults from file

走远了吗. 提交于 2019-12-12 10:15:04
问题 I have a Python script which takes a lot of arguments. I currently use a configuration.ini file (read using configparser ), but would like to allow the user to override specific arguments using command line. If I'd only have had two arguments I'd have used something like: if not arg1: arg1 = config[section]['arg1'] But I don't want to do that for 30 arguments. Any easy way to take optional arguments from cmd line, and default to the config file? 回答1: Try the following, using dict.update():

Using argparse to convert csv to xml in python

妖精的绣舞 提交于 2019-12-12 09:59:20
问题 A really quick quesiton, basically my program takes an inputfile which is a csv file and then converts it into a xml file. However, the name of the xml file can either be set by the user input or if the user doesn't specify the name, then the xml file will have the same name as the csv file except with a .xml extension. I need help with the latter. So far my program works when the output file is given a name but i don't know how to set the xml file name when the user doesn't input a name. I

Neatly pass positional arguments as args and optional arguments as kwargs from argparse to a function

吃可爱长大的小学妹 提交于 2019-12-12 09:50:27
问题 I would like to write a Python script that takes some necessary positional and some optional command-line arguments via argparse : Let's call the positional args a , b , c , and the optional arguments x , y , z . In my Python script, I would like to pass these args on to a function; specifically, I want a , b , c to be passed as *args , and x , y , z to be passed as **kwargs , the latter retaining their names. I would like to do this many times with different functions and different numbers

grouping an unknown number of arguments with argparse

僤鯓⒐⒋嵵緔 提交于 2019-12-12 09:06:10
问题 I am designing the user interface for a command line program that needs to be able to accept one or more groups of options. Each group is the same, but needs to be linked together, like so: ./myprogram.py --group1 name1,name2,pathA,pathB --group2 name3,name4,pathC,pathD This seems very clunky for a user to deal with. I have considered using a INI -style configparser setup, but it is also clunky, because I have a lot of other arguments besides this that generally have default values, and then

Using mutually exclusive between groups

本秂侑毒 提交于 2019-12-12 08:27:40
问题 I'm trying to have a mutually exclusive group between different groups: I have the arguments -a,-b,-c, and I want to have a conflict with -a and -b together, or -a and -c together. The help should show something like [-a | ([-b] [-c])]. The following code does not seem to do have mutually exclusive options: import argparse parser = argparse.ArgumentParser(description='My desc') main_group = parser.add_mutually_exclusive_group() mysub_group = main_group.add_argument_group() main_group.add

Most pythonic way of accepting arguments using optparse

孤街醉人 提交于 2019-12-12 08:15:08
问题 I currently have a python file that utilizes sys.argv[1] to accept a string at the command line. It then performs operations on that string and then returns the modified string to the command line. I would like to implement a batch mode option in which I can provide a file of strings (one per line, fwiw) and have it return to the command line so that I can redirect the output doing something like $ python script.py -someflag file.txt > modified.txt while still retaining the current