0

I am trying add description in one of my python script but out put is not formatted.

def main():

    hlp_string = (""" File nomenclature:
        \t<name>_<last name>_<version>_<report_name>.<extension>\n
        \tVersion will be always NONE\n
        \tFor txt:\n
        \t\treport_name = unformatted\n
        \t\tfile_extension = txt\n
        \tFor csv:\n
        \t\treport_name = column\n
        \t\tfile_extension = csv\n
        \t\tExample:\n
        \t\tRON_DAVIS_NONE_unformatted.txt\n      
        \t\tRON_DAVIS_NONE_column.csv
        """
    )
    parser = argparse.ArgumentParser(description=hlp_string)
    parser.add_argument("-l", "--name_details", action="store", help="name_details")
    parser.add_argument(
        "-p", "--data_details", action="store", help="data_details"
    )
    args = parser.parse_args()
    if args.name_details and args.data_details:
        print("Print got argument")
    else:
        parser.print_help()

if __name__=="__main__":
        main()

Once I am running this getting flat text, This is neither adding \t or \n in output

mytest.py --help
usage: mytest.py [-h] [-l NAME_DETAILS] [-p DATA_DETAILS]

File nomenclature: <name>_<last name>_<version>_<report_name>.<extension>
Version will be always NONE For txt: report_name = unformatted file_extension
= pdf For csv: report_name = column file_extension = csv Example:
RON_DAVIS_NONE_unformatted.txt RON_DAVIS_NONE_column.csv

optional arguments:
  -h, --help            show this help message and exit
  -l NAME_DETAILS, --name_details NAME_DETAILS
                        name_details
  -p DATA_DETAILS, --data_details DATA_DETAILS
                        data_details
choroba
  • 216,930
  • 22
  • 195
  • 267
CrazyC
  • 1,790
  • 6
  • 35
  • 58
  • Does https://stackoverflow.com/questions/5462873/control-formatting-of-the-argparse-help-argument-list help? I found it as the first search result for `argparse parser.print_help formatting`. – Karl Knechtel Nov 24 '21 at 16:59
  • 1
    FWIW, I have found that while it does a lot of standard tasks well, `argparse` isn't the cleanest or most elegantly designed thing out there. Third-party tool recommendations are off topic here, but you might consider looking around a bit. – Karl Knechtel Nov 24 '21 at 17:01

1 Answers1

0

The simplest solution is to replace parser.print_help() with print(hlp_string).
This may answer your question.

ma4strong
  • 195
  • 1
  • 5
  • `parser.print_help` prints more than just the `hlp_string` here - it does the extra work to display the `usage` line and neatly formatted `optional arguments`, which it determines automatically from the `parser`'s own state. – Karl Knechtel Nov 24 '21 at 16:58