in lib/ramble/ramble/main.py [0:0]
def format_help_sections(self, level):
"""Format help on sections for a particular verbosity level.
Args:
level (str): 'short' or 'long' (more commands shown for long)
"""
if level not in levels:
raise ValueError("level must be one of: %s" % levels)
# lazily add all commands to the parser when needed.
add_all_commands(self)
"""Print help on subcommands in neatly formatted sections."""
formatter = self._get_formatter()
# Create a list of subcommand actions. Argparse internals are nasty!
# Note: you can only call _get_subactions() once. Even nastier!
if not hasattr(self, "actions"):
self.actions = self._subparsers._actions[-1]._get_subactions()
# make a set of commands not yet added.
remaining = set(ramble.cmd.all_commands())
def add_group(group):
formatter.start_section(group.title)
formatter.add_text(group.description)
formatter.add_arguments(group._group_actions)
formatter.end_section()
def add_subcommand_group(title, commands):
"""Add informational help group for a specific subcommand set."""
cmd_set = {c for c in commands}
# make a dict of commands of interest
cmds = {a.dest: a for a in self.actions if a.dest in cmd_set}
# add commands to a group in order, and add the group
group = argparse._ArgumentGroup(self, title=title)
for name in commands:
group._add_action(cmds[name])
if name in remaining:
remaining.remove(name)
add_group(group)
# select only the options for the particular level we're showing.
show_options = options_by_level[level]
if show_options != "all":
opts = {
opt.option_strings[0].strip("-"): opt for opt in self._optionals._group_actions
}
new_actions = [opts[letter] for letter in show_options]
self._optionals._group_actions = new_actions
# custom, more concise usage for top level
help_options = self._optionals._group_actions
help_options = help_options + [self._positionals._group_actions[-1]]
formatter.add_usage(self.usage, help_options, self._mutually_exclusive_groups)
# description
formatter.add_text(self.description)
# start subcommands
formatter.add_text(intro_by_level[level])
# add argument groups based on metadata in commands
index = index_commands()
sections = index[level]
for section in sorted(sections):
if section == "help" or section == "secret":
continue # Cover help in the epilog.
group_description = section_descriptions.get(section, section)
to_display = sections[section]
commands = []
# add commands whose order we care about first.
if section in section_order:
commands.extend(cmd for cmd in section_order[section] if cmd in to_display)
# add rest in alphabetical order.
commands.extend(cmd for cmd in sorted(sections[section]) if cmd not in commands)
# add the group to the parser
add_subcommand_group(group_description, commands)
# optionals
add_group(self._optionals)
# epilog
formatter.add_text(