Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
24d5a528
Commit
24d5a528
authored
Nov 14, 2002
by
Andrew M. Kuchling
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add section on Optik
parent
857fb4c1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
2 deletions
+73
-2
Doc/whatsnew/whatsnew23.tex
Doc/whatsnew/whatsnew23.tex
+73
-2
No files found.
Doc/whatsnew/whatsnew23.tex
View file @
24d5a528
...
...
@@ -1253,9 +1253,80 @@ per-use basis.
%======================================================================
\subsection
{
Optik:
The
\module
{
optparse
}
Module
}
\subsection
{
The
\module
{
optparse
}
Module
}
XXX write this section
The
\module
{
getopt
}
module provides simple parsing of command-line
arguments. The new
\module
{
optparse
}
module (originally named Optik)
provides more elaborate command-line parsing that follows the Unix
conventions, automatically creates the output for
\longprogramopt
{
help
}
,
and can perform different actions
You start by creating an instance of
\class
{
OptionParser
}
and telling
it what your program's options are.
\begin{verbatim}
from optparse import OptionParser
op = OptionParser()
op.add
_
option('-i', '--input',
action='store', type='string', dest='input',
help='set input filename')
op.add
_
option('-l', '--length',
action='store', type='int', dest='length',
help='set maximum length of output')
\end{verbatim}
Parsing a command line is then done by calling the
\method
{
parse
_
args()
}
method.
\begin{verbatim}
options, args = op.parse
_
args(sys.argv[1:])
print options
print args
\end{verbatim}
This returns an object containing all of the option values,
and a list of strings containing the remaining arguments.
Invoking the script with the various arguments now works as you'd
expect it to. Note that the length argument is automatically
converted to an integer.
\begin{verbatim}
$
.
/
python opt.py
-
i data arg
1
<Values at
0
x
400
cad
4
c:
{
'input': 'data', 'length': None
}
>
[
'arg
1
'
]
$
./python opt.py --input=data --length=4
<Values at 0x400cad2c:
{
'input': 'data', 'length': 4
}
>
['arg1']
$
\end
{
verbatim
}
The help message is automatically generated for you:
\begin
{
verbatim
}
$
./python opt.py --help
usage: opt.py [options]
options:
-h, --help show this help message and exit
-iINPUT, --input=INPUT
set input filename
-lLENGTH, --length=LENGTH
set maximum length of output
$
\end
{
verbatim
}
Optik was written by Greg Ward, with suggestions from the readers of
the Getopt SIG.
\begin
{
seealso
}
\seeurl
{
http:
//
optik.sourceforge.net
}
{
The Optik site has tutorial and reference documentation for
\module
{
optparse
}
.
% XXX change to point to Python docs, when those docs get written.
}
\end
{
seealso
}
%======================================================================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment