argparse

Python argparse - Add argument to multiple subparsers

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My script defines one main parser and multiple subparsers. I want to apply the -p argument to some subparsers. So far the code looks like this: parser = argparse.ArgumentParser(prog="myProg") subparsers = parser.add_subparsers(title="actions") parser.add_argument("-v", "--verbose", action="store_true", dest="VERBOSE", help="run in verbose mode") parser_create = subparsers.add_parser ("create", help = "create the orbix environment") parser_create.add_argument ("-p", type = int, required = True, help = "set db parameter") # Update parser

One optional argument which does not require positional arguments

倖福魔咒の 提交于 2019-12-03 08:35:45
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 --method sub . But as soon as I call the script with test.py --list-methods to list all available methods

python argparse extra args

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: i would like to get extra args using argparse but without known what are they. for example, in maven we can add parameters in the form: -Dmaven.test.skip=true or -Dcmd=compiler:compile i would like to get the same thing in python using argparse , and get some kind of dict with all the args.. i know i can use: aparser.parse_known_args() but then i need to parse me extra args (remove the -D and split by = ). Was wondering if there is something out of the box? Thanks! 回答1: You can use parser.add_argument('-D', action='append', default=[]) which

Don't argparse read unicode from commandline?

做~自己de王妃 提交于 2019-12-03 08:19:56
问题 Running Python 2.7 When executing: $ python client.py get_emails -a "åäö" I get: usage: client.py get_emails [-h] [-a AREA] [-t {rfc2822,plain}] client.py get_emails: error: argument -a/--area: invalid unicode value: '\xc3\xa5\xc3\xa4\xc3\xb6' This is my parser: def _argparse(): desc = """ Simple CLI-client for... """ argparser = argparse.ArgumentParser(description=desc) subparsers = argparser.add_subparsers(dest='command') # create the parser for the "get_emails" command parser_get_emails =

How to make an optional value for argument using argparse?

 ̄綄美尐妖づ 提交于 2019-12-03 08:08:50
问题 I am creating a python script where I want to have an argument that manipulates how many search results you get as output. I've currently named the argument --head . This is the functionality I'd like it to have: When --head is not passed at the command line I'd like it to default to one value. In this case, a rather big one, like 80 When --head is passed without any value, I'd like it to default to another value. In this case, something limited, like 10 When --head is passed with a value, I

argparse option of options

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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("

how to add_argument_group to add_mutually_exclusive_group with python argparse

被刻印的时光 ゝ 提交于 2019-12-03 07:37:40
I am trying to implement the following: $ prog.py -h usage: prog.py [-h] [-s | -m] [[-y [year]] | [[-1 | -3] [month] [year]]] However, no matter how I played with add_argument_group and add_mutually_exclusive_group, #!/usr/bin/env python import argparse def main(opt): print(opt) if __name__ == '__main__': parser = argparse.ArgumentParser() bar = parser.add_mutually_exclusive_group() bar.add_argument('-s', action='store_true', default=True) bar.add_argument('-m', action='store_true', default=False) #bar = parser.add_argument_group() bar = parser.add_mutually_exclusive_group() bar.add_argument('

Customize argparse help message

戏子无情 提交于 2019-12-03 07:35:02
问题 I have written the following sample code to demonstrate my issue. import argparse parser = argparse.ArgumentParser() parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0') parser.parse_args() This produces the following help message. $ python foo.py --help usage: foo.py [-h] [-v] optional arguments: -h, --help show this help message and exit -v, --version show program's version number and exit I want to customize this help output such that it capitalizes all phrases

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

风流意气都作罢 提交于 2019-12-03 05:55:47
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 find a version that satisfies the requirement argparse==1.2.1 (from -r requirements.txt (line 4)) (from

How to code argparse combinational options in python

ぐ巨炮叔叔 提交于 2019-12-03 05:53:51
I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result. Requirement: test2.py [-c/-v] -f Usage or Rules: -c (compare) takes 2 parameter. -v (verify) takes 1 parameter. Either of these two must be present, but not both . -f is a mandatory parameter (output file name). Output: I am able to get the desired output as shown below kp@kp:~/Study/scripts$ ./test.py -c P1 P2 -f p kp@kp:~/Study/scripts$ ./test.py -v P1 -f p kp@kp:~/Study/scripts$ ./test.py -v P1 usage: test.py <functional argument> <ouput target argument>