# This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, # You can obtain one at http://mozilla.org/MPL/2.0/.
import argparse import re import subprocess import sys from itertools import chain from pathlib import Path
import attr from mozbuild.util import memoize
from mach.decorators import Command, CommandArgument, SubCommand
@memoize def global_options(command_context): """Return a dict of global options.
Of the form `{("-o", "--option"): "description"}`. """ for group in command_context._mach_context.global_parser._action_groups: if group.title == "Global Arguments": return _get_parser_options(group)
@memoize def _get_handler_options(handler): """Return a dict of options for the given handler.
Of the form `{("-o", "--option"): "description"}`. """
options = {} for option_strings, val in handler.arguments: # ignore positional args if option_strings[0][0] != "-": continue
@memoize def commands_info(command_context): """Return a list of CommandInfo objects for each command."""
commands_info = [] # Loop over self.commands() rather than self.command_handlers().items() for # alphabetical order. for c in commands(command_context):
commands_info.append(_get_handler_info(command_handlers(command_context)[c])) return commands_info
@Command("mach-commands", category="misc", description="List all mach commands.") def run_commands(command_context):
print("\n".join(commands(command_context)))
@Command( "mach-debug-commands",
category="misc",
description="Show info about available mach commands.",
)
@CommandArgument( "match",
metavar="MATCH",
default=None,
nargs="?",
help="Only display commands containing given substring.",
) def run_debug_commands(command_context, match=None): import inspect
for command, handler in command_handlers(command_context).items(): if match and match notin command: continue
@Command( "mach-completion",
category="misc",
description="Prints a list of completion strings for the specified command.",
)
@CommandArgument( "args", default=None, nargs=argparse.REMAINDER, help="Command to complete."
) def run_completion(command_context, args): ifnot args:
print("\n".join(commands(command_context))) return
is_help = "help"in args
command = None for i, arg in enumerate(args): if arg in commands(command_context):
command = arg
args = args[i + 1 :] break
# If no command is typed yet, just offer the commands. ifnot command:
print("\n".join(commands(command_context))) return
handler = command_handlers(command_context)[command] # If a subcommand was typed, update the handler. for arg in args: if arg in handler.subcommand_handlers:
handler = handler.subcommand_handlers[arg] break
targets = sorted(handler.subcommand_handlers.keys()) if is_help:
print("\n".join(targets)) return
def _zsh_describe(value, description=None):
value = '"' + value.replace(":", "\\:") if description:
description = subprocess.list2cmdline(
[re.sub(r'(["\'#&;`|*?~<>^()\[\]{}$\\\x0A\xFF])', r"\\\1", description)]
).lstrip('"')
if description.endswith('"') andnot description.endswith(r"\""):
description = description[:-1]
value += ":{}".format(description)
value += '"'
return value
@SubCommand( "mach-completion", "bash",
description="Print mach completion script for bash shell",
)
@CommandArgument( "-f", "--file",
dest="outfile",
default=None,
help="File path to save completion script.",
) def completion_bash(command_context, outfile):
commands_subcommands = []
case_options = []
case_subcommands = [] for i, cmd in enumerate(commands_info(command_context)): # Build case statement for options.
options = [] for opt_strs, description in cmd.options.items(): for opt in opt_strs:
options.append(_zsh_describe(opt, None).strip('"'))
# Build case statement for subcommand options. for sub in cmd.subcommands:
options = [] for opt_strs, description in sub.options.items(): for opt in opt_strs:
options.append(_zsh_describe(opt, None))
# Build case statement for subcommands.
subcommands = [_zsh_describe(s.subcommand, None) for s in cmd.subcommands] if subcommands:
commands_subcommands.append( '[{}]=" {} "'.format(
cmd.name, " ".join([h.subcommand for h in cmd.subcommands])
)
)
@SubCommand( "mach-completion", "zsh",
description="Print mach completion script for zsh shell",
)
@CommandArgument( "-f", "--file",
dest="outfile",
default=None,
help="File path to save completion script.",
) def completion_zsh(command_context, outfile):
commands_descriptions = []
commands_subcommands = []
case_options = []
case_subcommands = [] for i, cmd in enumerate(commands_info(command_context)):
commands_descriptions.append(_zsh_describe(cmd.name, cmd.description))
# Build case statement for options.
options = [] for opt_strs, description in cmd.options.items(): for opt in opt_strs:
options.append(_zsh_describe(opt, description))
# Build case statement for subcommand options. for sub in cmd.subcommands:
options = [] for opt_strs, description in sub.options.items(): for opt in opt_strs:
options.append(_zsh_describe(opt, description))
# Build case statement for subcommands.
subcommands = [
_zsh_describe(s.subcommand, s.description) for s in cmd.subcommands
] if subcommands:
commands_subcommands.append( '[{}]=" {} "'.format(
cmd.name, " ".join([h.subcommand for h in cmd.subcommands])
)
)
globalopts = [] for opt_strings, description in global_options(command_context).items(): for opt in opt_strings:
globalopts.append(_zsh_describe(opt, description))
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.