argparse epilog call function?

Didymus lynto28 at gmail.com
Tue Jun 27 11:47:13 EDT 2017


On Tuesday, June 27, 2017 at 9:56:13 AM UTC-4, Didymus wrote:
> Greetings,
> 
> I might be barking up the wrong tree, but was wondering if there's a way to have the argpasre epilog call a function. for example:
> 
> epilog=Examples()
> 
> Where Examples is:
> 
> def Examples():
>     text = """Lots of examples"""
>     print(text.format())
> 
> I've place this in and found that it prints out no matter if I use the -h or
> not and also it prints first.... If I do:
> 
> epilog='Single Example'
> 
> it works as intended, unfortunately, I need to show several examples. Just wondering if someone has found a why to do this (without making a custom help).
> 
> thanks
>   Tom
Well, I thought I had a good idea to work around this by setting a string to the formatted text and then use that string in the epilog:

text = Example()
#
parser = argparse.ArgumentParser(prog=sys.argv[0],
description="Example Arg Parse",
epilog=text)

I had to change the Example function to return the string and in a print it works fine, however when used in the argsparse, the formatting is lost...

-T



More information about the Python-list mailing list