Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a8e484c

Browse files
committed
getopt(): revise description of long_options parameter slightly so it will
be less confusing; add a paragraph separation so that comments about the options and long_options parameters don't have references that are easily misinterpreted. Adjust the interactive examples to not need the string module. Add an example showing how the module is commonly used in a script.
1 parent f29f47b commit a8e484c

1 file changed

Lines changed: 43 additions & 19 deletions

File tree

Doc/lib/libgetopt.tex

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@ \section{\module{getopt} ---
2121
running program. Typically, this means \samp{sys.argv[1:]}.
2222
\var{options} is the string of option letters that the script wants to
2323
recognize, with options that require an argument followed by a colon
24-
(i.e., the same format that \UNIX{} \cfunction{getopt()} uses). If
25-
specified, \var{long_options} is a list of strings with the names of
26-
the long options which should be supported. The leading
24+
(\character{:}; i.e., the same format that \UNIX{}
25+
\cfunction{getopt()} uses).
26+
27+
\var{long_options}, if specified, must be a list of strings with the
28+
names of the long options which should be supported. The leading
2729
\code{'-}\code{-'} characters should not be included in the option
28-
name. Options which require an argument should be followed by an
29-
equal sign (\code{'='}).
30+
name. Long options which require an argument should be followed by an
31+
equal sign (\character{=}).
3032

3133
The return value consists of two elements: the first is a list of
3234
\code{(\var{option}, \var{value})} pairs; the second is the list of
3335
program arguments left after the option list was stripped (this is a
34-
trailing slice of the first argument).
35-
Each option-and-value pair returned has the option as its first
36-
element, prefixed with a hyphen for short options (e.g., \code{'-x'})
37-
or two hyphens for long options (e.g., \code{'-}\code{-long-option'}),
38-
and the option argument as its second element, or an empty string if
39-
the option has no argument.
40-
The options occur in the list in the same order in which they were
41-
found, thus allowing multiple occurrences. Long and short options may
42-
be mixed.
36+
trailing slice of \var{args}). Each option-and-value pair returned
37+
has the option as its first element, prefixed with a hyphen for short
38+
options (e.g., \code{'-x'}) or two hyphens for long options (e.g.,
39+
\code{'-}\code{-long-option'}), and the option argument as its second
40+
element, or an empty string if the option has no argument. The
41+
options occur in the list in the same order in which they were found,
42+
thus allowing multiple occurrences. Long and short options may be
43+
mixed.
4344
\end{funcdesc}
4445

4546
\begin{excdesc}{GetoptError}
@@ -61,23 +62,22 @@ \section{\module{getopt} ---
6162
An example using only \UNIX{} style options:
6263

6364
\begin{verbatim}
64-
>>> import getopt, string
65-
>>> args = string.split('-a -b -cfoo -d bar a1 a2')
65+
>>> import getopt
66+
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
6667
>>> args
6768
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
6869
>>> optlist, args = getopt.getopt(args, 'abc:d:')
6970
>>> optlist
7071
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
7172
>>> args
7273
['a1', 'a2']
73-
>>>
7474
\end{verbatim}
7575

7676
Using long option names is equally easy:
7777

7878
\begin{verbatim}
7979
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
80-
>>> args = string.split(s)
80+
>>> args = s.split()
8181
>>> args
8282
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
8383
>>> optlist, args = getopt.getopt(args, 'x', [
@@ -87,5 +87,29 @@ \section{\module{getopt} ---
8787
'')]
8888
>>> args
8989
['a1', 'a2']
90-
>>>
90+
\end{verbatim}
91+
92+
In a script, typical usage is something like this:
93+
94+
\begin{verbatim}
95+
import getopt, sys
96+
97+
def main():
98+
try:
99+
opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
100+
except getopt.GetoptError:
101+
# print help information and exit:
102+
usage()
103+
sys.exit(2)
104+
output = None
105+
for o, a in opts:
106+
if o in ("-h", "--help"):
107+
usage()
108+
sys.exit()
109+
if o in ("-o", "--output"):
110+
output = a
111+
# ...
112+
113+
if __name__ == "__main__":
114+
main()
91115
\end{verbatim}

0 commit comments

Comments
 (0)