Helpstring в argparse, каждая строка из новой строки - PullRequest
0 голосов
/ 11 ноября 2018

Я делаю утилиту CLI. При добавлении строки документации для вызова справки для модуля с функцией -- help в консоли я столкнулся с тем, что весь добавленный текст отображается как непрерывное неразрывное сообщение. Как отделить строки друг от друга? Я пытался добавить \n в конце строки, но это не работает.

def createParser():
    parser = argparse.ArgumentParser(
        prog='samplefind',
        description="""
        Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file.
        From command line run python sfind.py .
        To view all available options: python sfind.py -h .
        """

1 Ответ

0 голосов
/ 11 ноября 2018

Используйте formatter_class=argparse.RawTextHelpFormatter, чтобы сохранить все пробелы в строке справки. Это строка справки приложения argparse, а не docstring. Может выглядеть немного уродливо, хотя:

parser = argparse.ArgumentParser(
        prog='samplefind',
        formatter_class=argparse.RawTextHelpFormatter,
        description="""
        Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file.
        From command line run python sfind.py .
        To view all available options: python sfind.py -h .
        """)

С терминала:

py bla.py -h использование: samplefind [-h]

    Script to search for matches by word or lines in a text file and save the found information in a new outfile.txt file.
    From command line run python sfind.py .
    To view all available options: python sfind.py -h .

Обратите внимание, что это включает пробелы от начала строки, новые строки, все.

...