| 1 | # -*- coding: iso-8859-1 -*-
|
|---|
| 2 | """Parser for command line options.
|
|---|
| 3 |
|
|---|
| 4 | This module helps scripts to parse the command line arguments in
|
|---|
| 5 | sys.argv. It supports the same conventions as the Unix getopt()
|
|---|
| 6 | function (including the special meanings of arguments of the form `-'
|
|---|
| 7 | and `--'). Long options similar to those supported by GNU software
|
|---|
| 8 | may be used as well via an optional third argument. This module
|
|---|
| 9 | provides two functions and an exception:
|
|---|
| 10 |
|
|---|
| 11 | getopt() -- Parse command line options
|
|---|
| 12 | gnu_getopt() -- Like getopt(), but allow option and non-option arguments
|
|---|
| 13 | to be intermixed.
|
|---|
| 14 | GetoptError -- exception (class) raised with 'opt' attribute, which is the
|
|---|
| 15 | option involved with the exception.
|
|---|
| 16 | """
|
|---|
| 17 |
|
|---|
| 18 | # Long option support added by Lars Wirzenius <[email protected]>.
|
|---|
| 19 | #
|
|---|
| 20 | # Gerrit Holl <[email protected]> moved the string-based exceptions
|
|---|
| 21 | # to class-based exceptions.
|
|---|
| 22 | #
|
|---|
| 23 | # Peter Åstrand <[email protected]> added gnu_getopt().
|
|---|
| 24 | #
|
|---|
| 25 | # TODO for gnu_getopt():
|
|---|
| 26 | #
|
|---|
| 27 | # - GNU getopt_long_only mechanism
|
|---|
| 28 | # - allow the caller to specify ordering
|
|---|
| 29 | # - RETURN_IN_ORDER option
|
|---|
| 30 | # - GNU extension with '-' as first character of option string
|
|---|
| 31 | # - optional arguments, specified by double colons
|
|---|
| 32 | # - a option string with a W followed by semicolon should
|
|---|
| 33 | # treat "-W foo" as "--foo"
|
|---|
| 34 |
|
|---|
| 35 | __all__ = ["GetoptError","error","getopt","gnu_getopt"]
|
|---|
| 36 |
|
|---|
| 37 | import os
|
|---|
| 38 |
|
|---|
| 39 | class GetoptError(Exception):
|
|---|
| 40 | opt = ''
|
|---|
| 41 | msg = ''
|
|---|
| 42 | def __init__(self, msg, opt=''):
|
|---|
| 43 | self.msg = msg
|
|---|
| 44 | self.opt = opt
|
|---|
| 45 | Exception.__init__(self, msg, opt)
|
|---|
| 46 |
|
|---|
| 47 | def __str__(self):
|
|---|
| 48 | return self.msg
|
|---|
| 49 |
|
|---|
| 50 | error = GetoptError # backward compatibility
|
|---|
| 51 |
|
|---|
| 52 | def getopt(args, shortopts, longopts = []):
|
|---|
| 53 | """getopt(args, options[, long_options]) -> opts, args
|
|---|
| 54 |
|
|---|
| 55 | Parses command line options and parameter list. args is the
|
|---|
| 56 | argument list to be parsed, without the leading reference to the
|
|---|
| 57 | running program. Typically, this means "sys.argv[1:]". shortopts
|
|---|
| 58 | is the string of option letters that the script wants to
|
|---|
| 59 | recognize, with options that require an argument followed by a
|
|---|
| 60 | colon (i.e., the same format that Unix getopt() uses). If
|
|---|
| 61 | specified, longopts is a list of strings with the names of the
|
|---|
| 62 | long options which should be supported. The leading '--'
|
|---|
| 63 | characters should not be included in the option name. Options
|
|---|
| 64 | which require an argument should be followed by an equal sign
|
|---|
| 65 | ('=').
|
|---|
| 66 |
|
|---|
| 67 | The return value consists of two elements: the first is a list of
|
|---|
| 68 | (option, value) pairs; the second is the list of program arguments
|
|---|
| 69 | left after the option list was stripped (this is a trailing slice
|
|---|
| 70 | of the first argument). Each option-and-value pair returned has
|
|---|
| 71 | the option as its first element, prefixed with a hyphen (e.g.,
|
|---|
| 72 | '-x'), and the option argument as its second element, or an empty
|
|---|
| 73 | string if the option has no argument. The options occur in the
|
|---|
|
|---|