Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python optparse metavar

People also ask

What is Metavar in Argparse?

Metavar: It provides a different name for optional argument in help messages.

What is import Optparse in Python?

Optparse module makes easy to write command-line tools. It allows argument parsing in the python program. optparse make it easy to handle the command-line argument. It comes default with python. It allows dynamic data input to change the output.

What is Optparse?

optparse is a more convenient, flexible, and powerful library for parsing command-line options than the old getopt module. optparse uses a more declarative style of command-line parsing: you create an instance of OptionParser , populate it with options, and parse the command line.


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.


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