Python optparse metavar

12,429

Solution 1

As @Guillaume says, it's used for generating help. If you want to have an option that takes an argument, such as a filename, you can add the metavar parameter to the add_option call so your preferred argument name/descriptor is output in the help message. From the current module documentation:

usage = "usage: %prog [options] arg1 arg2"
parser = OptionParser(usage=usage)
parser.add_option("-f", "--filename",
                  metavar="FILE", help="write output to FILE"),

would produce help like this:

usage: <yourscript> [options] arg1 arg2

options:
  -f FILE, --filename=FILE

The "FILE" after the "-f" and the "--filename" comes from the metavar.

Solution 2

metavar seems to be used for generating help : http://www.python.org/doc/2.5.2/lib/optparse-generating-help.html

Share:
12,429

Related videos on Youtube

bluish
Author by

bluish

I'm here to learn and to improve help retrieval about IT. #SOreadytohelp You can contact me at rumoreblu snail gmail dot com ;)

Updated on November 13, 2020

Comments

  • bluish
    bluish about 3 years

    I am not sure what optparse's metavar parameter is used for. I see it is used all around, but I can't see its use.

    Can someone make it clear to me? Thanks.

  • user1066101
    user1066101 about 15 years
    metavar="helpfulname" customizes the help message with a helpful name; different from the dest="name". The dest="name" might not be helpful to users.