argparse

Using the argparse output to call functions

谁说胖子不能爱 提交于 2019-12-04 17:47:37
问题 Currently my code looks like this. It allows me to parse multiple parameters my program script gets. Is there a different way that is closer to 'best practices'? I haven't seen code actually using the output of argparse , only how to set it up. def useArguments(): x = 0 while x <= 5: if x == 0: if args.getweather != None: getWeather(args.getweather) if x == 1: if args.post != None: post(args.post) if x == 2: if args.custompost != None: custompost(args.custompost) if x == 3: if args.list !=

Python: argparse optional arguments without dashes

馋奶兔 提交于 2019-12-04 16:56:22
问题 I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2 FILE2 using parser.add_argument('--file1',type=file) parser.add_argument('--file2',type=file) however, if I remove the dashes, argparse starts to interprete it as a positional rather than optional argument... In other words, is it possible to specifically tell argparse

argparse option of options

时光怂恿深爱的人放手 提交于 2019-12-04 16:05:50
I am trying to add option of options in argparse . Currently I have: group = parser.add_mutually_exclusive_group() group.add_argument("--md", help="Create xyz file for each ionic step for" " visualization", action='store_true') group.add_argument("--force", help="See which atom has maximum force", action='store_true') group.add_argument("--opt", help="grep string from file", nargs=2, metavar=("str", "file")) parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)" " visualization", action='store_true') parser.add_argument("-N", help="Showing first N line", metavar='integer',

grouping an unknown number of arguments with argparse

ε祈祈猫儿з 提交于 2019-12-04 15:45:14
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 I lose all of the power of the argparse module for handling requirements, checking if files exist, etc.

How can I set a subparser to be optional in argparse?

≯℡__Kan透↙ 提交于 2019-12-04 15:39:58
import argparse parser_sub = subparsers.add_parser('files') parser_sub.add_argument( '--file-name', action='store', dest='filename', nargs='*') options = parser.parse_args() Output: error: too few arguments. As per this link: https://bugs.python.org/issue9253 it states that subparsers cant be optional. Can this behaviour be changed? I would like my subcommands to be optional. How can I achieve this through argparse in python 2.6? There's not much that can be added to that bug/issue https://bugs.python.org/issue9253 . subparsers is a special kind of positional argument. Normally the only way to

Allowing specific values for an Argparse argument [duplicate]

北慕城南 提交于 2019-12-04 14:54:32
问题 This question already has an answer here : Restricting values of command line options (1 answer) Closed 2 years ago . Is it possible to require that an argparse argument be one of a few preset values? My current approach is would be to examine the argument manually and if it's not one of the allowed values call print_help() and exit. Here's the current implementation: ... parser.add_argument('--val', help='Special testing value') args = parser.parse_args(sys.argv[1:]) if args.val not in ['a',

One optional argument which does not require positional arguments

本小妞迷上赌 提交于 2019-12-04 13:43:57
问题 I have a question regarding python's argparse: Is it possible to have a optional argument, which does not require positional arguments? Example: parser.add_argument('lat', help="latitude") parser.add_argument('lon', help="longitude") parser.add_argument('--method', help="calculation method (default: add)", default="add") parser.add_argument('--list-methods', help="list available methods", action="store_true") The normal command line would be test.py 47.249 -33.282 or test.py 47.249 -33.282 -

argparse and unittest python

杀马特。学长 韩版系。学妹 提交于 2019-12-04 11:46:42
I am using argparse to handle command line arguments. Code was working fine. However as soon as I am adding unittest.main() in the main, it is not working. I am getting: I am here option -i not recognized Usage: testing.py [options] [test] [...] Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output -f, --failfast Stop on first failure -c, --catch Catch control-C and display results -b, --buffer Buffer stdout and stderr during test runs Examples: testing.py - run default set of tests testing.py MyTestSuite - run suite 'MyTestSuite' testing.py MyTestCase

Specifying default filenames with argparse, but not opening them on --help?

喜欢而已 提交于 2019-12-04 10:28:51
问题 Let's say I have a script that does some work on a file. It takes this file's name on the command line, but if it's not provided, it defaults to a known filename ( content.txt , say). With python's argparse , I use the following: parser = argparse.ArgumentParser(description='my illustrative example') parser.add_argument('--content', metavar='file', default='content.txt', type=argparse.FileType('r'), help='file to process (defaults to content.txt)') args = parser.parse_args() # do some work on

Pip doesn't install latest available version from pypi (argparse in this case)

无人久伴 提交于 2019-12-04 10:07:41
问题 The problem I worked on some python projects lately and had lots of problems with pip not installing the latest versions of some requirements. I am on osx and and I used brew to install Python 2.7.6 . In the project I'm working on, we simply pip install -r requirements.txt . In the current case, I needed to install argparse==1.2.1 . This is the actual latest version shown on the pypi website Here's my output Downloading/unpacking argparse==1.2.1 (from -r requirements.txt (line 4)) Could not