def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)]
for subparsers_action in subparsers_actions:
for choice, subparser in subparsers_action.choices.items():
print("\n{}:\n{}".format(choice.upper(), "-" * (len(choice) + 1)))
print(subparser.format_help())
parser.exit()
python类_SubParsersAction()的实例源码
def __call__(self, parser, namespace, values, option_string=None):
parser.print_help()
subparsers_actions = [
action for action in parser._actions
if isinstance(action, _SubParsersAction)]
for subparsers_action in subparsers_actions:
for choice, subparser in subparsers_action.choices.items():
print("Command '{}'".format(choice))
print(subparser.format_usage())
parser.exit()
def __init__(self):
"""Constructor."""
# Extend sys.path so that conda.cli module can be imported, then import
# conda's CLI modules.
self.conda_sp_dpath = self._get_conda_sp_dpath()
self.prefix_dpath = os.path.join(
os.path.split(os.path.split(os.path.split(
self.conda_sp_dpath
)[0])[0])[0],
'envs',
)
(self._base_mod,
self._main_mod,
self._main_install_mod,
self._main_create_mod) = self._import_conda_modules()
self._create_parser, self._install_parser = None, None
parser, sub_parsers = self._main_mod.generate_parser()
self._main_install_mod.configure_parser(sub_parsers)
self._main_create_mod.configure_parser(sub_parsers)
subparsers_action = None
for action in parser._subparsers._actions:
if isinstance(action, argparse._SubParsersAction):
subparsers_action = action
break
action_parser_map = subparsers_action._name_parser_map
if 'install' in action_parser_map:
self._install_parser = action_parser_map['install']
# These arguments are somehow dropped from the Namespace
self._install_parser.add_argument('--no-default-packages',
default=False)
self._install_parser.add_argument('--clone', default=False)
if 'create' in action_parser_map:
self._create_parser = action_parser_map['create']
# Additional branches may be added here to support more of conda's
# subparsers
def __call__(self, parser, namespace, values, option_string=None):
"""Custom call method."""
parser.print_help()
print("\n")
subparsers_actions = [
action for action in parser._actions
if isinstance(action, argparse._SubParsersAction)]
for subparsers_action in subparsers_actions:
for choice, subparser in subparsers_action.choices.items():
print("Subcommand: '{}'".format(choice))
print(subparser.format_help())
def get_input_object(self, action, prefix):
input_parameters = {}
input_parameters['class'] = self.get_class(prefix)
input_parameters['name'] = self.get_id(action, prefix)
input_parameters['id'] = self.get_id(action, prefix)
input_type = web.form.Textbox
if self.get_choices(action):
input_type = web.form.Dropdown
input_parameters['args'] = [choice for choice in action.choices]
if self.get_multiple(action):
input_parameters['multiple'] = 'multiple'
input_parameters['size'] = 4
elif isinstance(action, (argparse._StoreTrueAction, argparse._StoreFalseAction, argparse._StoreConstAction)):
input_type = web.form.Checkbox
input_parameters['checked'] = True if action.default else False
input_parameters['value'] = action.const
else:
input_parameters['value'] = action.default if action.default else ""
if isinstance(action, argparse._SubParsersAction):
input_parameters['onChange'] = "javascript: update_show(this);"
input_parameters['value'] = action.choices.keys()[0]
if len(action.option_strings):
input_parameters['default'] = action.default
# if optional argument may be present with either 1 or no parameters, the default shifts
# to being the no parameter's value. this is mearly to properly display actual values to the user
if action.nargs == '?':
input_parameters['value'] = action.const
# TODO: support these actions: append, append_const, count
self._actions[self.get_id(action, prefix)] = action
input_object = input_type(**input_parameters)
input_object.description = self.get_description(action)
input_object.nargs = self.get_nargs(action)
input_object.help = self.get_help(action)
input_object.disposition = self.get_disposition(action)
input_object.subparser = self.get_subparser(action)
input_object.choices = self.get_choices(action)
return input_object