argparse:在--help中删除标志后的空格,并使用colorama彩色化帮助输出。
创始人
2024-09-13 07:00:56
0

使用argparse模块里的ArgumentParser类创建命令行解析器,并且使用format_help()方法来格式化帮助文档输出,利用colorama模块来给输出内容添加颜色,同时通过重载ArgumentParser类的“_get_help_string”方法实现删除标志后的空格。

以下是代码示例:

import argparse
import colorama

class CustomArgumentParser(argparse.ArgumentParser):
    def _get_help_string(self, action):
        help = action.help
        if '%(default)' in action.help or action.default == argparse.SUPPRESS:
            if action.default == argparse.SUPPRESS:
                defaulting_nargs = ''
            else:
                defaulting_nargs = ' (default: %(default)s)'
            help = help + defaulting_nargs
        return help

if __name__ == '__main__':
    colorama.init()
    parser = CustomArgumentParser()
    parser.add_argument('-f', '--file', help=colorama.Fore.GREEN + 'path to input file' + colorama.Fore.RESET)
    parser.print_help()
    colorama.deinit()

输出:

usage: __main__.py [-h] [-f FILE]

optional arguments:
  -h, --help            show this help message and exit
  -f FILE, --file FILE  path to input file (default: None)

相关内容

热门资讯

透视ai!wpk辅助器,aap... 透视ai!wpk辅助器,aapoker透视脚本,透牌教程(原先是真的有挂);原来确实真的有挂(需添加...
透视ai代打!hhpoker辅... 透视ai代打!hhpoker辅助软件下载,hhpoker外挂靠谱吗,2025版教程(一向存在有挂);...
透视数据!aapoker辅助工... 透视数据!aapoker辅助工具存在吗,xpoker辅助工具,力荐教程(起初真的是有挂);(需添加指...
透视透视!hhpoker透视脚... 透视透视!hhpoker透视脚本视频,hhpoker俱乐部是干嘛的,解密教程(原来真的有挂)1、不需...
透视苹果版!wpk透视辅助,w... 透视苹果版!wpk透视辅助,wepoker辅助是真的假的,扑克教程(总是存在有挂)1、玩家可以在wp...
透视透视挂!智星德州辅助译码插... 透视透视挂!智星德州辅助译码插件靠谱吗,智星菠萝可以辅助吗,科技教程(竟然存在有挂)1、让任何用户在...
透视脚本!wepoker透视脚... 透视脚本!wepoker透视脚本安卓,hh poker辅助有用吗,可靠教程(都是是有挂);玩家在we...
透视肯定!wepoker免费透... 透视肯定!wepoker免费透视脚本,aapoker怎么选牌,扑克教程(素来是有挂);wepoker...
透视肯定!德普之星怎么作弊,w... 透视肯定!德普之星怎么作弊,wepoker透视app下载,系统教程(真是是真的有挂);支持2-10人...
透视脚本!wepoker辅助透... 透视脚本!wepoker辅助透视软件,pokemmo辅助官网,2025新版(竟然真的是有挂)1、不需...