如何在Python argparse中生成带换行的多行示例命令?
问题:argparse中description换行失效的解决办法
我编写了名为dd.py的Python脚本,使用argparse库实现命令行参数解析。我定义了包含5条CAZyme注释相关示例命令的字符串example_commands,并将其作为argparse.ArgumentParser的description参数。期望执行python dd.py --help时,这些示例命令能按设定的换行格式清晰显示,但实际输出中所有示例命令的换行失效,内容被拼接在一起。
脚本代码
import argparse ## Define the command line arguments example_commands='''Example commands:\n 1. CAZyme annotation with isolated genome sequence as input \n run_dbcan EscheriaColiK12MG1655.fna prok \n 2. CAZyme annotation with isolated protein sequence as input \n run_dbcan EscheriaColiK12MG1655.faa protein \n 3. CAZyme annotation with meta genome as input \n run_dbcan EscheriaColiK12MG1655.fna meta \n 4. CAZyme and CGC annotation with meta genome as input \n run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff \n 5. CAZyme, CGC annotation and substrate prediction with meta genome as input \n run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff --cgc_substrate \n''' parser = argparse.ArgumentParser(description=example_commands) parser.add_argument('input_file', help='Input file path') parser.add_argument('input_type', choices=['prok', 'protein', 'meta'], help='Input type (prok, protein, or meta)') parser.add_argument('-c', '--gff_file', help='GFF file path') parser.add_argument('--cgc_substrate', action='store_true', help='Enable substrate prediction') # Parse the command line arguments and display the help message args = parser.parse_args()
预期输出
(base) ➜ ~ python dd.py --help usage: dd.py [-h] [-c GFF_FILE] [--cgc_substrate] input_file {prok,protein,meta} Example commands: 1. CAZyme annotation with isolated genome sequence as input run_dbcan EscheriaColiK12MG1655.fna prok 2. CAZyme annotation with isolated protein sequence as input run_dbcan EscheriaColiK12MG1655.faa protein 3. CAZyme annotation with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta 4. CAZyme and CGC annotation with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff 5. CAZyme, CGC annotation and substrate prediction with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff --cgc_substrate positional arguments: input_file Input file path {prok,protein,meta} Input type (prok, protein, or meta) optional arguments: -h, --help show this help message and exit -c GFF_FILE, --gff_file GFF_FILE GFF file path --cgc_substrate Enable substrate prediction
实际输出
(base) ➜ ~ python dd.py --help usage: dd.py [-h] [-c GFF_FILE] [--cgc_substrate] input_file {prok,protein,meta} Example commands: 1. CAZyme annotation with isolated genome sequence as input run_dbcan EscheriaColiK12MG1655.fna prok 2. CAZyme annotation with isolated protein sequence as input run_dbcan EscheriaColiK12MG1655.faa protein 3. CAZyme annotation with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta 4. CAZyme and CGC annotation with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff 5. CAZyme, CGC annotation and substrate prediction with meta genome as input run_dbcan EscheriaColiK12MG1655.fna meta -c EscheriaColiK12MG1655.gff --cgc_substrate positional arguments: input_file Input file path {prok,protein,meta} Input type (prok, protein, or meta) optional arguments: -h, --help show this help message and exit -c GFF_FILE, --gff_file GFF_FILE GFF file path --cgc_substrate Enable substrate prediction
解决办法
argparse默认的HelpFormatter会自动对description文本重新排版,把所有连续空白(包括换行)都替换成单个空格,导致你的换行失效。解决方法是使用RawDescriptionHelpFormatter类,它会完全保留你定义的原始文本格式:
修改解析器的初始化代码,添加formatter_class参数:
parser = argparse.ArgumentParser(description=example_commands, formatter_class=argparse.RawDescriptionHelpFormatter)
替换后再执行python dd.py --help,示例命令就会按照你设定的换行格式显示了。
内容的提问来源于stack exchange,提问作者Lena
相关产品推荐
相关产品推荐

