argparse epilog call function?
Didymus
lynto28 at gmail.com
Tue Jun 27 12:20:55 EDT 2017
On Tuesday, June 27, 2017 at 11:47:42 AM UTC-4, Didymus wrote:
> 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
Greetings,
I got what I was looking for:
def Examples():
text = """Lots of examples"""
return text.format()
parser = argparse.ArgumentParser(prog=sys.argv[0],
formatter_class=argparse.RawTextHelpFormatter,
description="Argparse Example",
epilog=text)
The key here is the "formatter_class=argparse.RawTestHelpFormatter". Once I set that the epilog prints out nicely formatted:
% ./test.py -h
usage: ./test.py [-h] [-n NAME] [-f | -m | -r | -v]
Argparse Example.
optional arguments:
-h, --help show this help message and exit
-n NAME, --name NAME Name to be created.
Examples:
Ex 1:
./test.py -n juser
Ex 2:
./test.py -n juser -r
...
Thanks for all the help.
-Tom
More information about the Python-list
mailing list