How to get the content of help of argparse, and not to print to console

Viewed 61

I'm work on telegram bot, not the console, need to get the help content to send to user, rather than print out to console, something like this

parser = argparse.ArgumentParser()
parser.add_argument("-a", "--aaa", help="help for aaa")
cmd=["-a", "seta"]
args = parser.parse_args(cmd)
helpstring = parser.print_help() # get the help content for furthur process
telegrambot.send(helpstring)
1 Answers

You can use the parser.format_help method:

>>> parser.format_help()
'usage: [-h] [-a AAA]\n\noptional arguments:\n  -h, --help         show this help message and exit\n  -a AAA, --aaa AAA  help for aaa\n'
>>> print(parser.format_help())
usage: [-h] [-a AAA]

optional arguments:
  -h, --help         show this help message and exit
  -a AAA, --aaa AAA  help for aaa
Related