argparse subparser problem

Neal Becker ndbecker2 at gmail.com
Tue Nov 16 20:35:01 CET 2010


I want to have subparsers, but I also want to be able to say:

myprogram --version
and get the version #

---------------------------

import argparse

def stop():
    pass

parser = argparse.ArgumentParser()
parser.add_argument ('--version', action='store_true')

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'])
------------------------------

 python test_p1.py
usage: test_p1.py [-h] [--version] {stop} ...
test_p1.py: error: too few arguments




More information about the Python-list mailing list