[Python-Dev] PEP 389: argparse - new command line parsing module (original) (raw)

Steven Bethard steven.bethard at gmail.com
Sat Oct 3 17:49:18 CEST 2009


On Sat, Oct 3, 2009 at 8:17 AM, Michael Foord <fuzzyman at voidspace.org.uk> wrote:

Steven Bethard wrote:

On Sat, Oct 3, 2009 at 3:45 AM, Yuvgoog Greenle <ubershmekel at gmail.com> wrote:

I haven't checked if it's possible, but I suggest Argparse have it's own exception class that inherits from SystemExit and that exception would be thrown.

I've never seen such an idiom before (subclassing SystemExit) but it would certainly be possible create an ArgumentParserExit exception like that. Then you would have your choice of overriding .exit() or catching the exception. Why not just catch SystemExit? If you want a custom exception the overriding .exit() should be sufficient.

I'm certainly fine with that -- I'm just trying to make sure I've addressed whatever needs addressed to get argparse in.

I'd be much more interested in Guido's suggestion of auto-generated custom help messages for sub-commands.

Maybe I misunderstood, but I think this is already the default argparse behavior, no?

import argparse parser = argparse.ArgumentParser() parser.add_argument('--foo') subparsers = parser.add_subparsers() parser1 = subparsers.add_parser('1') parser1.add_argument('--bar') parser2 = subparsers.add_parser('2') parser2.add_argument('baz') parser.parse_args(['--help'])

import argparse parser = argparse.ArgumentParser() parser.addargument('--foo') subparsers = parser.addsubparsers() parser1 = subparsers.addparser('1') parser1.addargument('--bar') parser2 = subparsers.addparser('2') parser2.addargument('baz')

# top level argument help parser.parseargs(['--help']) usage: [-h] [--foo FOO] {1,2} ...

positional arguments: {1,2}

optional arguments: -h, --help show this help message and exit --foo FOO

# help for subparser 1 parser.parseargs(['1', '--help']) usage: 1 [-h] [--bar BAR]

optional arguments: -h, --help show this help message and exit --bar BAR

# help for subparser 2 parser.parseargs(['2', '--help']) usage: 2 [-h] baz

positional arguments: baz

optional arguments: -h, --help show this help message and exit

Steve

Where did you get that preposterous hypothesis? Did Steve tell you that? --- The Hiphopopotamus



More information about the Python-Dev mailing list