Issue 34174: argparse formatter_class issue for RawDescriptionHelpFormatter

Created on 2018-07-20 20:34 by ckessler, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg322034 - (view) Author: Chris Kessler (ckessler) Date: 2018-07-20 20:34
In document https://docs.python.org/2/library/argparse.html#argparse.RawDescriptionHelpFormatter

this will fail with NameError: global name 'textwrap' is not defiend

>>> parser = argparse.ArgumentParser(
...     prog='PROG',
...     formatter_class=argparse.RawDescriptionHelpFormatter,
...     description=textwrap.dedent('''\
...         Please do not mess up this text!
...         --------------------------------
...             I have indented it
...             exactly the way
...             I want it
...         '''))
msg322036 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2018-07-20 20:42
Thanks for the report.

To make it work, you'll have to import textwrap first.

We don't always add all the necessary imports in our code snippets and examples, and we don't need to start adding imports into code examples either. So I'm closing this issue.

Thanks again.
msg322037 - (view) Author: Chris Kessler (ckessler) Date: 2018-07-20 21:02
Thanks for the quick response!
I didnt see it anywhere in the documentation on that page so I assumed it was built in with argparse.
Thanks!
msg322228 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-07-23 16:00
argparse does import 'textwrap', but as '_textwrap', so it could be used with:

    argparse._textwrap.dedent(...)

Importing your own is cleaner.
History
Date User Action Args
2022-04-11 14:59:03adminsetgithub: 78355
2018-07-23 16:00:05paul.j3setnosy: + paul.j3
messages: + msg322228
2018-07-20 21:02:39ckesslersetmessages: + msg322037
2018-07-20 20:42:30Mariattasetstatus: open -> closed

nosy: + Mariatta
messages: + msg322036

resolution: not a bug
stage: resolved

2018-07-20 20:34:50ckesslercreate