argparse

Fully customized Python Help Usage

ε祈祈猫儿з 提交于 2019-12-11 02:08:25
问题 I'm trying to create a fully customized "help" usage with Python (which I plan to import into many programs that I want to have style consistency) but am having some trouble. I don't know why my description is ignoring newlines... tried "" and '', I can't get the ":" to occur after a /newline for the "...ARGS" lines, and obviously they look weird sitting on their own lines, and I have no idea how to add the newline at the end. Help please?? Here's a sample of what I am getting right now:

Parse a nested list with python argparse

混江龙づ霸主 提交于 2019-12-10 22:51:14
问题 Assume that I am expecting a list of lists where the inner lists have different types and lengths, e. g., [[1, 2], ["foo", "bar"], [3.14, "baz", 20]] how can I parse the above list using argparse? Most useful questions on stackoverflow: Similar questions exist, where most useful one is here. But they are not good enough in my case as they ignore the fact that the list is nested with different data types and lenghts. 回答1: expanding on my comment: from argparse import ArgumentParser import json

Why does argparse only work if the python interpreter is called explicitly?

戏子无情 提交于 2019-12-10 21:44:51
问题 I have the python interpreter location in my path environment variable so that I don't need to explicitly call the python interpreter from the command line. However, when I use the argparse module to read command line arguments, it only works if I explicitly call the python interpreter. import argparse parser = argparse.ArgumentParser() parser.add_argument('w') cmd_args = parser.parse_args() print(cmd_args.w) when I don't explicitly call the interpreter, this occurs: C:\Users\nheme\Desktop>

Python argparse skip if not int

拈花ヽ惹草 提交于 2019-12-10 21:40:52
问题 I would like to have 3 optional positional arguments ( int , int , then str ). What I want: $ ./args.py 0 100 vid start=0 end=100 video='vid' $ ./args.py 0 100 start=0 end=100 video=None $ ./args.py 0 start=0 end=None video=None $ ./args.py vid start=None end=None video='vid' $ ./args.py start=None end=None video=None What I have tried: #!/usr/bin/python3 import argparse parser = argparse.ArgumentParser() parser.add_argument('start', type=int, nargs='?') parser.add_argument('end', type=int,

argparse: How to separate unknown(and optional) args when subparsers are present.(subparsers are also optional)

守給你的承諾、 提交于 2019-12-10 19:59:22
问题 I have the following code parser = argparse.ArgumentParser(allow_abbrev=False, add_help=False) parser.add_argument('--conf', nargs=1) parser.add_argument('-h', '--help', nargs='?', const='generic') parser.add_argument('-v', '--version', action="store_true") subparsers = parser.add_subparsers() subparsers.required = False parser_start = subparsers.add_parser('start') group1 = parser_start.add_mutually_exclusive_group() group1.add_argument('--quiet', action="store_true") group1.add_argument('-V

Python: After raising argparse.ArgumentError, argparse raises generic error

给你一囗甜甜゛ 提交于 2019-12-10 19:01:05
问题 I defined a custom regex type for an argument that needs to follow an exact format. I used code from another post (regex custom type) that has been super useful. My problem is that I'm writing unit tests where I expect the regex to fail and trying to assert that the argparse.ArgumentError is raised ( assertRaises(argparse.ArgumentError, parser.parse_args(inargs.split())) ). The problem is that argparse appears to be catching the ArgumentError and throwing a generic error, preventing me from

Python argparse with — as the value

烂漫一生 提交于 2019-12-10 17:25:58
问题 Is there a way to pass -- as a value to a Python program using argparse without using the equals (=) sign? The command line arguments that I added to the argparser are defined like below: parser.add_argument('--myarg', help="my arg description") You would use this argument in a program like this: python myprogram.py --myarg value123 Is there a way to run this program with -- as the value instead of 'value123'? i.e python myprogram.py --myarg -- 回答1: I suspect it will not be possible to make

How to split a string like the shell in python?

别等时光非礼了梦想. 提交于 2019-12-10 16:52:28
问题 I have command line arguments in a string and I need to split it to feed to argparse.ArgumentParser.parse_args . I see that the documentation uses string.split() plentifully. However in complex cases, this does not work, such as --foo "spaces in brakets" --bar escaped\ spaces Is there a functionality to do that in python? ( A similar question for java was asked here ). 回答1: This is what shlex.split was created for. 回答2: If you're parsing a windows-style command line, then shlex.split doesn't

how to show argparse subcommands in groups?

感情迁移 提交于 2019-12-10 15:44:49
问题 For a program with many subcommands, I'd like to show them logically grouped in the --help output. Python argparse has a add_argument_group method, but it doesn't seem compatible with subparsers. subparsers can only be added to the top level ArgumentParser , and _SubParsersAction doesn't allow argument groups. Is there some way around this? 回答1: You can't really do it in any straightforward way, other than implementing a custom HelpFormatter . You can find some more information on

How do I check for a particular subparser?

好久不见. 提交于 2019-12-10 15:33:02
问题 How do I check for a particular subparser? import argparse if __name__ == "__main__": mainparser = argparse.ArgumentParser() submainadder = mainparser.add_subparsers(title='subcommands') parser_ut = submainadder.add_parser('unittest') stuff = mainparser.parse_args() # if 'unittest' was selected: # do_things() 回答1: Maybe something like this ? import argparse def do_things(args): print args # Do your stuff mainparser = argparse.ArgumentParser() submainadder = mainparser.add_subparsers(title=