I would like to write a Python script called sync
that has three or four modes of operation, each receiving a different number of arguments. For example,
sync set_version <build> <version_number>
sync get_version <build>
sync has_started <build_1> <build_2> ... <build_n>
I've tried using argparse
's subparsers for each mode of operation:
import argparse
parser = argparse.ArgumentParser(description='Build synchronization mechanism')
subparsers = parser.add_subparsers()
parser_get_version = subparsers.add_parser('get_version')
parser_get_version.add_argument('build')
parser_update_version = subparsers.add_parser('update_version')
parser_update_version.add_argument('build')
parser_update_version.add_argument('version')
args = parser.parse_args()
print args
The problem is that the help message is not reflecting the structure of the arguments of each operation modes. Instead, it simply lists the operation modes:
usage: sync.py [-h] {get_version,update_version} ...
Build synchronization mechanism
positional arguments:
{get_version,update_version}
optional arguments:
-h, --help show this help message and exit
How do I force argparse to display a full help message, with all the subparsers parameters?