where the value of \emph{yourscript} is determined at runtime (normally
from \code{sys.argv{[}0]}).
-% $Id$
+% $Id: intro.txt 413 2004-09-28 00:59:13Z greg $
\subsection{Background\label{optparse-background}}
argument. \code{"foo"} and \code{"bar"} are positional arguments.
-\subsubsection{What are options for?\label{optparse-what-are-options-for?}}
+\subsubsection{What are options for?\label{optparse-what-options-for}}
Options are used to provide extra information to tune or customize the
execution of a program. In case it wasn't clear, options are usually
another, or several files to another directory.
-\subsubsection{What are positional arguments for?\label{optparse-what-are-positional-arguments-for?}}
+\subsubsection{What are positional arguments for?\label{optparse-what-positional-arguments-for}}
Positional arguments are for those pieces of information that your
program absolutely, positively requires to run.
implementation becomes. Too much flexibility has drawbacks as well, of
course; too many options can overwhelm users and make your code much
harder to maintain.
-% $Id$
+% $Id: tao.txt 413 2004-09-28 00:59:13Z greg $
\subsection{Tutorial\label{optparse-tutorial}}
Of these, \member{action} is the most fundamental.
-\subsubsection{Option actions\label{optparse-option-actions}}
+\subsubsection{Understanding option actions\label{optparse-understanding-option-actions}}
Actions tell \module{optparse} what to do when it encounters an option on the
command line. There is a fixed set of actions hard-coded into \module{optparse};
If you don't specify an option action, \module{optparse} defaults to \code{store}.
-\subsubsection{The store action\label{optparse-the-store-action}}
+\subsubsection{The store action\label{optparse-store-action}}
The most common option action is \code{store}, which tells \module{optparse} to take
the next argument (or the remainder of the current argument), ensure
types is covered in section~\ref{optparse-extending}, Extending \module{optparse}.
-\subsubsection{Handling flag (boolean) options\label{optparse-handling-flag-(boolean)-options}}
+\subsubsection{Handling boolean (flag) options\label{optparse-handling-boolean-options}}
Flag options{---}set a variable to true or false when a particular option
is seen{---}are quite common. \module{optparse} supports them with two separate
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
-default values{---}see section~\ref{optparse-default-values}, Default values, below.)
+default values{---}see below.)
When \module{optparse} encounters \code{"-v"} on the command line, it sets
\code{options.verbose} to \code{True}; when it encounters \code{"-q"},
call a specified function
\end{description}
-These are covered in the section~\ref{None}, Reference Guide and section~\ref{None}, Option Callbacks
-documents.
+These are covered in section~\ref{optparse-reference-guide}, Reference Guide and section~\ref{optparse-option-callbacks}, Option Callbacks.
\subsubsection{Default values\label{optparse-default-values}}
\end{itemize}
-\subsubsection{Printing a version string\label{optparse-printing-a-version-string}}
+\subsubsection{Printing a version string\label{optparse-printing-version-string}}
Similar to the brief usage string, \module{optparse} can also print a version string
for your program. You have to supply the string as the \code{version}
\end{verbatim}
-\subsubsection{Error-handling\label{optparse-error-handling}}
+\subsubsection{How \module{optparse} handles errors\label{optparse-how-optik-handles-errors}}
There are two broad classes of errors that \module{optparse} has to worry about:
programmer errors and user errors. Programmer errors are usually
if __name__ == "__main__":
main()
\end{verbatim}
-% $Id$
+% $Id: tutorial.txt 415 2004-09-30 02:26:17Z greg $
\subsection{Reference Guide\label{optparse-reference-guide}}
-\subsubsection{Populating the parser\label{optparse-populating-the-parser}}
+\subsubsection{Populating the parser\label{optparse-populating-parser}}
There are several ways to populate the parser with options. The
preferred way is by using \code{OptionParser.add{\_}option()}, as shown in
-section~\ref{None}, the tutorial section. \method{add{\_}option()} can be called in one of two
+section~\ref{optparse-tutorial}, the tutorial. \method{add{\_}option()} can be called in one of two
ways:
\begin{itemize}
\item {}
options.
-\subsubsection{Option actions\label{optparse-option-actions}}
+\subsubsection{Standard option actions\label{optparse-standard-option-actions}}
The various option actions all have slightly different requirements and
effects. Most actions have several relevant option attributes which you
*args, **kwargs)
\end{verbatim}
-See section~\ref{None}, Option Callbacks for more detail.
+See section~\ref{optparse-option-callbacks}, Option Callbacks for more detail.
\item {}
\member{help}
\end{itemize}
-\subsubsection{Option types\label{optparse-option-types}}
+\subsubsection{Standard option types\label{optparse-standard-option-types}}
\module{optparse} has six built-in option types: \code{string}, \code{int}, \code{long},
\code{choice}, \code{float} and \code{complex}. If you need to add new option
OptionValueError if an invalid string is given.
-\subsubsection{Querying and manipulating your option parser\label{optparse-querying-and-manipulating-your-option-parser}}
+\subsubsection{Querying and manipulating your option parser\label{optparse-querying-manipulating-option-parser}}
Sometimes, it's useful to poke around your option parser and see what's
there. OptionParser provides a couple of methods to help you out:
-n, --noisy be noisy
--dry-run new dry-run option
\end{verbatim}
-% $Id$
+% $Id: reference.txt 415 2004-09-30 02:26:17Z greg $
\subsection{Option Callbacks\label{optparse-option-callbacks}}
\end{itemize}
-\subsubsection{Defining a callback option\label{optparse-defining-a-callback-option}}
+\subsubsection{Defining a callback option\label{optparse-defining-callback-option}}
As always, the easiest way to define a callback option is by using the
\code{parser.add{\_}option()} method. Apart from \member{action}, the only option
\end{description}
-\subsubsection{How callbacks are called\label{optparse-how-callbacks-are-called}}
+\subsubsection{How callbacks are called\label{optparse-how-callbacks-called}}
All callbacks are called as follows:
\begin{verbatim}
\end{description}
-\subsubsection{Error handling\label{optparse-error-handling}}
+\subsubsection{Raising errors in a callback\label{optparse-raising-errors-in-callback}}
The callback function should raise OptionValueError if there are any
problems with the option or its argument(s). \module{optparse} catches this and
figuring out what he did wrong.
-\subsubsection{Callback example 1: trivial callback\label{optparse-callback-example-1:-trivial-callback}}
+\subsubsection{Callback example 1: trivial callback\label{optparse-callback-example-1}}
Here's an example of a callback option that takes no arguments, and
simply records that the option was seen:
Of course, you could do that with the \code{store{\_}true} action.
-\subsubsection{Callback example 2: check option order\label{optparse-callback-example-2:-check-option-order}}
+\subsubsection{Callback example 2: check option order\label{optparse-callback-example-2}}
Here's a slightly more interesting example: record the fact that
\code{"-a"} is seen, but blow up if it comes after \code{"-b"} in the
\end{verbatim}
-\subsubsection{Callback example 3: check option order (generalized)\label{optparse-callback-example-3:-check-option-order-(generalized)}}
+\subsubsection{Callback example 3: check option order (generalized)\label{optparse-callback-example-3}}
If you want to re-use this callback for several similar options (set a
flag, but blow up if \code{"-b"} has already been seen), it needs a bit of
\end{verbatim}
-\subsubsection{Callback example 4: check arbitrary condition\label{optparse-callback-example-4:-check-arbitrary-condition}}
+\subsubsection{Callback example 4: check arbitrary condition\label{optparse-callback-example-4}}
Of course, you could put any condition in there{---}you're not limited
to checking the values of already-defined options. For example, if
reader.)
-\subsubsection{Callback example 5: fixed arguments\label{optparse-callback-example-5:-fixed-arguments}}
+\subsubsection{Callback example 5: fixed arguments\label{optparse-callback-example-5}}
Things get slightly more interesting when you define callback options
that take a fixed number of arguments. Specifying that a callback
obviously you don't need a callback for this example.)
-\subsubsection{Callback example 6: variable arguments\label{optparse-callback-example-6:-variable-arguments}}
+\subsubsection{Callback example 6: variable arguments\label{optparse-callback-example-6}}
Things get hairy when you want an option to take a variable number of
arguments. For this case, you must write a callback, as \module{optparse} doesn't
numbers in the arguments following \code{"-c"} will be interpreted as
further options (probably causing an error), rather than as arguments to
\code{"-c"}. Fixing this is left as an exercise for the reader.
-% $Id$
+% $Id: callbacks.txt 415 2004-09-30 02:26:17Z greg $