argparse

mutually_exclusive_group with optional and positional argument

Deadly 提交于 2019-12-02 04:24:12
问题 I created an cli specification with docopt which works great, however for some reason I have to rewrite it to argparse Usage: update_store_products <store_name>... update_store_products --all Options: -a --all Updates all stores configured in config How to do that? What is important I don't want to have something like this: update_store_products [--all] <store_name>... I think it would be rather something like this: update_store_products (--all | <store_name>...) I tried to use add_mutually

Argparse - differentiate between no options, option invoked, and option invoked with argument?

送分小仙女□ 提交于 2019-12-02 03:41:23
问题 As an example: #thing.py import argparse parser = argparse.ArgumentParser() parser.add_argument("--show", nargs='?', action="store") args = parser.parse_args() How do I differentiate between the following usages: python thing.py python thing.py --show python thing.py --show all Essentially, I want to do different things if: The user specifies no options The user specifies the "--show" option by itself The user specifies "--show all" - with a string / argument. Using default="foo" in add

How do you get argparse to choose a default subparser?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 02:02:34
I have the following code in script.py : import argparse parser = argparse.ArgumentParser() sp = parser.add_subparsers(dest='command') sp.default = 'a' a_parser = sp.add_parser('a') b_parser = sp.add_parser('b') a_parser.add_argument('--thing', default='thing') b_parser.add_argument('--nothing', default='nothing') args = parser.parse_args() print(args) I can call this three different ways: $ python3 script.py Namespace(command='a') $ python3 script.py a Namespace(command='a', thing='thing') $ python3 script.py b Namespace(command='b', nothing='nothing') There's only one problem with this: what

How do you get argparse to choose a default subparser?

旧时模样 提交于 2019-12-02 01:55:33
问题 I have the following code in script.py : import argparse parser = argparse.ArgumentParser() sp = parser.add_subparsers(dest='command') sp.default = 'a' a_parser = sp.add_parser('a') b_parser = sp.add_parser('b') a_parser.add_argument('--thing', default='thing') b_parser.add_argument('--nothing', default='nothing') args = parser.parse_args() print(args) I can call this three different ways: $ python3 script.py Namespace(command='a') $ python3 script.py a Namespace(command='a', thing='thing') $

Python argparse: Force a list item to be unique

北慕城南 提交于 2019-12-02 01:11:32
问题 Being able to validate the list items using choices=servers below is nice. servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer", "SkeppServer", "HavsServer", "SovServer" ] parser = argparse.ArgumentParser() parser.add_argument('-o', '--only', nargs='*', choices=servers, help='Space separated list of case sensitive server names to process') Is it possible to force an item in the list to be unique, so that no duplicates are allowed? 回答1: The way to properly discard

Execute function via arg

前提是你 提交于 2019-12-02 00:58:34
问题 What I would like to do is when I enter a specific argument it starts a function, is this possible through argparse . So if I hit the add argument in my application it triggers the "add" function. parser = argparse.ArgumentParser(description='to do list') parser.add_argument('-a', '--add', help='add an item to the todo list') parser.add_argument('-r', '--remove',) parser.add_argument('-l', '--list',) args = parser.parse_args() def add(args): conn = sqlite3.connect('todo.db') c = conn.cursor()

argparse on demand imports for types, choices etc

安稳与你 提交于 2019-12-01 23:12:01
问题 I have quite a big program which has a CLI interaction based on argparse , with several sub parsers. The list of supported choices for the subparsers arguments are determined based on DB queries, parsing different xml files, making different calculations etc, so it is quite IO intensive and time consuming. The problem is that argparse seems to fetch choices for all sub parser when I run the script, which adds a considerable and annoying startup delay. Is there a way to make argparse only

Execute function via arg

 ̄綄美尐妖づ 提交于 2019-12-01 22:57:17
What I would like to do is when I enter a specific argument it starts a function, is this possible through argparse . So if I hit the add argument in my application it triggers the "add" function. parser = argparse.ArgumentParser(description='to do list') parser.add_argument('-a', '--add', help='add an item to the todo list') parser.add_argument('-r', '--remove',) parser.add_argument('-l', '--list',) args = parser.parse_args() def add(args): conn = sqlite3.connect('todo.db') c = conn.cursor() c.execute("INSERT INTO todo VALUES (args.add, timestamp)") Sure, you can just use add as the type

Is there a way to create argument in python's argparse that returns true in case no values given

六月ゝ 毕业季﹏ 提交于 2019-12-01 21:51:20
问题 Currently --resize flag that I created is boolean, and means that all my objects will be resized: parser.add_argument("--resize", action="store_true", help="Do dictionary resize") # ... # if resize flag is true I'm re-sizing all objects if args.resize: for object in my_obects: object.do_resize() Is there a way implement argparse argument that if passed as boolean flag ( --resize ) will return true, but if passed with value ( --resize 10 ), will contain value. Example: python ./my_script.py -

Python's argparse choices constrained printing

大兔子大兔子 提交于 2019-12-01 21:27:43
Currently I want Python's argparse module to only print out '1 - 65535' rather than {1, 2, 3, ... 65535}, but the documentation doesn't seem to provide any method for this. Any suggestions? You can alter the way defaults are formatted by setting the formatter_class option . I'd subclass the HelpFormatter class to alter the way it formats your choices values. This class is officially an "implementation detail" but I doubt it'll change much with newer python versions. The _metavar_formatter method formats the {1, 2, ..., 65535} string and your subclass could override that: class