argparse subparser problem

Neal Becker ndbecker2 at
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():

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

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

More information about the Python-list mailing list