def build_subcommands_parser(parser, module):
mdefs = module.__dict__<EOL>keys = list(mdefs.keys())<EOL>keys.sort()<EOL>subparsers = parser.add_subparsers(help='<STR_LIT>')<EOL>for command in keys:<EOL><INDENT>if command.startswith('<STR_LIT>'):<EOL><INDENT>func = module.__dict__[command]<EOL>parser = subparsers.add_parser(command[<NUM_LIT:4>:], help=func.__doc__)<EOL>parser.set_defaults(func=func)<EOL>argspec = inspect.getargspec(func)<EOL>flags = len(argspec.args)<EOL>if argspec.defaults:<EOL><INDENT>flags = len(argspec.args) - len(argspec.defaults)<EOL><DEDENT>if flags >= <NUM_LIT:1>:<EOL><INDENT>for arg in argspec.args[:flags - <NUM_LIT:1>]:<EOL><INDENT>parser.add_argument(arg)<EOL><DEDENT>parser.add_argument(argspec.args[flags - <NUM_LIT:1>], nargs='<STR_LIT:*>')<EOL><DEDENT>short_opts = set([])<EOL>for idx, arg in enumerate(argspec.args[flags:]):<EOL><INDENT>arg = arg.replace('<STR_LIT:_>', '<STR_LIT:->')<EOL>short_opt = arg[<NUM_LIT:0>]<EOL>if not (arg.startswith('<STR_LIT>') or (short_opt in short_opts)):<EOL><INDENT>opts = ['<STR_LIT>' % short_opt, '<STR_LIT>' % arg]<EOL><DEDENT>else:<EOL><INDENT>opts = ['<STR_LIT>' % arg]<EOL><DEDENT>short_opts |= set([short_opt])<EOL>if isinstance(argspec.defaults[idx], list):<EOL><INDENT>parser.add_argument(*opts, action='<STR_LIT>')<EOL><DEDENT>elif isinstance(argspec.defaults[idx], dict):<EOL><INDENT>parser.add_argument(*opts, type=json.loads)<EOL><DEDENT>elif argspec.defaults[idx] is False:<EOL><INDENT>parser.add_argument(*opts, action='<STR_LIT:store_true>')<EOL><DEDENT>elif argspec.defaults[idx] is not None:<EOL><INDENT>parser.add_argument(*opts, default=argspec.defaults[idx])<EOL><DEDENT>else:<EOL><INDENT>parser.add_argument(*opts)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
Returns a parser for the subcommands defined in the *module*
(i.e. commands starting with a 'pub_' prefix).