使用空格格式化帮助菜单

2024-06-16 13:55:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个交互式shell创建一个帮助菜单,我需要的是它以格式化的方式输出列表,这个程序已经使用argparse,当没有给定标志时,用户被重定向到脚本的命令行界面(终端)。我希望用户能够以格式化的方式查看帮助菜单:

TOOL_LIST = {
    "-s": ["(Run a SQLi vulnerability scan on a URL)", "sqli"],
    "-x": ["(Run a cross site scripting scan on a URL)", "xss"],
    "-p": ["(Run a Port scan on a URL or given host)", "port"],
    "-h": ["(Attempt to crack a given hash)", "crack"],
    "-v": ["(Verify the algorithm used for a given hash)", "verify"],
    "-d": ["(Do a dork check to verify if your dork is good)", "dork"],
    "-f": ["(Find usable proxies)", "proxy"],
    "-hh": ["(Produce a help menu with basic descriptions)", "help"]
}

def help_menu():
    """
    Specs: Produce a help menu with basic descriptions
    Usage: run menu
    """
    print("Command  Secondary-Command  Descriptor")
    primary_spacer = ""
    descrip_spacer = ""
    secondary_spacer = ""
    for key in TOOL_LIST.iterkeys():
        if len(key) == 3:
            primary_spacer = " " * 2
            secondary_spacer = " " * 10
            descrip_spacer = " " * len(TOOL_LIST[key][1])
        else:
            primary_spacer = " " * 2
            secondary_spacer = " " * 11
            descrip_spacer = " " * len(TOOL_LIST[key][1])

        print("{}{}{}{}{}{}".format(
            primary_spacer, key, secondary_spacer,
            TOOL_LIST[key][1], descrip_spacer, TOOL_LIST[key][0]
        ))

到目前为止,它的输出如下:

Command  Secondary-Command  Descriptor
  -d           dork    (Do a dork check to verify if your dork is good)
  -f           proxy     (Find usable proxies)
  -v           verify      (Verify the algorithm used for a given hash)
  -p           port    (Run a Port scan on a URL or given host)
  -s           sqli    (Run a SQLi vulnerability scan on a URL)
  -hh          help    (Produce a help menu with basic descriptions)
  -x           xss   (Run a cross site scripting scan on a URL)
  -h           crack     (Attempt to crack a given hash)

如何让python格式化菜单的输出,如下所示:

Command  Secondary-Command  Descriptor
  -d           dork         (Do a dork check to verify if your dork is good)
  -f           proxy        (Find usable proxies)
  -v           verify       (Verify the algorithm used for a given hash)
  -p           port         (Run a Port scan on a URL or given host)
  -s           sqli         (Run a SQLi vulnerability scan on a URL)
  -hh          help         (Produce a help menu with basic descriptions)
  -x           xss          (Run a cross site scripting scan on a URL)
  -h           crack        (Attempt to crack a given hash)

Tags: tokeyrunurlscanonhelphash
1条回答
网友
1楼 · 发布于 2024-06-16 13:55:17

这样就可以了-

descript_spacer(两处)替换为:

descrip_spacer =  " " * (13 - len(TOOL_LIST[key][1]))

我选择了13,因为它看起来和你想要的一模一样。您可能希望将其设为顶级变量。你知道吗

输出-

Command  Secondary-Command  Descriptor
  -d           dork         (Do a dork check to verify if your dork is good)
  -f           proxy        (Find usable proxies)
  -v           verify       (Verify the algorithm used for a given hash)
  -p           port         (Run a Port scan on a URL or given host)
  -s           sqli         (Run a SQLi vulnerability scan on a URL)
  -hh          help         (Produce a help menu with basic descriptions)
  -x           xss          (Run a cross site scripting scan on a URL)
  -h           crack        (Attempt to crack a given hash)

相关问题 更多 >