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
5e3be7a6
Commit
5e3be7a6
authored
May 03, 2003
by
Greg Ward
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use TeX quotes -- ``foo'' -- as appropriate.
Remove whitespace around em-dashes.
parent
ade9205b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
39 additions
and
39 deletions
+39
-39
Doc/lib/liboptparse.tex
Doc/lib/liboptparse.tex
+39
-39
No files found.
Doc/lib/liboptparse.tex
View file @
5e3be7a6
...
@@ -33,7 +33,7 @@ parser.add_option("-q", "--quiet",
...
@@ -33,7 +33,7 @@ parser.add_option("-q", "--quiet",
\end{verbatim}
\end{verbatim}
With these few lines of code, users of your script can now do the
With these few lines of code, users of your script can now do the
"usual thing"
on the command-line:
``usual thing''
on the command-line:
\begin{verbatim}
\begin{verbatim}
$
<yourscript>
-
f outfile
--
quiet
$
<yourscript>
-
f outfile
--
quiet
...
@@ -188,7 +188,7 @@ execution of a program. In case it wasn't clear, options are usually
...
@@ -188,7 +188,7 @@ execution of a program. In case it wasn't clear, options are usually
options whatsoever. (Pick a random program from the
\UNIX
{}
or GNU
options whatsoever. (Pick a random program from the
\UNIX
{}
or GNU
toolsets. Can it run without any options at all and still make sense?
toolsets. Can it run without any options at all and still make sense?
The only exceptions I can think of are
\program
{
find
}
,
\program
{
tar
}
,
The only exceptions I can think of are
\program
{
find
}
,
\program
{
tar
}
,
and
\program
{
dd
}
---
all of which are mutant oddballs that have been
and
\program
{
dd
}
---
all of which are mutant oddballs that have been
rightly criticized for their non-standard syntax and confusing
rightly criticized for their non-standard syntax and confusing
interfaces.)
interfaces.)
...
@@ -228,18 +228,18 @@ positively requires to run.
...
@@ -228,18 +228,18 @@ positively requires to run.
A good user interface should have as few absolute requirements as
A good user interface should have as few absolute requirements as
possible. If your program requires 17 distinct pieces of information in
possible. If your program requires 17 distinct pieces of information in
order to run successfully, it doesn't much matter
\emph
{
how
}
you get that
order to run successfully, it doesn't much matter
\emph
{
how
}
you get that
information from the user
---
most people will give up and walk away
information from the user
---
most people will give up and walk away
before they successfully run the program. This applies whether the user
before they successfully run the program. This applies whether the user
interface is a command-line, a configuration file, a GUI, or whatever:
interface is a command-line, a configuration file, a GUI, or whatever:
if you make that many demands on your users, most of them will just give
if you make that many demands on your users, most of them will just give
up.
up.
In short, try to minimize the amount of information that users are
In short, try to minimize the amount of information that users are
absolutely required to supply
---
use sensible defaults whenever
absolutely required to supply
---
use sensible defaults whenever
possible. Of course, you also want to make your programs reasonably
possible. Of course, you also want to make your programs reasonably
flexible. That's what options are for. Again, it doesn't matter if
flexible. That's what options are for. Again, it doesn't matter if
they are entries in a config file, checkboxes in the ``Preferences''
they are entries in a config file, checkboxes in the ``Preferences''
dialog of a GUI, or command-line options
---
the more options you
dialog of a GUI, or command-line options
---
the more options you
implement, the more flexible your program is, and the more complicated
implement, the more flexible your program is, and the more complicated
its implementation becomes. It's quite easy to overwhelm users (and
its implementation becomes. It's quite easy to overwhelm users (and
yourself!) with too much flexibility, so be careful there.
yourself!) with too much flexibility, so be careful there.
...
@@ -281,7 +281,7 @@ strings. In this document, we'll only cover four of the things you
...
@@ -281,7 +281,7 @@ strings. In this document, we'll only cover four of the things you
can put there:
\var
{
action
}
,
\var
{
type
}
,
\var
{
dest
}
(destination), and
can put there:
\var
{
action
}
,
\var
{
type
}
,
\var
{
dest
}
(destination), and
\var
{
help
}
.
\var
{
help
}
.
\subsubsection
{
The
"store"
action
\label
{
optparse-store-action
}}
\subsubsection
{
The
\var
{
store
}
action
\label
{
optparse-store-action
}}
The action tells
\module
{
optparse
}
what to do when it sees one of the
The action tells
\module
{
optparse
}
what to do when it sees one of the
option strings for this option on the command-line. For example, the
option strings for this option on the command-line. For example, the
...
@@ -289,7 +289,7 @@ action \var{store} means: take the next argument (or the remainder of
...
@@ -289,7 +289,7 @@ action \var{store} means: take the next argument (or the remainder of
the current argument), ensure that it is of the correct type, and
the current argument), ensure that it is of the correct type, and
store it to your chosen destination.
store it to your chosen destination.
For example, let's fill in the
"..."
of that last option:
For example, let's fill in the
``...''
of that last option:
\begin{verbatim}
\begin{verbatim}
parser.add
_
option("-f", "--file",
parser.add
_
option("-f", "--file",
...
@@ -308,7 +308,7 @@ args = ["-f", "foo.txt"]
...
@@ -308,7 +308,7 @@ args = ["-f", "foo.txt"]
\function
{
parse
_
args()
}
, it automatically uses
\var
{
sys.argv[1:]
}
.)
\function
{
parse
_
args()
}
, it automatically uses
\var
{
sys.argv[1:]
}
.)
When
\module
{
optparse
}
sees the
\programopt
{
-f
}
, it sucks in the next
When
\module
{
optparse
}
sees the
\programopt
{
-f
}
, it sucks in the next
argument
--- ``foo.txt'' ---
and stores it in the
\var
{
filename
}
argument
---
\code
{
foo.txt
}
---
and stores it in the
\var
{
filename
}
attribute of a special object. That object is the first return value
attribute of a special object. That object is the first return value
from
\function
{
parse
_
args()
}
, so:
from
\function
{
parse
_
args()
}
, so:
...
@@ -316,20 +316,20 @@ from \function{parse_args()}, so:
...
@@ -316,20 +316,20 @@ from \function{parse_args()}, so:
print options.filename
print options.filename
\end{verbatim}
\end{verbatim}
will print
``foo.txt''
.
will print
\code
{
foo.txt
}
.
Other option types supported by
\module
{
optparse
}
are
``int''
and
Other option types supported by
\module
{
optparse
}
are
\code
{
int
}
and
``float''
. Here's an option that expects an integer argument:
\code
{
float
}
. Here's an option that expects an integer argument:
\begin{verbatim}
\begin{verbatim}
parser.add
_
option("-n", type="int", dest="num")
parser.add
_
option("-n", type="int", dest="num")
\end{verbatim}
\end{verbatim}
Note that I didn't supply a long option, which is perfectly acceptable.
Note that I didn't supply a long option, which is perfectly acceptable.
I also didn't specify the action
---
it defaults to ``store''.
I also didn't specify the action
---
it defaults to ``store''.
Let's parse another fake command-line. This time, we'll jam the
Let's parse another fake command-line. This time, we'll jam the
option argument right up against the option
---
\programopt
{
-n42
}
(one
option argument right up against the option
---
\programopt
{
-n42
}
(one
argument) is equivalent to
\programopt
{
-n 42
}
(two arguments). :
argument) is equivalent to
\programopt
{
-n 42
}
(two arguments). :
\begin{verbatim}
\begin{verbatim}
...
@@ -359,10 +359,10 @@ destination for \programopt{-f} is \var{f}.
...
@@ -359,10 +359,10 @@ destination for \programopt{-f} is \var{f}.
Adding types is fairly easy; please refer to
Adding types is fairly easy; please refer to
section~
\ref
{
optparse-adding-types
}
, ``Adding new types.''
section~
\ref
{
optparse-adding-types
}
, ``Adding new types.''
\subsubsection
{
Other
"store
_
*"
actions
\label
{
optparse-other-store-actions
}}
\subsubsection
{
Other
\var
{
store
_
*
}
actions
\label
{
optparse-other-store-actions
}}
Flag options
---
set a variable to true or false when a particular
Flag options
---
set a variable to true or false when a particular
option is seen
---
are quite common.
\module
{
optparse
}
supports them
option is seen
---
are quite common.
\module
{
optparse
}
supports them
with two separate actions, ``store
_
true'' and ``store
_
false''. For
with two separate actions, ``store
_
true'' and ``store
_
false''. For
example, you might have a
\var
{
verbose
}
flag that is turned on with
example, you might have a
\var
{
verbose
}
flag that is turned on with
\programopt
{
-v
}
and off with
\programopt
{
-q
}
:
\programopt
{
-v
}
and off with
\programopt
{
-q
}
:
...
@@ -374,7 +374,7 @@ parser.add_option("-q", action="store_false", dest="verbose")
...
@@ -374,7 +374,7 @@ parser.add_option("-q", action="store_false", dest="verbose")
Here we have two different options with the same destination, which is
Here we have two different options with the same destination, which is
perfectly OK. (It just means you have to be a bit careful when setting
perfectly OK. (It just means you have to be a bit careful when setting
default values
---
see below.)
default values
---
see below.)
When
\module
{
optparse
}
sees
\programopt
{
-v
}
on the command line, it
When
\module
{
optparse
}
sees
\programopt
{
-v
}
on the command line, it
sets the
\var
{
verbose
}
attribute of the special
{
option values
}
sets the
\var
{
verbose
}
attribute of the special
{
option values
}
...
@@ -474,16 +474,16 @@ best possible help message:
...
@@ -474,16 +474,16 @@ best possible help message:
usage = "usage:
%prog [options] arg1 arg2"
usage = "usage:
%prog [options] arg1 arg2"
\end{verbatim}
\end{verbatim}
\module
{
optparse
}
expands
"
\%
prog"
in the usage string to the name of the
\module
{
optparse
}
expands
\samp
{
\%
prog
}
in the usage string to the name of the
current script, ie.
\code
{
os.path.basename(sys.argv[0])
}
. The
current script, ie.
\code
{
os.path.basename(sys.argv[0])
}
. The
expanded string is then printed before the detailed option help.
expanded string is then printed before the detailed option help.
If you don't supply a usage string,
\module
{
optparse
}
uses a bland but
If you don't supply a usage string,
\module
{
optparse
}
uses a bland but
sensible default:
``usage:
\%
prog [options]''
, which is fine if your
sensible default:
\code
{
"usage:
\%
prog [options]"
}
, which is fine if your
script doesn't take any positional arguments.
script doesn't take any positional arguments.
\item
every option defines a help string, and doesn't worry about
\item
every option defines a help string, and doesn't worry about
line-wrapping
---
\module
{
optparse
}
takes care of wrapping lines and
line-wrapping
---
\module
{
optparse
}
takes care of wrapping lines and
making the help output look good.
making the help output look good.
\item
options that take a value indicate this fact in their
\item
options that take a value indicate this fact in their
...
@@ -497,7 +497,7 @@ Here, ``MODE'' is called the meta-variable: it stands for the argument
...
@@ -497,7 +497,7 @@ Here, ``MODE'' is called the meta-variable: it stands for the argument
that the user is expected to supply to
that the user is expected to supply to
\programopt
{
-m
}
/
\longprogramopt
{
mode
}
. By default,
\module
{
optparse
}
\programopt
{
-m
}
/
\longprogramopt
{
mode
}
. By default,
\module
{
optparse
}
converts the destination variable name to uppercase and uses that for
converts the destination variable name to uppercase and uses that for
the meta-variable. Sometimes, that's not what you want
---
for
the meta-variable. Sometimes, that's not what you want
---
for
example, the
\var
{
filename
}
option explicitly sets
example, the
\var
{
filename
}
option explicitly sets
\code
{
metavar="FILE"
}
, resulting in this automatically-generated
\code
{
metavar="FILE"
}
, resulting in this automatically-generated
option description:
option description:
...
@@ -578,7 +578,7 @@ $
...
@@ -578,7 +578,7 @@ $
The one thing you need to know for basic usage is how
The one thing you need to know for basic usage is how
\module
{
optparse
}
behaves when it encounters an error on the
\module
{
optparse
}
behaves when it encounters an error on the
command-line
---
e.g.
\programopt
{
-n4x
}
where
\programopt
{
-n
}
is an
command-line
---
e.g.
\programopt
{
-n4x
}
where
\programopt
{
-n
}
is an
integer-valued option.
\module
{
optparse
}
prints your usage message to
integer-valued option.
\module
{
optparse
}
prints your usage message to
stderr, followed by a useful and human-readable error message. Then
stderr, followed by a useful and human-readable error message. Then
it terminates (calls
\function
{
sys.exit()
}
) with a non-zero exit
it terminates (calls
\function
{
sys.exit()
}
) with a non-zero exit
...
@@ -673,7 +673,7 @@ parser.add_option("-q", "--quiet",
...
@@ -673,7 +673,7 @@ parser.add_option("-q", "--quiet",
This method makes it easier to track down exceptions raised by the
This method makes it easier to track down exceptions raised by the
\class
{
Option
}
constructor, which are common because of the complicated
\class
{
Option
}
constructor, which are common because of the complicated
interdependencies among the various keyword arguments
---
if you get it
interdependencies among the various keyword arguments
---
if you get it
wrong,
\module
{
optparse
}
raises
\exception
{
OptionError
}
.
wrong,
\module
{
optparse
}
raises
\exception
{
OptionError
}
.
\method
{
add
_
option()
}
can be called in one of two ways:
\method
{
add
_
option()
}
can be called in one of two ways:
...
@@ -1084,7 +1084,7 @@ parser.add_option("-n", "--noisy", ...)
...
@@ -1084,7 +1084,7 @@ parser.add_option("-n", "--noisy", ...)
On the assumption that this is usually a mistake,
\module
{
optparse
}
On the assumption that this is usually a mistake,
\module
{
optparse
}
raises an exception (
\exception
{
OptionConflictError
}
) by default when
raises an exception (
\exception
{
OptionConflictError
}
) by default when
this happens. Since this is an easily-fixed programming error, you
this happens. Since this is an easily-fixed programming error, you
shouldn't try to catch this exception
---
fix your mistake and get on
shouldn't try to catch this exception
---
fix your mistake and get on
with life.
with life.
Sometimes, you want newer options to deliberately replace the option
Sometimes, you want newer options to deliberately replace the option
...
@@ -1185,7 +1185,7 @@ to call:
...
@@ -1185,7 +1185,7 @@ to call:
parser.add
_
option("-c", callback=my
_
callback)
parser.add
_
option("-c", callback=my
_
callback)
\end{verbatim}
\end{verbatim}
Note that you supply a function object here
---
so you must have
Note that you supply a function object here
---
so you must have
already defined a function
\function
{
my
_
callback()
}
when you define
already defined a function
\function
{
my
_
callback()
}
when you define
the callback option. In this simple case,
\module
{
optparse
}
knows
the callback option. In this simple case,
\module
{
optparse
}
knows
nothing about the arguments the
\programopt
{
-c
}
option expects to
nothing about the arguments the
\programopt
{
-c
}
option expects to
...
@@ -1237,7 +1237,7 @@ is the \class{Option} instance that's calling the callback.
...
@@ -1237,7 +1237,7 @@ is the \class{Option} instance that's calling the callback.
\term
{
opt
}
\term
{
opt
}
is the option string seen on the command-line that's triggering the
is the option string seen on the command-line that's triggering the
callback. (If an abbreviated long option was used,
\var
{
opt
}
will be
callback. (If an abbreviated long option was used,
\var
{
opt
}
will be
the full, canonical option string
---
e.g. if the user puts
the full, canonical option string
---
e.g. if the user puts
\longprogramopt
{
foo
}
on the command-line as an abbreviation for
\longprogramopt
{
foo
}
on the command-line as an abbreviation for
\longprogramopt
{
foobar
}
, then
\var
{
opt
}
will be
\longprogramopt
{
foobar
}
, then
\var
{
opt
}
will be
\longprogramopt
{
foobar
}
.)
\longprogramopt
{
foobar
}
.)
...
@@ -1348,7 +1348,7 @@ parser.add_option("-b", action="store_true", dest="b")
...
@@ -1348,7 +1348,7 @@ parser.add_option("-b", action="store_true", dest="b")
parser.add
_
option("-c", action="callback", callback=check
_
order, dest='c')
parser.add
_
option("-c", action="callback", callback=check
_
order, dest='c')
\end{verbatim}
\end{verbatim}
Of course, you could put any condition in there
---
you're not limited
Of course, you could put any condition in there
---
you're not limited
to checking the values of already-defined options. For example, if
to checking the values of already-defined options. For example, if
you have options that should not be called when the moon is full, all
you have options that should not be called when the moon is full, all
you have to do is this:
you have to do is this:
...
@@ -1363,7 +1363,7 @@ parser.add_option("--foo",
...
@@ -1363,7 +1363,7 @@ parser.add_option("--foo",
action="callback", callback=check
_
moon, dest="foo")
action="callback", callback=check
_
moon, dest="foo")
\end{verbatim}
\end{verbatim}
(The definition of
is
_
full
_
moon()
is left as an exercise for the
(The definition of
\code
{
is
_
full
_
moon()
}
is left as an exercise for the
reader.)
reader.)
\strong
{
Fixed arguments
}
\strong
{
Fixed arguments
}
...
@@ -1567,20 +1567,20 @@ Adding new actions is a bit trickier, because you have to understand
...
@@ -1567,20 +1567,20 @@ Adding new actions is a bit trickier, because you have to understand
that
\module
{
optparse
}
has a couple of classifications for actions:
that
\module
{
optparse
}
has a couple of classifications for actions:
\begin{definitions}
\begin{definitions}
\term
{
"store"
actions
}
\term
{
``store''
actions
}
actions that result in
\module
{
optparse
}
storing a value to an attribute
actions that result in
\module
{
optparse
}
storing a value to an attribute
of the OptionValues instance; these options require a
'dest'
of the OptionValues instance; these options require a
\var
{
dest
}
attribute to be supplied to the Option constructor
attribute to be supplied to the Option constructor
\term
{
"typed"
actions
}
\term
{
``typed''
actions
}
actions that take a value from the command line and expect it to be
actions that take a value from the command line and expect it to be
of a certain type; or rather, a string that can be converted to a
of a certain type; or rather, a string that can be converted to a
certain type. These options require a
'type'
attribute to the
certain type. These options require a
\var
{
type
}
attribute to the
Option constructor.
Option constructor.
\end{definitions}
\end{definitions}
Some default ``store'' actions are
``store'', ``store
_
const''
,
Some default ``store'' actions are
\var
{
store
}
,
\var
{
store
_
const
}
,
``append'', and ``count''
. The default ``typed'' actions are
\var
{
append
}
, and
\var
{
count
}
. The default ``typed'' actions are
``store'', ``append'', and ``callback''
.
\var
{
store
}
,
\var
{
append
}
, and
\var
{
callback
}
.
When you add an action, you need to decide if it's a ``store'' action,
When you add an action, you need to decide if it's a ``store'' action,
a ``typed'', neither, or both. Three class attributes of
a ``typed'', neither, or both. Three class attributes of
...
@@ -1590,10 +1590,10 @@ a ``typed'', neither, or both. Three class attributes of
...
@@ -1590,10 +1590,10 @@ a ``typed'', neither, or both. Three class attributes of
All actions must be listed as strings in ACTIONS.
All actions must be listed as strings in ACTIONS.
\end{memberdesc}
\end{memberdesc}
\begin{memberdesc}
{
STORE
_
ACTIONS
}
\begin{memberdesc}
{
STORE
_
ACTIONS
}
"store"
actions are additionally listed here.
``store''
actions are additionally listed here.
\end{memberdesc}
\end{memberdesc}
\begin{memberdesc}
{
TYPED
_
ACTIONS
}
\begin{memberdesc}
{
TYPED
_
ACTIONS
}
"typed"
actions are additionally listed here.
``typed''
actions are additionally listed here.
\end{memberdesc}
\end{memberdesc}
In order to actually implement your new action, you must override
In order to actually implement your new action, you must override
...
@@ -1688,8 +1688,8 @@ You'll have to
...
@@ -1688,8 +1688,8 @@ You'll have to
\begin{enumerate}
\begin{enumerate}
\item
subclass OptionParser and override the error() method
\item
subclass OptionParser and override the error() method
\item
subclass Option and override the take
_
action() method
---
you'll
\item
subclass Option and override the take
_
action() method
---
you'll
need to provide your own handling of the
"help"
action that
need to provide your own handling of the
``help''
action that
doesn't call sys.exit()
doesn't call sys.exit()
\end{enumerate}
\end{enumerate}
...
...
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