This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse.ArgumentParser: usage example option
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, bethard, eric.araujo, jonash, paul.j3
Priority: normal Keywords:

Created on 2011-06-08 13:39 by jonash, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg137905 - (view) Author: Jonas H. (jonash) * Date: 2011-06-08 13:39
I'd like to see an `examples` option added to argparse.ArgumentParser as found in many man pages.

This could also be done using the `epilog` option, but that misses the "%(proc)s" replacement which makes usage examples like this

  Example usage:
    ./script.py option1 option2

impossible.
msg137965 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-06-09 13:13
Is http://docs.python.org/dev/library/argparse#usage what you want?
msg138008 - (view) Author: Jonas H. (jonash) * Date: 2011-06-09 16:29
Nope. I want an "examples" section, for example from `man git log`:


EXAMPLES
       git log --no-merges
           Show the whole commit history, but skip any merges

       git log v2.6.12.. include/scsi drivers/scsi
           Show all commits since version v2.6.12 that changed any file in the include/scsi or drivers/scsi subdirectories

       ...
msg149544 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2011-12-15 12:32
%(prog)s is available in epilog:

---------- temp.py ----------
import argparse

epilog = """\
Example usage:
  %(prog)s option1 option2
"""
parser = argparse.ArgumentParser(
    formatter_class=argparse.RawTextHelpFormatter,
    epilog=epilog)
parser.parse_args()
------------------------------
$ python temp.py -h
usage: temp.py [-h]

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

Example usage:
  temp.py option1 option2
------------------------------

Did you need more than that?
msg223197 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-16 13:36
@Jonas do you wish to follow up on this?
History
Date User Action Args
2022-04-11 14:57:18adminsetgithub: 56493
2017-03-13 10:54:41martin.pantersetstatus: open -> closed
resolution: works for me
stage: needs patch -> resolved
2014-07-16 13:36:56BreamoreBoysetnosy: + paul.j3, BreamoreBoy
messages: + msg223197
2011-12-15 12:33:00bethardsetmessages: + msg149544
2011-06-09 16:30:44brian.curtinsetstage: needs patch
2011-06-09 16:29:55jonashsetmessages: + msg138008
2011-06-09 13:13:08eric.araujosetnosy: + eric.araujo

messages: + msg137965
versions: + Python 3.3, - Python 2.7
2011-06-08 14:28:00r.david.murraysetnosy: + bethard
2011-06-08 13:39:41jonashcreate