Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

argparse epilog call function?

324 views
Skip to first unread message

Didymus

unread,
Jun 27, 2017, 9:56:13 AM6/27/17
to
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

Chris Angelico

unread,
Jun 27, 2017, 10:11:31 AM6/27/17
to
The way you've written it, Examples() will be called before argparse
does its work, and whatever it returns becomes the epilog. Can you
work your function such that it returns a string, instead of printing
something? Or is it a very expensive function?

ChrisA

Fox

unread,
Jun 27, 2017, 10:15:22 AM6/27/17
to

what " -h " are you even talkin bout ?




def Examples():
text = """Lots of examples"""
print(text.format())



epilog='where the heck to put a -h ?????? '

epilog=Examples()


Chris Angelico

unread,
Jun 27, 2017, 10:25:10 AM6/27/17
to
On Wed, Jun 28, 2017 at 12:09 AM, Fox <fir...@firemail.cc> wrote:
> what " -h " are you even talkin bout ?
>
>
>
>
> def Examples():
> text = """Lots of examples"""
> print(text.format())
>
>
>
> epilog='where the heck to put a -h ?????? '
>
> epilog=Examples()

List admins, this person has been abusing the pydotorg-www list for
several days. Can he please be banned?

ChrisA

Peter Otten

unread,
Jun 27, 2017, 11:36:11 AM6/27/17
to
Here's a way using a custom *formatter*:

$ cat epilog.py
import argparse

class MyHelpFormatter(argparse.HelpFormatter):
def add_text(self, text):
if callable(text):
text = text()
return super().add_text(text)

def examples():
return "yadda yadda"

parser = argparse.ArgumentParser(
epilog=examples,
formatter_class=MyHelpFormatter
)
parser.add_argument("--foo")

parser.parse_args()
$ python3 epilog.py -h
usage: epilog.py [-h] [--foo FOO]

optional arguments:
-h, --help show this help message and exit
--foo FOO

yadda yadda
$


Didymus

unread,
Jun 27, 2017, 11:47:42 AM6/27/17
to
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

Didymus

unread,
Jun 27, 2017, 12:21:26 PM6/27/17
to
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

bream...@gmail.com

unread,
Jun 27, 2017, 2:04:27 PM6/27/17
to
I'll second that, the wxpython lists have been suffering similar abuse for several days.

Kindest regards.

Mark Lawrence.

Rasputin

unread,
Jun 30, 2017, 8:35:38 AM6/30/17
to
will you ever post a single .py file which actually works ?

all the stuff you post is breaking.

Rasputin

unread,
Jun 30, 2017, 8:45:00 AM6/30/17
to
C. Angelico and Mark Lawrence,


who the fuck does this twin-scum think they are ?

you better stfu right now.


these guys are responsible for turning the pythonWiki into a pile of
rubbish.

fuck off and never return !








bream...@gmail.com:

bream...@gmail.com

unread,
Jun 30, 2017, 8:59:55 AM6/30/17
to
On Friday, June 30, 2017 at 1:45:00 PM UTC+1, Rasputin wrote:
> C. Angelico and Mark Lawrence,
>
> who the fuck does this twin-scum think they are ?
>
> you better stfu right now.
>
> these guys are responsible for turning the pythonWiki into a pile of
> rubbish.

pythonWiki???

>
> fuck off and never return !
>

One strike and you're out.

*plonk*

Mark Lawrence

D'Arcy Cain

unread,
Jun 30, 2017, 9:22:28 AM6/30/17
to
On 06/30/17 08:44, Rasputin wrote:
> fuck off and never return !

Chill dude. Haven't you ever heard of a killfile? You just earned a
place in mine.

--
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:da...@Vex.Net VoIP: sip:da...@VybeNetworks.com

Rasputin

unread,
Jun 30, 2017, 11:17:11 AM6/30/17
to
LOL

Rasputin

unread,
Jun 30, 2017, 11:23:56 AM6/30/17
to
bream...@gmail.com:

> pythonWiki???
>
>>
>> fuck off and never return !
>>
>
> One strike and you're out.
>
> *plonk*
>
> Mark Lawrence


read up about all those criminals of newsgroup "comp.python.pydotorg-www"
right here:

https://practical-scheme.net/wiliki/wiliki.cgi?SandBox

Andre Müller

unread,
Jun 30, 2017, 12:02:35 PM6/30/17
to
Just don't read it. Calm down.

Nikolaus Wirth

unread,
Jun 30, 2017, 3:33:27 PM6/30/17
to
>> One strike and you're out.
>> Mark Lawrence


Can you formally prove that?

Mark Lawrence

unread,
Jul 1, 2017, 8:48:53 AM7/1/17
to
Rasputin:
> LOL


shocking ! this guy wants to undermine our position of power to rule
over the posting scum !

we must punish all users !

0 new messages