Last active
December 13, 2015 18:09
-
-
Save cutwater/4953481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
class _SubParsersAction(argparse._SubParsersAction): | |
def __init__(self, *args, **kwargs): | |
super(_SubParsersAction, self).__init__(*args, **kwargs) | |
self._other_actions = [] | |
def add_parser(self, name, **kwargs): | |
if 'help' not in kwargs: | |
choice_action = self._ChoicesPseudoAction(name, '') | |
self._choices_actions.append(choice_action) | |
return super(_SubParsersAction, self).add_parser(name, **kwargs) | |
def _get_subactions(self): | |
return sorted(self._choices_actions, key=lambda x: x.dest) | |
class CommandFormatter(argparse.HelpFormatter): | |
def _format_args(self, action, default_metavar): | |
if isinstance(action, argparse._SubParsersAction): | |
return '{command}' | |
else: | |
return super(CommandFormatter, self)._format_args(action, default_metavar) | |
def _metavar_formatter(self, action, default_metavar): | |
# noinspection PyUnusedLocal | |
def format_subparser(tuple_size): | |
return '', | |
if isinstance(action, argparse._SubParsersAction): | |
return format_subparser | |
else: | |
return super(CommandFormatter, self)._metavar_formatter(action, default_metavar) | |
class ArgumentParser(argparse.ArgumentParser): | |
""" | |
Strict argument parser. | |
Disabled quessing names. | |
""" | |
def __init__(self, **kwargs): | |
kwargs['formatter_class'] = CommandFormatter | |
super(ArgumentParser, self).__init__(**kwargs) | |
self.register('action', 'parsers', _SubParsersAction) | |
def _get_option_tuples(self, option_string): | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment