View on GitHub


1 hr
Test Coverage
The contents of this module are shamelessly stolen from ruamel's std.argparse
Because std.argparse is MIT licensed, I'm not including licensing and copyright
information inline here -- it's available in the project's repo.

We want to do parsing with argparse, but we also want a behavior that argparse
doesn't have: defaulting to 'deploy' if no subcommand is present.

Some slight modifications have been made:
 - I've made this a function that takes a parser argument, rather than a class
   method for parsers.
 - There is no support for an 'args' kwarg
 - Simpler check for '--help' argument
 - Only slices sys.argv once, rather than once per arg (just feels cleaner to

import sys
import argparse

def set_default_subparser(parser, name):
    Default subparser selection. Call after setup, just before parse_args()
    subparser_found = False

    opts = sys.argv[1:]

    # global help takes precedence
    if '-h' in opts or '--help' in opts:

    for x in parser._subparsers._actions:
        if not isinstance(x, argparse._SubParsersAction):
        for sp_name in x._name_parser_map.keys():
            if sp_name in opts:
                subparser_found = True
    if not subparser_found:
        # insert default in first position, this implies no
        # global options without a sub_parsers specified
        sys.argv.insert(1, name)