argparse subparser problem

Sven nomail at thisaddress.com
Thu Nov 18 08:02:06 EST 2010


On 2010-11-16 14:35:01 -0500, Neal Becker said:

> I want to have subparsers, but I also want to be able to say:
> 
> myprogram --version
> and get the version #

Would it not be possible to print the version number and then exit the 
program before building the subparser?

> ---------------------------
> 
> import argparse
> 
> def stop():
>     pass
> 
> parser = argparse.ArgumentParser()
> parser.add_argument ('--version', action='store_true')

# if --version print version
# sys.exit(0)

> subparsers = parser.add_subparsers()
> 
> parser_stop = subparsers.add_parser ('stop')
> parser_stop.add_argument ('stop', action='store_true')
> parser_stop.set_defaults (func=stop)
> 
> 
> opt = parser.parse_args (['--version'])
> ------------------------------


-- 
./Sven




More information about the Python-list mailing list