argparse

How to Set a Default Subparser using Argparse Module with Python 2.7

巧了我就是萌 提交于 2019-12-29 07:19:12
问题 I'm using Python 2.7 and I'm trying to accomplish a shell like behavior using argparse. My issue, in general, that I cannot seem to find a way, in Python 2.7, to use argparse's subparsers as optional. It's kind of hard to explain my issue so I'll describe what I require from my program. The program has 2 modes of work: Starting the program with a given command (each command has it's own additional arguments) and additional arguments will run a specific task. Starting the program without a

Allow positional command-line arguments with nargs to be seperated by a flag

烈酒焚心 提交于 2019-12-29 01:34:47
问题 I have a program using argparse. It takes 1 required positional argument, 1 optional positional argument, and 1 flag argument. Something like: usage: test.py [-h] [-a A] b [c] So, I tried using this: parser = argparse.ArgumentParser() parser.add_argument('-a') parser.add_argument('b') parser.add_argument('c', nargs='?', default=None) print(parser.parse_args()) Which works fine for test.py B C -a A and test.py -a A B C . But when I do test.py B -a A C , it throws an error: $ python3 test.py B

python argparse help message, disable metavar for short options?

戏子无情 提交于 2019-12-28 04:21:10
问题 I want to construct a argparser help message that looks like: -i, --input=INPUT help for input -o, --output=output help for output My current code: arg_parser = argparse.ArgumentParser arg_parser.add_argument('-i', '--input', dest='input', metavar='=INPUT', help='help for input') arg_parser.add_argument('-o', '--output', dest='output', metavar='=OUTPUT', help='help for output') arg_parser.print_help() is giving me -i =INPUT, --input =INPUT help for input -o =INPUT, --output =output help for

Python argparse - Add argument to multiple subparsers

我的未来我决定 提交于 2019-12-27 19:17:27
问题 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

argparse in python3.2.3 on windows 7 does not seem to parse

拜拜、爱过 提交于 2019-12-25 12:52:13
问题 since I got python on windows running, here is the next problem I encountered with argparse, and for which I did not see a solution. I uses optparse before. Here is my code: import argparse parser = argparse.ArgumentParser( description = 'Test description') # main description for help parser.add_argument('-d', '--dir', # -u or --user option dest = "dir", help = 'directory to start with') args = parser.parse_args() print(args.dir) but when I run this code with either code.py -d test code.py -

Setting default option in Python of two mutually exclusive options using the argparse module

限于喜欢 提交于 2019-12-25 08:16:12
问题 import argparse parser = argparse.ArgumentParser(description="List or update! That is the question!") group = parser.add_mutually_exclusive_group() group.add_argument('-l', '--list', dest="update", action='store_false') group.add_argument('-u', '--update', dest="update", action='store_true') args = parser.parse_args() print args If the user does not specify any optional arguments I want update=False . [Edit]: I changed my question to not be so general, it was confusing. Sorry. 回答1: You should

Python argparse - disable help for subcommands?

拥有回忆 提交于 2019-12-25 07:25:49
问题 I'm using argparse on Python 3.5.1. I don't want the default help commands, so I disabled it using the add_help=False argument to the ArgumentParser constructor. However, while the help commands for the application are removed, they still exist for the subcommands. How can I remove the help for the subcommands/subparsers? 回答1: The subparser is created in: class _SubParsersAction(Action): .... def add_parser(self, name, **kwargs): ... # create the parser and add it to the map parser = self.

Use either one flag argument or two positional arguments with argparse

拥有回忆 提交于 2019-12-25 06:44:39
问题 i'm stuck on a task, which require either 2 positional args or 1 (a file) if flag enabled: parser.add_argument('pos_arg1', help='desc') parser.add_argument('pos_arg2', help='desc') parser.add_argument('--add_arg1', help='desc', type=argparse.FileType('r'), nargs='?') so using the script with either script.py arg1 arg2 or script.py --add_arg1 file are both valid. How would I do this? 回答1: If you define: In [422]: parser=argparse.ArgumentParser() In [423]: g=parser.add_mutually_exclusive_group(

Pass an optional list to argparse

时间秒杀一切 提交于 2019-12-25 05:26:11
问题 Is there a more elegant way to pass an optional list of integers to argparse than to pass a delimited string and parse it later? I also have a positional argument. parser.add_argument('--ids', type=int, nargs='+') parser.add_argument('cmd') doesn't work, because argparse attempts to grab cmd and complains that it isn't an integer. Ideally, I'd like to execute with one of program.py --ids 6,32,12 refresh program.py --ids 6 32 12 refresh or something similar, but also be able to program.py

How to get available flags out of an ArgumentParser object?

可紊 提交于 2019-12-25 05:05:14
问题 I'm using the argparse module for this python project. I'm looking to get the available flags out of an ArgumentParser object before calling parse_args() . Anyone have any ideas? 回答1: Got this from the source code of add_argument(): >>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-v', '--verbosity', help='more debug info', action='store_true') _StoreTrueAction(option_strings=['-v', '--verbosity'], dest='verbosity', nargs=0, const=True, default=False, type