python - 退格 \n \t 在长字符串 python argparse 中不起作用
问题描述
我有以下代码,我在其中添加了工具的描述
usage = r"""
\n-------------------------------------------
Serial Chain Processing\n \
\n--------------------------------------------
\nSteps.
1. Buffers to 500 m a shapefile containing the country boundaries in EPSG: 3035
projection.\n
2. Clip a continental mask (DLT, GRA, or TCD), to the country boundaries (EPSG:3035)\n
3. Reproject the raster from step 2 to the National Grid using a WKT file
with the translation parameters.\n
4. Use the raster calculator to correct the outside areas using the
following calculation:
\n\tif(a==1){
\n\t\toutput=b;
\n\t}
\n\tif(a==0){
\n\t\toutput = a;
\n\t}
\n\tif(a==1&&IsNoData(b)){
\n\t\toutput=0;
\n\t}
\n\tif(a==1&&b==255){
\n\t\toutput=0;
\n}
\n\tif(a==0){
\n\t\toutput=255;
\n\t}
\nBeing
\n\ta == Binary country boundaries raster in National projection
\n\tb == Raster from Step 3.
\n5. Check for compression, no data, pyramids and create raster attribute table,
as well as the color maps.
"""
import glob
import subprocess
import os
import gdal
import osr
import ogr
import argparse
from osgeo import gdal,osr
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
def main(mask, country_bound_LAEA, country_bound_NAT):
"""Initializes the workflow."""
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=usage)
parser.add_argument('--mask',metavar="", type=dir_path, help = "National Product in LAEA")
parser.add_argument('--country_bound_LAEA',metavar="", type=dir_path, help = "Country national boundaries in LAEA - Shapefile")
parser.add_argument('--WKT_file',metavar="", type=dir_path, help = "WKT File derived from the national boundaries raster")
parser.add_argument('--country_bound_NAT',metavar="", type=dir_path, help = "National Boundaries, binary raster dataset")
parser.parse_args()
#main(mask, country_bound_LAEA, country_bound_NAT)
当我运行 python -h 时,它会打印存储在 中的描述usage
,但是\n
并\t
不能跳到下一行和同一行中的制表符,以使描述更清晰一些。我不知道如何解决这个问题。我认为在过去,我使用 increment 添加了越来越多的字符串+
,但是这很烦人,因为描述有几行长。
解决方案
使用 .制作多行字符串时不需要任何特殊的转义字符"""
。你也不需要r
前面的"""
. 您需要做的就是formatter_class
向 argparse 提供一个,如下所示:
parser = argparse.ArgumentParser(description=usage, formatter_class=argparse.RawDescriptionHelpFormatter)
这应该使它能够准确地打印它在您的代码中的外观。
推荐阅读
- python - Python在单行上使用变量打印日期和时间
- powershell - 从PowerShell中的文件名中获取2个字符
- reactjs - 如何在 VPS 中备份 MERN Web 应用程序
- javascript - React Select 在多选时选择缩写下拉名称
- mysql - 检查Laravel的子表中是否存在2条具有不同user_id的不同记录
- laravel - 重置密码电子邮件未在 Laravel 8.53 中进行翻译
- java - 如何通过 JsonPath 配置将 JSON 反序列化为 POJO
- adsense - 可以在 pythonanywhere 免费计划上添加 adsense 吗?
- sql - PostgreSQL 对计数字段的分组查询作为 smth 和总计数字段的一部分
- c - 使用c中的动态分配从文件中读取多个矩阵