{
    "mode": "pydoc",
    "parameter": "getopt",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/pydoc/getopt/json",
    "generated": "2026-06-02T15:03:22Z",
    "sections": {
        "NAME": {
            "content": "getopt - Parser for command line options.\n",
            "subsections": []
        },
        "MODULE REFERENCE": {
            "content": "https://docs.python.org/3.10/library/getopt.html\n\nThe following documentation is automatically generated from the Python\nsource files.  It may be incomplete, incorrect or include features that\nare considered implementation detail and may vary between Python\nimplementations.  When in doubt, consult the module reference at the\nlocation listed above.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module helps scripts to parse the command line arguments in\nsys.argv.  It supports the same conventions as the Unix getopt()\nfunction (including the special meanings of arguments of the form `-'\nand `--').  Long options similar to those supported by GNU software\nmay be used as well via an optional third argument.  This module\nprovides two functions and an exception:\n",
            "subsections": [
                {
                    "name": "getopt",
                    "content": ""
                },
                {
                    "name": "gnu_getopt",
                    "content": "to be intermixed.\nGetoptError -- exception (class) raised with 'opt' attribute, which is the\noption involved with the exception.\n"
                }
            ]
        },
        "CLASSES": {
            "content": "builtins.Exception(builtins.BaseException)\nGetoptError\n",
            "subsections": [
                {
                    "name": "class GetoptError",
                    "content": "|  GetoptError(msg, opt='')\n|\n|  Method resolution order:\n|      GetoptError\n|      builtins.Exception\n|      builtins.BaseException\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  init(self, msg, opt='')\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  str(self)\n|      Return str(self).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  msg = ''\n|\n|  opt = ''\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from builtins.Exception:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from builtins.BaseException:\n|\n|  delattr(self, name, /)\n|      Implement delattr(self, name).\n|\n|  getattribute(self, name, /)\n|      Return getattr(self, name).\n|\n|  reduce(...)\n|      Helper for pickle.\n|\n|  repr(self, /)\n|      Return repr(self).\n|\n|  setattr(self, name, value, /)\n|      Implement setattr(self, name, value).\n|\n|  setstate(...)\n|\n|  withtraceback(...)\n|      Exception.withtraceback(tb) --\n|      set self.traceback to tb and return self.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from builtins.BaseException:\n|\n|  cause\n|      exception cause\n|\n|  context\n|      exception context\n|\n|  dict\n|\n|  suppresscontext\n|\n|  traceback\n|\n|  args\n\nerror = class GetoptError(builtins.Exception)\n|  error(msg, opt='')\n|\n|  Method resolution order:\n|      GetoptError\n|      builtins.Exception\n|      builtins.BaseException\n|      builtins.object\n|\n|  Methods defined here:\n|\n|  init(self, msg, opt='')\n|      Initialize self.  See help(type(self)) for accurate signature.\n|\n|  str(self)\n|      Return str(self).\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors defined here:\n|\n|  weakref\n|      list of weak references to the object (if defined)\n|\n|  ----------------------------------------------------------------------\n|  Data and other attributes defined here:\n|\n|  msg = ''\n|\n|  opt = ''\n|\n|  ----------------------------------------------------------------------\n|  Static methods inherited from builtins.Exception:\n|\n|  new(*args, kwargs) from builtins.type\n|      Create and return a new object.  See help(type) for accurate signature.\n|\n|  ----------------------------------------------------------------------\n|  Methods inherited from builtins.BaseException:\n|\n|  delattr(self, name, /)\n|      Implement delattr(self, name).\n|\n|  getattribute(self, name, /)\n|      Return getattr(self, name).\n|\n|  reduce(...)\n|      Helper for pickle.\n|\n|  repr(self, /)\n|      Return repr(self).\n|\n|  setattr(self, name, value, /)\n|      Implement setattr(self, name, value).\n|\n|  setstate(...)\n|\n|  withtraceback(...)\n|      Exception.withtraceback(tb) --\n|      set self.traceback to tb and return self.\n|\n|  ----------------------------------------------------------------------\n|  Data descriptors inherited from builtins.BaseException:\n|\n|  cause\n|      exception cause\n|\n|  context\n|      exception context\n|\n|  dict\n|\n|  suppresscontext\n|\n|  traceback\n|\n|  args\n"
                }
            ]
        },
        "FUNCTIONS": {
            "content": "",
            "subsections": [
                {
                    "name": "getopt",
                    "content": "getopt(args, options[, longoptions]) -> opts, args\n\nParses command line options and parameter list.  args is the\nargument list to be parsed, without the leading reference to the\nrunning program.  Typically, this means \"sys.argv[1:]\".  shortopts\nis the string of option letters that the script wants to\nrecognize, with options that require an argument followed by a\ncolon (i.e., the same format that Unix getopt() uses).  If\nspecified, longopts is a list of strings with the names of the\nlong options which should be supported.  The leading '--'\ncharacters should not be included in the option name.  Options\nwhich require an argument should be followed by an equal sign\n('=').\n\nThe return value consists of two elements: the first is a list of\n(option, value) pairs; the second is the list of program arguments\nleft after the option list was stripped (this is a trailing slice\nof the first argument).  Each option-and-value pair returned has\nthe option as its first element, prefixed with a hyphen (e.g.,\n'-x'), and the option argument as its second element, or an empty\nstring if the option has no argument.  The options occur in the\nlist in the same order in which they were found, thus allowing\nmultiple occurrences.  Long and short options may be mixed.\n"
                },
                {
                    "name": "gnu_getopt",
                    "content": "getopt(args, options[, longoptions]) -> opts, args\n\nThis function works like getopt(), except that GNU style scanning\nmode is used by default. This means that option and non-option\narguments may be intermixed. The getopt() function stops\nprocessing options as soon as a non-option argument is\nencountered.\n\nIf the first character of the option string is `+', or if the\nenvironment variable POSIXLYCORRECT is set, then option\nprocessing stops as soon as a non-option argument is encountered.\n"
                }
            ]
        },
        "DATA": {
            "content": "all = ['GetoptError', 'error', 'getopt', 'gnugetopt']\n",
            "subsections": []
        },
        "FILE": {
            "content": "/usr/lib/python3.10/getopt.py\n\n",
            "subsections": []
        }
    },
    "summary": "getopt - Parser for command line options.",
    "flags": [],
    "examples": [],
    "see_also": [],
    "tldr": {
        "source": "official",
        "description": "Parse command-line arguments.",
        "examples": [
            {
                "description": "Parse optional `verbose`/`version` flags with shorthands",
                "command": "getopt {{-o|--options}} vV {{-l|--longoptions}} verbose,version -- --version --verbose"
            },
            {
                "description": "Add a `--file` option with a required argument with shorthand `-f`",
                "command": "getopt {{-o|--options}} f: {{-l|--longoptions}} file: -- --file=somefile"
            },
            {
                "description": "Add a `--verbose` option with an optional argument with shorthand `-v`, and pass a non-option parameter `arg`",
                "command": "getopt {{-o|--options}} v:: {{-l|--longoptions}} verbose:: -- --verbose arg"
            },
            {
                "description": "Accept a `-r` and `--verbose` flag, a `--accept` option with an optional argument and add a `--target` with a required argument option with shorthands",
                "command": "getopt {{-o|--options}} rv::s::t: {{-l|--longoptions}} verbose,source::,target: -- -v --target target"
            }
        ]
    }
}