コード例 #1
0
def loop_main():
    """
    run_loop main function
    """
    gpf = gget("general.pf")
    api = gget("api")
    old_namespace = ''
    while gget("loop"):
        namespace = gget("namespace")
        tpf = None
        npf = gget(f"{namespace}.pf")
        if (namespace != old_namespace):
            wordlist = gget(namespace + ".wordlist")
            prefix_wordlist = gget(namespace + ".prefix_wordlist")
            readline.set_wordlist(wordlist)
            readline.set_prefix_wordlist(prefix_wordlist)
        # --------------------------------------
        if (gget("preload_command")):
            cmd = gget("preload_command")
            gset("preload_command", None, True)
        else:
            print(gget(f"{namespace}.prompt"), end="")
            if (gget("raw_input") is True):
                cmd = input().strip()
            else:
                cmd = readline().strip()
        gset("raw_command", cmd, True)
        if (not cmd):
            continue
        args = shlex.split(cmd)  # 切割
        if " " in cmd:  # 输入的命令
            order = args[0]
        else:
            order = cmd
        del args[0]
        raw_command_args = " ".join(args)
        gset("raw_command_args", raw_command_args, True)
        order = order_alias(order)  # 解析别名
        # --------------------------------------
        if order in npf:  # 命令存在
            tpf = npf
        elif order in gpf:
            tpf = gpf
        elif cmd:
            print("[Error] %s: Command Not Found" % order)
        if tpf:
            try:
                arg_dict = args_parse(args)  # 解析参数
                tpf[order].run(**arg_dict)
            except TypeError as e:
                exc_type, exc_value, exc_tb = exc_info()
                print("[TypeError] %s" %
                      str(e).replace("%s()" % api, "%s()" % order))
                if DEBUG["LOOP"]:
                    print_exception(exc_type, exc_value, exc_tb)
            except Exception as e:
                exc_type, exc_value, exc_tb = exc_info()
                if DEBUG["LOOP"]:
                    print_exception(exc_type, exc_value, exc_tb)
                print("[%s] %s" % (exc_type.__name__, e))
コード例 #2
0
def run(order: str = ""):
    """
    help

    Output the help document for the command or all help menu.

    eg: help {order}
    """
    if (order == ""):
        set_namespace(gget("namespace"), True, False)
        return
    tpf = None
    gpf = gget(f"general.pf")
    npf = gget(f"{gget('namespace')}.pf")
    cpf = gget(f"custom.pf")
    order = order_alias(order)
    if (order in npf):
        tpf = npf
    elif (order in gpf):
        tpf = gpf
    elif (order in cpf):
        tpf = cpf
    elif order:
        print("%s object is not command-function" % order)
        return
    api = gget("api")
    func = getattr(tpf[order], api)
    if (func.__doc__):
        print(func.__doc__)
    block = " " * 4
    block_two = block * 2
    sig = inspect.signature(func)
    folders_namespace = gget("folders_namespace")
    func_folder, func_name = func.__module__.split(".")
    func_reverse_alias = gget("%s.reverse_alias" % func_name,
                              folders_namespace[func_folder])
    if (len(sig.parameters)):
        print("%sCommand Args:" % block)
    for k, v in sig.parameters.items():
        arg = "--%s" % k
        if k in func_reverse_alias:
            arg = "-%s,%s" % (func_reverse_alias[k], arg)
        desc = str(v).split(":")
        if len(desc) > 1:  # 存在参数类型
            desc = desc[1].split("=")
            if len(desc) > 1:  # 存在默认值
                desc = "[%s] %s (Default: %s)" % (
                    desc[0].strip(),
                    k,
                    desc[1].strip(),
                )
            else:
                desc = "[%s] %s" % (desc[0].strip(), k)
                arg = "%s(*)" % arg
        else:
            desc = "[?] %s" % desc[0]
            arg = "%s(?)" % arg
        print("%s%-25s%s%s\n" % (block_two, arg, block_two, desc))