示例#1
0
文件: _main.py 项目: sdot-b/rez
def setup_parser():
    """Create and setup parser for given rez command line interface.

    Returns:
        LazyArgumentParser: Argument parser for rez command.
    """
    py = sys.version_info
    parser = LazyArgumentParser("rez")

    parser.add_argument("-i",
                        "--info",
                        action=InfoAction,
                        help="print information about rez and exit")
    parser.add_argument("-V",
                        "--version",
                        action="version",
                        version="Rez %s from %s (python %d.%d)" %
                        (__version__, module_root_path, py.major, py.minor))

    # add args common to all subcommands... we add them both to the top parser,
    # AND to the subparsers, for two reasons:
    #  1) this allows us to do EITHER "rez --debug build" OR
    #     "rez build --debug"
    #  2) this allows the flags to be used when using either "rez" or
    #     "rez-build" - ie, this will work: "rez-build --debug"
    _add_common_args(parser)

    # add lazy subparsers
    subparser = parser.add_subparsers(dest='cmd', metavar='COMMAND')
    for subcommand, data in subcommands.items():
        module_name = data.get('module_name', 'rez.cli.%s' % subcommand)

        subparser.add_parser(
            subcommand,
            help='',  # required so that it can be setup later
            setup_subparser=SetupRezSubParser(module_name))

    return parser
示例#2
0
def command(opts, parser, extra_arg_groups=None):
    from rez.cli._util import subcommands
    import os
    import re

    # get comp info from environment variables
    comp_line = os.getenv("COMP_LINE", "")
    comp_point = os.getenv("COMP_POINT", "")
    try:
        comp_point = int(comp_point)
    except:
        comp_point = len(comp_line)

    last_word = comp_line.split()[-1]
    if comp_line.endswith(last_word):
        prefix = last_word
    else:
        prefix = None

    def _pop_arg(l, p):
        words = l.split()
        arg = None
        if words:
            arg = words[0]
            l_ = l.lstrip()
            p -= (len(l) - len(l_) + len(arg))
            l = l_[len(arg):]
            return l, p, arg
        return l, p, arg

    # determine subcommand, possibly give subcommand completion
    subcommand = None
    comp_line, comp_point, cmd = _pop_arg(comp_line, comp_point)
    if cmd in ("rez", "rezolve"):
        comp_line, comp_point, arg = _pop_arg(comp_line, comp_point)
        if arg:
            if prefix != arg:
                subcommand = arg
    else:
        subcommand = cmd.split("-", 1)[-1]

    if subcommand is None:
        cmds = [k for k, v in subcommands.items() if not v.get("hidden")]

        if prefix:
            cmds = (x for x in cmds if x.startswith(prefix))
        print(" ".join(cmds))

    if subcommand not in subcommands:
        return

    # replace '--' with special '--N#' flag so that subcommands can specify
    # custom completions.
    regex = re.compile("\s--\s")
    ddashes = regex.findall(comp_line)
    for i, ddash in enumerate(ddashes):
        j = comp_line.find(ddash)
        while comp_line[j] != "-":
            j += 1
        j += 2
        s = "N%d" % i
        comp_line = comp_line[:j] + s + comp_line[j:]
        if comp_point >= j:
            comp_point += len(s)

    # create parser for subcommand
    from rez.backport.importlib import import_module
    module_name = "rez.cli.%s" % subcommand
    mod = import_module(module_name)
    parser = argparse.ArgumentParser()
    mod.setup_parser(parser, completions=True)

    # have to massage input a little so argcomplete behaves
    cmd = "rez-%s" % subcommand
    comp_line = cmd + comp_line
    comp_point += len(cmd)

    # generate the completions
    from rez.cli._complete_util import RezCompletionFinder
    completer = RezCompletionFinder(parser=parser,
                                    comp_line=comp_line,
                                    comp_point=comp_point)
    words = completer.completions
    words = [w.decode() if hasattr(w, 'decode') else w for w in words]
    print(' '.join(words))