argparse surprise


Nothing terrible, just a handy thing to keep in mind

This is some of the things you don't expect to happen. And when it happens, is maybe in the midst of coding when your head is more focused on elaborated problems, not wanting to deal with some obscure argparse error. Sounds like a kind of catharsis?

Let's go to the point: argparse is clean and easy to use, but if you use a "%" in the help/summary/epilog text, it will fail, and will not be solved by use "\%". I discovered it the hard way.

Let's see the following example, where the text for "h3" contains the named symbol in "(...) 80% of the distribution (...)":

When running the code it will give an error:

[paztronomer]$ python --help
TypeError: %o format: an integer is required, not dict

What we do? Simply change "%" by "%%" and argparse will not complain :)

The modified text will read:

Hope you'll find it useful, when the time comes.