Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1 | \section{\module{optparse} --- More powerful command line option parser} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{optparse} |
| 3 | \moduleauthor{Greg Ward}{gward@python.net} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 4 | \modulesynopsis{More convenient, flexible, and powerful command-line parsing library.} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 5 | \versionadded{2.3} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 6 | \sectionauthor{Greg Ward}{gward@python.net} |
| 7 | % An intro blurb used only when generating LaTeX docs for the Python |
| 8 | % manual (based on README.txt). |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 9 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 10 | \code{optparse} is a more convenient, flexible, and powerful library for |
| 11 | parsing command-line options than \code{getopt}. \code{optparse} uses a more |
| 12 | declarative style of command-line parsing: you create an instance of |
| 13 | \class{OptionParser}, populate it with options, and parse the command line. |
| 14 | \code{optparse} allows users to specify options in the conventional GNU/POSIX |
| 15 | syntax, and additionally generates usage and help messages for you. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 16 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 17 | Here's an example of using \code{optparse} in a simple script: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 18 | \begin{verbatim} |
| 19 | from optparse import OptionParser |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 20 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 21 | parser = OptionParser() |
| 22 | parser.add_option("-f", "--file", dest="filename", |
| 23 | help="write report to FILE", metavar="FILE") |
| 24 | parser.add_option("-q", "--quiet", |
Greg Ward | 1f53517 | 2003-05-03 20:13:08 +0000 | [diff] [blame] | 25 | action="store_false", dest="verbose", default=True, |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 26 | help="don't print status messages to stdout") |
| 27 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 28 | (options, args) = parser.parse_args() |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 29 | \end{verbatim} |
| 30 | |
| 31 | With these few lines of code, users of your script can now do the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 32 | ``usual thing'' on the command-line, for example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 33 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 34 | <yourscript> --file=outfile -q |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 35 | \end{verbatim} |
| 36 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 37 | As it parses the command line, \code{optparse} sets attributes of the |
| 38 | \var{options} object returned by \method{parse{\_}args()} based on user-supplied |
| 39 | command-line values. When \method{parse{\_}args()} returns from parsing this |
| 40 | command line, \var{options.filename} will be \code{"outfile"} and |
| 41 | \code{options.verbose} will be \code{False}. \code{optparse} supports both long |
| 42 | and short options, allows short options to be merged together, and |
| 43 | allows options to be associated with their arguments in a variety of |
| 44 | ways. Thus, the following command lines are all equivalent to the above |
| 45 | example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 46 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 47 | <yourscript> -f outfile --quiet |
| 48 | <yourscript> --quiet --file outfile |
| 49 | <yourscript> -q -foutfile |
| 50 | <yourscript> -qfoutfile |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 51 | \end{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 52 | |
| 53 | Additionally, users can run one of |
| 54 | \begin{verbatim} |
| 55 | <yourscript> -h |
| 56 | <yourscript> --help |
| 57 | \end{verbatim} |
| 58 | |
| 59 | and \code{optparse} will print out a brief summary of your script's |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 60 | options: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 61 | \begin{verbatim} |
| 62 | usage: <yourscript> [options] |
| 63 | |
| 64 | options: |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 65 | -h, --help show this help message and exit |
| 66 | -f FILE, --file=FILE write report to FILE |
| 67 | -q, --quiet don't print status messages to stdout |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 68 | \end{verbatim} |
| 69 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 70 | where the value of \emph{yourscript} is determined at runtime (normally |
| 71 | from \code{sys.argv{[}0]}). |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 72 | % $Id: intro.txt 413 2004-09-28 00:59:13Z greg $ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 73 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 74 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 75 | \subsection{Background\label{optparse-background}} |
| 76 | |
| 77 | \module{optparse} was explicitly designed to encourage the creation of programs with |
| 78 | straightforward, conventional command-line interfaces. To that end, it |
| 79 | supports only the most common command-line syntax and semantics |
| 80 | conventionally used under \UNIX{}. If you are unfamiliar with these |
| 81 | conventions, read this section to acquaint yourself with them. |
| 82 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 83 | |
| 84 | \subsubsection{Terminology\label{optparse-terminology}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 85 | \begin{description} |
| 86 | \item[argument] |
| 87 | a string entered on the command-line, and passed by the shell to |
| 88 | \code{execl()} or \code{execv()}. In Python, arguments are elements of |
| 89 | \code{sys.argv{[}1:]} (\code{sys.argv{[}0]} is the name of the program being |
| 90 | executed). \UNIX{} shells also use the term ``word''. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 91 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 92 | It is occasionally desirable to substitute an argument list other |
| 93 | than \code{sys.argv{[}1:]}, so you should read ``argument'' as ``an element of |
| 94 | \code{sys.argv{[}1:]}, or of some other list provided as a substitute for |
| 95 | \code{sys.argv{[}1:]}''. |
| 96 | \item[option ] |
| 97 | an argument used to supply extra information to guide or customize the |
| 98 | execution of a program. There are many different syntaxes for |
| 99 | options; the traditional \UNIX{} syntax is a hyphen (``-'') followed by a |
| 100 | single letter, e.g. \code{"-x"} or \code{"-F"}. Also, traditional \UNIX{} |
| 101 | syntax allows multiple options to be merged into a single argument, |
| 102 | e.g. \code{"-x -F"} is equivalent to \code{"-xF"}. The GNU project |
| 103 | introduced \code{"-{}-"} followed by a series of hyphen-separated words, |
| 104 | e.g. \code{"-{}-file"} or \code{"-{}-dry-run"}. These are the only two option |
| 105 | syntaxes provided by \module{optparse}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 106 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 107 | Some other option syntaxes that the world has seen include: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 108 | \begin{itemize} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 109 | \item {} |
| 110 | a hyphen followed by a few letters, e.g. \code{"-pf"} (this is |
| 111 | \emph{not} the same as multiple options merged into a single argument) |
| 112 | |
| 113 | \item {} |
| 114 | a hyphen followed by a whole word, e.g. \code{"-file"} (this is |
| 115 | technically equivalent to the previous syntax, but they aren't |
| 116 | usually seen in the same program) |
| 117 | |
| 118 | \item {} |
| 119 | a plus sign followed by a single letter, or a few letters, |
| 120 | or a word, e.g. \code{"+f"}, \code{"+rgb"} |
| 121 | |
| 122 | \item {} |
| 123 | a slash followed by a letter, or a few letters, or a word, e.g. |
| 124 | \code{"/f"}, \code{"/file"} |
| 125 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 126 | \end{itemize} |
| 127 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 128 | These option syntaxes are not supported by \module{optparse}, and they never will |
| 129 | be. This is deliberate: the first three are non-standard on any |
| 130 | environment, and the last only makes sense if you're exclusively |
| 131 | targeting VMS, MS-DOS, and/or Windows. |
| 132 | \item[option argument] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 133 | an argument that follows an option, is closely associated with that |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 134 | option, and is consumed from the argument list when that option is. |
| 135 | With \module{optparse}, option arguments may either be in a separate argument |
| 136 | from their option: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 137 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 138 | -f foo |
| 139 | --file foo |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 140 | \end{verbatim} |
| 141 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 142 | or included in the same argument: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 143 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 144 | -ffoo |
| 145 | --file=foo |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 146 | \end{verbatim} |
| 147 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 148 | Typically, a given option either takes an argument or it doesn't. |
| 149 | Lots of people want an ``optional option arguments'' feature, meaning |
| 150 | that some options will take an argument if they see it, and won't if |
| 151 | they don't. This is somewhat controversial, because it makes parsing |
| 152 | ambiguous: if \code{"-a"} takes an optional argument and \code{"-b"} is |
| 153 | another option entirely, how do we interpret \code{"-ab"}? Because of |
| 154 | this ambiguity, \module{optparse} does not support this feature. |
| 155 | \item[positional argument] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 156 | something leftover in the argument list after options have been |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 157 | parsed, i.e. after options and their arguments have been parsed and |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 158 | removed from the argument list. |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 159 | \item[required option] |
| 160 | an option that must be supplied on the command-line; note that the |
| 161 | phrase ``required option'' is self-contradictory in English. \module{optparse} |
| 162 | doesn't prevent you from implementing required options, but doesn't |
| 163 | give you much help at it either. See \code{examples/required{\_}1.py} and |
| 164 | \code{examples/required{\_}2.py} in the \module{optparse} source distribution for two |
| 165 | ways to implement required options with \module{optparse}. |
| 166 | \end{description} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 167 | |
| 168 | For example, consider this hypothetical command-line: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 169 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 170 | prog -v --report /tmp/report.txt foo bar |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 171 | \end{verbatim} |
| 172 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 173 | \code{"-v"} and \code{"-{}-report"} are both options. Assuming that |
| 174 | \longprogramopt{report} takes one argument, \code{"/tmp/report.txt"} is an option |
| 175 | argument. \code{"foo"} and \code{"bar"} are positional arguments. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 176 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 177 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 178 | \subsubsection{What are options for?\label{optparse-what-options-for}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 179 | |
| 180 | Options are used to provide extra information to tune or customize the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 181 | execution of a program. In case it wasn't clear, options are usually |
| 182 | \emph{optional}. A program should be able to run just fine with no options |
| 183 | whatsoever. (Pick a random program from the \UNIX{} or GNU toolsets. Can |
| 184 | it run without any options at all and still make sense? The main |
| 185 | exceptions are \code{find}, \code{tar}, and \code{dd}{---}all of which are mutant |
| 186 | oddballs that have been rightly criticized for their non-standard syntax |
| 187 | and confusing interfaces.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 188 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 189 | Lots of people want their programs to have ``required options''. Think |
| 190 | about it. If it's required, then it's \emph{not optional}! If there is a |
| 191 | piece of information that your program absolutely requires in order to |
| 192 | run successfully, that's what positional arguments are for. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 193 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 194 | As an example of good command-line interface design, consider the humble |
| 195 | \code{cp} utility, for copying files. It doesn't make much sense to try to |
| 196 | copy files without supplying a destination and at least one source. |
| 197 | Hence, \code{cp} fails if you run it with no arguments. However, it has a |
| 198 | flexible, useful syntax that does not require any options at all: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 199 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 200 | cp SOURCE DEST |
| 201 | cp SOURCE ... DEST-DIR |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 202 | \end{verbatim} |
| 203 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 204 | You can get pretty far with just that. Most \code{cp} implementations |
| 205 | provide a bunch of options to tweak exactly how the files are copied: |
| 206 | you can preserve mode and modification time, avoid following symlinks, |
| 207 | ask before clobbering existing files, etc. But none of this distracts |
| 208 | from the core mission of \code{cp}, which is to copy either one file to |
| 209 | another, or several files to another directory. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 210 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 211 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 212 | \subsubsection{What are positional arguments for?\label{optparse-what-positional-arguments-for}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 213 | |
| 214 | Positional arguments are for those pieces of information that your |
| 215 | program absolutely, positively requires to run. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 216 | |
| 217 | A good user interface should have as few absolute requirements as |
| 218 | possible. If your program requires 17 distinct pieces of information in |
| 219 | order to run successfully, it doesn't much matter \emph{how} you get that |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 220 | information from the user{---}most people will give up and walk away |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 221 | before they successfully run the program. This applies whether the user |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 222 | interface is a command-line, a configuration file, or a GUI: if you make |
| 223 | that many demands on your users, most of them will simply give up. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 224 | |
| 225 | In short, try to minimize the amount of information that users are |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 226 | absolutely required to supply{---}use sensible defaults whenever |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 227 | possible. Of course, you also want to make your programs reasonably |
| 228 | flexible. That's what options are for. Again, it doesn't matter if |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 229 | they are entries in a config file, widgets in the ``Preferences'' dialog |
| 230 | of a GUI, or command-line options{---}the more options you implement, the |
| 231 | more flexible your program is, and the more complicated its |
| 232 | implementation becomes. Too much flexibility has drawbacks as well, of |
| 233 | course; too many options can overwhelm users and make your code much |
| 234 | harder to maintain. |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 235 | % $Id: tao.txt 413 2004-09-28 00:59:13Z greg $ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 236 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 237 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 238 | \subsection{Tutorial\label{optparse-tutorial}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 239 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 240 | While \module{optparse} is quite flexible and powerful, it's also straightforward to |
| 241 | use in most cases. This section covers the code patterns that are |
| 242 | common to any \module{optparse}-based program. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 243 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 244 | First, you need to import the OptionParser class; then, early in the |
| 245 | main program, create an OptionParser instance: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 246 | \begin{verbatim} |
| 247 | from optparse import OptionParser |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 248 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 249 | parser = OptionParser() |
| 250 | \end{verbatim} |
| 251 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 252 | Then you can start defining options. The basic syntax is: |
| 253 | \begin{verbatim} |
| 254 | parser.add_option(opt_str, ..., |
| 255 | attr=value, ...) |
| 256 | \end{verbatim} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 257 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 258 | Each option has one or more option strings, such as \code{"-f"} or |
| 259 | \code{"-{}-file"}, and several option attributes that tell \module{optparse} what to |
| 260 | expect and what to do when it encounters that option on the command |
| 261 | line. |
| 262 | |
| 263 | Typically, each option will have one short option string and one long |
| 264 | option string, e.g.: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 265 | \begin{verbatim} |
| 266 | parser.add_option("-f", "--file", ...) |
| 267 | \end{verbatim} |
| 268 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 269 | You're free to define as many short option strings and as many long |
| 270 | option strings as you like (including zero), as long as there is at |
| 271 | least one option string overall. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 272 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 273 | The option strings passed to \method{add{\_}option()} are effectively labels for |
| 274 | the option defined by that call. For brevity, we will frequently refer |
| 275 | to \emph{encountering an option} on the command line; in reality, \module{optparse} |
| 276 | encounters \emph{option strings} and looks up options from them. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 277 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 278 | Once all of your options are defined, instruct \module{optparse} to parse your |
| 279 | program's command line: |
| 280 | \begin{verbatim} |
| 281 | (options, args) = parser.parse_args() |
| 282 | \end{verbatim} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 283 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 284 | (If you like, you can pass a custom argument list to \method{parse{\_}args()}, |
| 285 | but that's rarely necessary: by default it uses \code{sys.argv{[}1:]}.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 286 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 287 | \method{parse{\_}args()} returns two values: |
| 288 | \begin{itemize} |
| 289 | \item {} |
| 290 | \var{options}, an object containing values for all of your options{---}e.g. if \code{"-{}-file"} takes a single string argument, then |
| 291 | \var{options.file} will be the filename supplied by the user, or |
| 292 | \code{None} if the user did not supply that option |
| 293 | |
| 294 | \item {} |
| 295 | \var{args}, the list of positional arguments leftover after parsing |
| 296 | options |
| 297 | |
| 298 | \end{itemize} |
| 299 | |
| 300 | This tutorial section only covers the four most important option |
| 301 | attributes: \member{action}, \member{type}, \member{dest} (destination), and \member{help}. |
| 302 | Of these, \member{action} is the most fundamental. |
| 303 | |
| 304 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 305 | \subsubsection{Understanding option actions\label{optparse-understanding-option-actions}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 306 | |
| 307 | Actions tell \module{optparse} what to do when it encounters an option on the |
| 308 | command line. There is a fixed set of actions hard-coded into \module{optparse}; |
| 309 | adding new actions is an advanced topic covered in section~\ref{optparse-extending}, Extending \module{optparse}. |
| 310 | Most actions tell \module{optparse} to store a value in some variable{---}for |
| 311 | example, take a string from the command line and store it in an |
| 312 | attribute of \var{options}. |
| 313 | |
| 314 | If you don't specify an option action, \module{optparse} defaults to \code{store}. |
| 315 | |
| 316 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 317 | \subsubsection{The store action\label{optparse-store-action}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 318 | |
| 319 | The most common option action is \code{store}, which tells \module{optparse} to take |
| 320 | the next argument (or the remainder of the current argument), ensure |
| 321 | that it is of the correct type, and store it to your chosen destination. |
| 322 | |
| 323 | For example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 324 | \begin{verbatim} |
| 325 | parser.add_option("-f", "--file", |
| 326 | action="store", type="string", dest="filename") |
| 327 | \end{verbatim} |
| 328 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 329 | Now let's make up a fake command line and ask \module{optparse} to parse it: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 330 | \begin{verbatim} |
| 331 | args = ["-f", "foo.txt"] |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 332 | (options, args) = parser.parse_args(args) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 333 | \end{verbatim} |
| 334 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 335 | When \module{optparse} sees the option string \code{"-f"}, it consumes the next |
| 336 | argument, \code{"foo.txt"}, and stores it in \var{options.filename}. So, |
| 337 | after this call to \method{parse{\_}args()}, \var{options.filename} is |
| 338 | \code{"foo.txt"}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 339 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 340 | Some other option types supported by \module{optparse} are \code{int} and \code{float}. |
| 341 | Here's an option that expects an integer argument: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 342 | \begin{verbatim} |
| 343 | parser.add_option("-n", type="int", dest="num") |
| 344 | \end{verbatim} |
| 345 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 346 | Note that this option has no long option string, which is perfectly |
| 347 | acceptable. Also, there's no explicit action, since the default is |
| 348 | \code{store}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 349 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 350 | Let's parse another fake command-line. This time, we'll jam the option |
| 351 | argument right up against the option: since \code{"-n42"} (one argument) is |
| 352 | equivalent to \code{"-n 42"} (two arguments), the code |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 353 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 354 | (options, args) = parser.parse_args(["-n42"]) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 355 | print options.num |
| 356 | \end{verbatim} |
| 357 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 358 | will print \code{"42"}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 359 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 360 | If you don't specify a type, \module{optparse} assumes \code{string}. Combined with the |
| 361 | fact that the default action is \code{store}, that means our first example |
| 362 | can be a lot shorter: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 363 | \begin{verbatim} |
| 364 | parser.add_option("-f", "--file", dest="filename") |
| 365 | \end{verbatim} |
| 366 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 367 | If you don't supply a destination, \module{optparse} figures out a sensible default |
| 368 | from the option strings: if the first long option string is |
| 369 | \code{"-{}-foo-bar"}, then the default destination is \code{foo{\_}bar}. If there |
| 370 | are no long option strings, \module{optparse} looks at the first short option |
| 371 | string: the default destination for \code{"-f"} is \code{f}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 372 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 373 | \module{optparse} also includes built-in \code{long} and \code{complex} types. Adding |
| 374 | types is covered in section~\ref{optparse-extending}, Extending \module{optparse}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 375 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 376 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 377 | \subsubsection{Handling boolean (flag) options\label{optparse-handling-boolean-options}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 378 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 379 | Flag options{---}set a variable to true or false when a particular option |
| 380 | is seen{---}are quite common. \module{optparse} supports them with two separate |
| 381 | actions, \code{store{\_}true} and \code{store{\_}false}. For example, you might have a |
| 382 | \var{verbose} flag that is turned on with \code{"-v"} and off with \code{"-q"}: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 383 | \begin{verbatim} |
| 384 | parser.add_option("-v", action="store_true", dest="verbose") |
| 385 | parser.add_option("-q", action="store_false", dest="verbose") |
| 386 | \end{verbatim} |
| 387 | |
| 388 | Here we have two different options with the same destination, which is |
| 389 | perfectly OK. (It just means you have to be a bit careful when setting |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 390 | default values{---}see below.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 391 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 392 | When \module{optparse} encounters \code{"-v"} on the command line, it sets |
| 393 | \code{options.verbose} to \code{True}; when it encounters \code{"-q"}, |
| 394 | \code{options.verbose} is set to \code{False}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 395 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 396 | |
| 397 | \subsubsection{Other actions\label{optparse-other-actions}} |
| 398 | |
| 399 | Some other actions supported by \module{optparse} are: |
| 400 | \begin{description} |
| 401 | \item[\code{store{\_}const}] |
| 402 | store a constant value |
| 403 | \item[\code{append}] |
| 404 | append this option's argument to a list |
| 405 | \item[\code{count}] |
| 406 | increment a counter by one |
| 407 | \item[\code{callback}] |
| 408 | call a specified function |
| 409 | \end{description} |
| 410 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 411 | These are covered in section~\ref{optparse-reference-guide}, Reference Guide and section~\ref{optparse-option-callbacks}, Option Callbacks. |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 412 | |
| 413 | |
| 414 | \subsubsection{Default values\label{optparse-default-values}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 415 | |
| 416 | All of the above examples involve setting some variable (the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 417 | ``destination'') when certain command-line options are seen. What happens |
| 418 | if those options are never seen? Since we didn't supply any defaults, |
| 419 | they are all set to \code{None}. This is usually fine, but sometimes you |
| 420 | want more control. \module{optparse} lets you supply a default value for each |
| 421 | destination, which is assigned before the command line is parsed. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 422 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 423 | First, consider the verbose/quiet example. If we want \module{optparse} to set |
| 424 | \var{verbose} to \code{True} unless \code{"-q"} is seen, then we can do this: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 425 | \begin{verbatim} |
Greg Ward | 1f53517 | 2003-05-03 20:13:08 +0000 | [diff] [blame] | 426 | parser.add_option("-v", action="store_true", dest="verbose", default=True) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 427 | parser.add_option("-q", action="store_false", dest="verbose") |
| 428 | \end{verbatim} |
| 429 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 430 | Since default values apply to the \emph{destination} rather than to any |
| 431 | particular option, and these two options happen to have the same |
| 432 | destination, this is exactly equivalent: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 433 | \begin{verbatim} |
| 434 | parser.add_option("-v", action="store_true", dest="verbose") |
Greg Ward | 1f53517 | 2003-05-03 20:13:08 +0000 | [diff] [blame] | 435 | parser.add_option("-q", action="store_false", dest="verbose", default=True) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 436 | \end{verbatim} |
| 437 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 438 | Consider this: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 439 | \begin{verbatim} |
Greg Ward | 1f53517 | 2003-05-03 20:13:08 +0000 | [diff] [blame] | 440 | parser.add_option("-v", action="store_true", dest="verbose", default=False) |
| 441 | parser.add_option("-q", action="store_false", dest="verbose", default=True) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 442 | \end{verbatim} |
| 443 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 444 | Again, the default value for \var{verbose} will be \code{True}: the last |
Greg Ward | d723128 | 2003-05-03 21:22:58 +0000 | [diff] [blame] | 445 | default value supplied for any particular destination is the one that |
| 446 | counts. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 447 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 448 | A clearer way to specify default values is the \method{set{\_}defaults()} |
| 449 | method of OptionParser, which you can call at any time before calling |
| 450 | \method{parse{\_}args()}: |
| 451 | \begin{verbatim} |
| 452 | parser.set_defaults(verbose=True) |
| 453 | parser.add_option(...) |
| 454 | (options, args) = parser.parse_args() |
| 455 | \end{verbatim} |
| 456 | |
| 457 | As before, the last value specified for a given option destination is |
| 458 | the one that counts. For clarity, try to use one method or the other of |
| 459 | setting default values, not both. |
| 460 | |
| 461 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 462 | \subsubsection{Generating help\label{optparse-generating-help}} |
| 463 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 464 | \module{optparse}'s ability to generate help and usage text automatically is useful |
| 465 | for creating user-friendly command-line interfaces. All you have to do |
| 466 | is supply a \member{help} value for each option, and optionally a short usage |
| 467 | message for your whole program. Here's an OptionParser populated with |
| 468 | user-friendly (documented) options: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 469 | \begin{verbatim} |
| 470 | usage = "usage: %prog [options] arg1 arg2" |
| 471 | parser = OptionParser(usage=usage) |
| 472 | parser.add_option("-v", "--verbose", |
Greg Ward | 1f53517 | 2003-05-03 20:13:08 +0000 | [diff] [blame] | 473 | action="store_true", dest="verbose", default=True, |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 474 | help="make lots of noise [default]") |
| 475 | parser.add_option("-q", "--quiet", |
| 476 | action="store_false", dest="verbose", |
| 477 | help="be vewwy quiet (I'm hunting wabbits)") |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 478 | parser.add_option("-f", "--filename", |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 479 | metavar="FILE", help="write output to FILE"), |
| 480 | parser.add_option("-m", "--mode", |
| 481 | default="intermediate", |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 482 | help="interaction mode: novice, intermediate, " |
| 483 | "or expert [default: %default]") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 484 | \end{verbatim} |
| 485 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 486 | If \module{optparse} encounters either \code{"-h"} or \code{"-{}-help"} on the command-line, |
| 487 | or if you just call \method{parser.print{\_}help()}, it prints the following to |
| 488 | standard output: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 489 | \begin{verbatim} |
| 490 | usage: <yourscript> [options] arg1 arg2 |
| 491 | |
| 492 | options: |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 493 | -h, --help show this help message and exit |
| 494 | -v, --verbose make lots of noise [default] |
| 495 | -q, --quiet be vewwy quiet (I'm hunting wabbits) |
| 496 | -f FILE, --filename=FILE |
| 497 | write output to FILE |
| 498 | -m MODE, --mode=MODE interaction mode: novice, intermediate, or |
| 499 | expert [default: intermediate] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 500 | \end{verbatim} |
| 501 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 502 | (If the help output is triggered by a help option, \module{optparse} exits after |
| 503 | printing the help text.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 504 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 505 | There's a lot going on here to help \module{optparse} generate the best possible |
| 506 | help message: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 507 | \begin{itemize} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 508 | \item {} |
| 509 | the script defines its own usage message: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 510 | \begin{verbatim} |
| 511 | usage = "usage: %prog [options] arg1 arg2" |
| 512 | \end{verbatim} |
| 513 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 514 | \module{optparse} expands \code{"{\%}prog"} in the usage string to the name of the current |
| 515 | program, i.e. \code{os.path.basename(sys.argv{[}0])}. The expanded string |
| 516 | is then printed before the detailed option help. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 517 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 518 | If you don't supply a usage string, \module{optparse} uses a bland but sensible |
| 519 | default: ``\code{usage: {\%}prog {[}options]"}, which is fine if your script |
| 520 | doesn't take any positional arguments. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 521 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 522 | \item {} |
| 523 | every option defines a help string, and doesn't worry about line- |
| 524 | wrapping{---}\module{optparse} takes care of wrapping lines and making the |
| 525 | help output look good. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 526 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 527 | \item {} |
| 528 | options that take a value indicate this fact in their |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 529 | automatically-generated help message, e.g. for the ``mode'' option: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 530 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 531 | -m MODE, --mode=MODE |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 532 | \end{verbatim} |
| 533 | |
| 534 | Here, ``MODE'' is called the meta-variable: it stands for the argument |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 535 | that the user is expected to supply to \programopt{-m}/\longprogramopt{mode}. By default, |
| 536 | \module{optparse} converts the destination variable name to uppercase and uses |
| 537 | that for the meta-variable. Sometimes, that's not what you want{---}for example, the \longprogramopt{filename} option explicitly sets |
| 538 | \code{metavar="FILE"}, resulting in this automatically-generated option |
| 539 | description: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 540 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 541 | -f FILE, --filename=FILE |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 542 | \end{verbatim} |
| 543 | |
| 544 | This is important for more than just saving space, though: the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 545 | manually written help text uses the meta-variable ``FILE'' to clue the |
| 546 | user in that there's a connection between the semi-formal syntax ``-f |
| 547 | FILE'' and the informal semantic description ``write output to FILE''. |
| 548 | This is a simple but effective way to make your help text a lot |
| 549 | clearer and more useful for end users. |
| 550 | |
| 551 | \item {} |
| 552 | options that have a default value can include \code{{\%}default} in |
| 553 | the help string{---}\module{optparse} will replace it with \function{str()} of the |
| 554 | option's default value. If an option has no default value (or the |
| 555 | default value is \code{None}), \code{{\%}default} expands to \code{none}. |
| 556 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 557 | \end{itemize} |
| 558 | |
Fred Drake | cf6d74a | 2003-04-18 15:50:13 +0000 | [diff] [blame] | 559 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 560 | \subsubsection{Printing a version string\label{optparse-printing-version-string}} |
Fred Drake | cf6d74a | 2003-04-18 15:50:13 +0000 | [diff] [blame] | 561 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 562 | Similar to the brief usage string, \module{optparse} can also print a version string |
| 563 | for your program. You have to supply the string as the \code{version} |
| 564 | argument to OptionParser: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 565 | \begin{verbatim} |
| 566 | parser = OptionParser(usage="%prog [-f] [-q]", version="%prog 1.0") |
| 567 | \end{verbatim} |
| 568 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 569 | Note that \code{"{\%}prog"} is expanded just like it is in \var{usage}. Apart |
| 570 | from that, \code{version} can contain anything you like. When you supply |
| 571 | it, \module{optparse} automatically adds a \code{"-{}-version"} option to your parser. |
| 572 | If it encounters this option on the command line, it expands your |
| 573 | \code{version} string (by replacing \code{"{\%}prog"}), prints it to stdout, and |
| 574 | exits. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 575 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 576 | For example, if your script is called \code{/usr/bin/foo}: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 577 | \begin{verbatim} |
| 578 | $ /usr/bin/foo --version |
| 579 | foo 1.0 |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 580 | \end{verbatim} |
| 581 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 582 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 583 | \subsubsection{How \module{optparse} handles errors\label{optparse-how-optik-handles-errors}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 584 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 585 | There are two broad classes of errors that \module{optparse} has to worry about: |
| 586 | programmer errors and user errors. Programmer errors are usually |
| 587 | erroneous calls to \code{parse.add{\_}option()}, e.g. invalid option strings, |
| 588 | unknown option attributes, missing option attributes, etc. These are |
| 589 | dealt with in the usual way: raise an exception (either |
| 590 | \code{optparse.OptionError} or \code{TypeError}) and let the program crash. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 591 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 592 | Handling user errors is much more important, since they are guaranteed |
| 593 | to happen no matter how stable your code is. \module{optparse} can automatically |
| 594 | detect some user errors, such as bad option arguments (passing \code{"-n |
| 595 | 4x"} where \programopt{-n} takes an integer argument), missing arguments |
| 596 | (\code{"-n"} at the end of the command line, where \programopt{-n} takes an argument |
| 597 | of any type). Also, you can call \code{parser.error()} to signal an |
| 598 | application-defined error condition: |
| 599 | \begin{verbatim} |
| 600 | (options, args) = parser.parse_args() |
| 601 | [...] |
| 602 | if options.a and options.b: |
| 603 | parser.error("options -a and -b are mutually exclusive") |
| 604 | \end{verbatim} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 605 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 606 | In either case, \module{optparse} handles the error the same way: it prints the |
| 607 | program's usage message and an error message to standard error and |
| 608 | exits with error status 2. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 609 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 610 | Consider the first example above, where the user passes \code{"4x"} to an |
| 611 | option that takes an integer: |
| 612 | \begin{verbatim} |
| 613 | $ /usr/bin/foo -n 4x |
| 614 | usage: foo [options] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 615 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 616 | foo: error: option -n: invalid integer value: '4x' |
| 617 | \end{verbatim} |
| 618 | |
| 619 | Or, where the user fails to pass a value at all: |
| 620 | \begin{verbatim} |
| 621 | $ /usr/bin/foo -n |
| 622 | usage: foo [options] |
| 623 | |
| 624 | foo: error: -n option requires an argument |
| 625 | \end{verbatim} |
| 626 | |
| 627 | \module{optparse}-generated error messages take care always to mention the option |
| 628 | involved in the error; be sure to do the same when calling |
| 629 | \code{parser.error()} from your application code. |
| 630 | |
| 631 | If \module{optparse}'s default error-handling behaviour does not suite your needs, |
| 632 | you'll need to subclass OptionParser and override \code{exit()} and/or |
| 633 | \method{error()}. |
| 634 | |
| 635 | |
| 636 | \subsubsection{Putting it all together\label{optparse-putting-it-all-together}} |
| 637 | |
| 638 | Here's what \module{optparse}-based scripts usually look like: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 639 | \begin{verbatim} |
| 640 | from optparse import OptionParser |
Greg Ward | d723128 | 2003-05-03 21:22:58 +0000 | [diff] [blame] | 641 | [...] |
| 642 | def main(): |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 643 | usage = "usage: %prog [options] arg" |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 644 | parser = OptionParser(usage) |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 645 | parser.add_option("-f", "--file", dest="filename", |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 646 | help="read data from FILENAME") |
| 647 | parser.add_option("-v", "--verbose", |
| 648 | action="store_true", dest="verbose") |
| 649 | parser.add_option("-q", "--quiet", |
| 650 | action="store_false", dest="verbose") |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 651 | [...] |
| 652 | (options, args) = parser.parse_args() |
| 653 | if len(args) != 1: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 654 | parser.error("incorrect number of arguments") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 655 | if options.verbose: |
Johannes Gijsbers | c9c37ca | 2004-09-11 15:47:30 +0000 | [diff] [blame] | 656 | print "reading %s..." % options.filename |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 657 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 658 | |
| 659 | if __name__ == "__main__": |
| 660 | main() |
| 661 | \end{verbatim} |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 662 | % $Id: tutorial.txt 415 2004-09-30 02:26:17Z greg $ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 663 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 664 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 665 | \subsection{Reference Guide\label{optparse-reference-guide}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 666 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 667 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 668 | \subsubsection{Populating the parser\label{optparse-populating-parser}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 669 | |
| 670 | There are several ways to populate the parser with options. The |
| 671 | preferred way is by using \code{OptionParser.add{\_}option()}, as shown in |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 672 | section~\ref{optparse-tutorial}, the tutorial. \method{add{\_}option()} can be called in one of two |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 673 | ways: |
| 674 | \begin{itemize} |
| 675 | \item {} |
| 676 | pass it an Option instance (as returned by \function{make{\_}option()}) |
| 677 | |
| 678 | \item {} |
| 679 | pass it any combination of positional and keyword arguments that are |
| 680 | acceptable to \function{make{\_}option()} (i.e., to the Option constructor), |
| 681 | and it will create the Option instance for you |
| 682 | |
| 683 | \end{itemize} |
| 684 | |
| 685 | The other alternative is to pass a list of pre-constructed Option |
| 686 | instances to the OptionParser constructor, as in: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 687 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 688 | option_list = [ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 689 | make_option("-f", "--filename", |
| 690 | action="store", type="string", dest="filename"), |
| 691 | make_option("-q", "--quiet", |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 692 | action="store_false", dest="verbose"), |
| 693 | ] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 694 | parser = OptionParser(option_list=option_list) |
| 695 | \end{verbatim} |
| 696 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 697 | (\function{make{\_}option()} is a factory function for creating Option instances; |
| 698 | currently it is an alias for the Option constructor. A future version |
| 699 | of \module{optparse} may split Option into several classes, and \function{make{\_}option()} |
| 700 | will pick the right class to instantiate. Do not instantiate Option |
| 701 | directly.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 702 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 703 | |
| 704 | \subsubsection{Defining options\label{optparse-defining-options}} |
| 705 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 706 | Each Option instance represents a set of synonymous command-line option |
Greg Ward | 961eda7 | 2004-11-12 01:20:17 +0000 | [diff] [blame] | 707 | strings, e.g. \programopt{-f} and \longprogramopt{file}. You can |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 708 | specify any number of short or long option strings, but you must specify |
| 709 | at least one overall option string. |
| 710 | |
| 711 | The canonical way to create an Option instance is by calling |
| 712 | \function{make{\_}option()}, so that is what will be shown here. However, the |
| 713 | most common and convenient way is to use \code{parser.add{\_}option()}. Note |
| 714 | that \function{make{\_}option()} and \code{parser.add{\_}option()} have identical call |
| 715 | signatures: |
| 716 | \begin{verbatim} |
| 717 | make_option(opt_str, ..., attr=value, ...) |
| 718 | parser.add_option(opt_str, ..., attr=value, ...) |
| 719 | \end{verbatim} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 720 | |
| 721 | To define an option with only a short option string: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 722 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 723 | make_option("-f", attr=value, ...) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 724 | \end{verbatim} |
| 725 | |
| 726 | And to define an option with only a long option string: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 727 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 728 | make_option("--foo", attr=value, ...) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 729 | \end{verbatim} |
| 730 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 731 | The \code{attr=value} keyword arguments define option attributes, |
| 732 | i.e. attributes of the Option object. The most important option |
| 733 | attribute is \member{action}, and it largely determines what other attributes |
| 734 | are relevant or required. If you pass irrelevant option attributes, or |
| 735 | fail to pass required ones, \module{optparse} raises an OptionError exception |
| 736 | explaining your mistake. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 737 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 738 | An options's \emph{action} determines what \module{optparse} does when it encounters |
| 739 | this option on the command-line. The actions hard-coded into \module{optparse} are: |
| 740 | \begin{description} |
| 741 | \item[\code{store}] |
| 742 | store this option's argument {[}default] |
| 743 | \item[\code{store{\_}const}] |
| 744 | store a constant value |
| 745 | \item[\code{store{\_}true}] |
| 746 | store a true value |
| 747 | \item[\code{store{\_}false}] |
| 748 | store a false value |
| 749 | \item[\code{append}] |
| 750 | append this option's argument to a list |
| 751 | \item[\code{count}] |
| 752 | increment a counter by one |
| 753 | \item[\code{callback}] |
| 754 | call a specified function |
| 755 | \item[\member{help}] |
| 756 | print a usage message including all options and the |
| 757 | documentation for them |
| 758 | \end{description} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 759 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 760 | (If you don't supply an action, the default is \code{store}. For this |
| 761 | action, you may also supply \member{type} and \member{dest} option attributes; see |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 762 | below.) |
| 763 | |
| 764 | As you can see, most actions involve storing or updating a value |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 765 | somewhere. \module{optparse} always creates an instance of \code{optparse.Values} |
| 766 | specifically for this purpose; we refer to this instance as \var{options}. |
| 767 | Option arguments (and various other values) are stored as attributes of |
| 768 | this object, according to the \member{dest} (destination) option attribute. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 769 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 770 | For example, when you call |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 771 | \begin{verbatim} |
| 772 | parser.parse_args() |
| 773 | \end{verbatim} |
| 774 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 775 | one of the first things \module{optparse} does is create the \var{options} object: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 776 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 777 | options = Values() |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 778 | \end{verbatim} |
| 779 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 780 | If one of the options in this parser is defined with |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 781 | \begin{verbatim} |
| 782 | make_option("-f", "--file", action="store", type="string", dest="filename") |
| 783 | \end{verbatim} |
| 784 | |
| 785 | and the command-line being parsed includes any of the following: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 786 | \begin{verbatim} |
| 787 | -ffoo |
| 788 | -f foo |
| 789 | --file=foo |
| 790 | --file foo |
| 791 | \end{verbatim} |
| 792 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 793 | then \module{optparse}, on seeing the \programopt{-f} or \longprogramopt{file} option, will do the |
| 794 | equivalent of |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 795 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 796 | options.filename = "foo" |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 797 | \end{verbatim} |
| 798 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 799 | The \member{type} and \member{dest} option attributes are almost as important as |
| 800 | \member{action}, but \member{action} is the only one that makes sense for \emph{all} |
| 801 | options. |
| 802 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 803 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 804 | \subsubsection{Standard option actions\label{optparse-standard-option-actions}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 805 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 806 | The various option actions all have slightly different requirements and |
| 807 | effects. Most actions have several relevant option attributes which you |
| 808 | may specify to guide \module{optparse}'s behaviour; a few have required attributes, |
| 809 | which you must specify for any option using that action. |
| 810 | \begin{itemize} |
| 811 | \item {} |
| 812 | \code{store} {[}relevant: \member{type}, \member{dest}, \code{nargs}, \code{choices}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 813 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 814 | The option must be followed by an argument, which is |
| 815 | converted to a value according to \member{type} and stored in |
| 816 | \member{dest}. If \code{nargs} {\textgreater} 1, multiple arguments will be consumed |
| 817 | from the command line; all will be converted according to |
| 818 | \member{type} and stored to \member{dest} as a tuple. See the ``Option |
| 819 | types'' section below. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 820 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 821 | If \code{choices} is supplied (a list or tuple of strings), the type |
| 822 | defaults to \code{choice}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 823 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 824 | If \member{type} is not supplied, it defaults to \code{string}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 825 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 826 | If \member{dest} is not supplied, \module{optparse} derives a destination from the |
| 827 | first long option string (e.g., \code{"-{}-foo-bar"} implies \code{foo{\_}bar}). |
| 828 | If there are no long option strings, \module{optparse} derives a destination from |
| 829 | the first short option string (e.g., \code{"-f"} implies \code{f}). |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 830 | |
| 831 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 832 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 833 | parser.add_option("-f") |
| 834 | parser.add_option("-p", type="float", nargs=3, dest="point") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 835 | \end{verbatim} |
| 836 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 837 | As it parses the command line |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 838 | \begin{verbatim} |
| 839 | -f foo.txt -p 1 -3.5 4 -fbar.txt |
| 840 | \end{verbatim} |
| 841 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 842 | \module{optparse} will set |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 843 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 844 | options.f = "foo.txt" |
| 845 | options.point = (1.0, -3.5, 4.0) |
| 846 | options.f = "bar.txt" |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 847 | \end{verbatim} |
| 848 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 849 | \item {} |
| 850 | \code{store{\_}const} {[}required: \code{const}; relevant: \member{dest}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 851 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 852 | The value \code{const} is stored in \member{dest}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 853 | |
| 854 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 855 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 856 | parser.add_option("-q", "--quiet", |
| 857 | action="store_const", const=0, dest="verbose") |
| 858 | parser.add_option("-v", "--verbose", |
| 859 | action="store_const", const=1, dest="verbose") |
| 860 | parser.add_option("--noisy", |
| 861 | action="store_const", const=2, dest="verbose") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 862 | \end{verbatim} |
| 863 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 864 | If \code{"-{}-noisy"} is seen, \module{optparse} will set |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 865 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 866 | options.verbose = 2 |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 867 | \end{verbatim} |
| 868 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 869 | \item {} |
| 870 | \code{store{\_}true} {[}relevant: \member{dest}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 871 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 872 | A special case of \code{store{\_}const} that stores a true value |
| 873 | to \member{dest}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 874 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 875 | \item {} |
| 876 | \code{store{\_}false} {[}relevant: \member{dest}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 877 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 878 | Like \code{store{\_}true}, but stores a false value. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 879 | |
| 880 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 881 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 882 | parser.add_option("--clobber", action="store_true", dest="clobber") |
| 883 | parser.add_option("--no-clobber", action="store_false", dest="clobber") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 884 | \end{verbatim} |
| 885 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 886 | \item {} |
| 887 | \code{append} {[}relevant: \member{type}, \member{dest}, \code{nargs}, \code{choices}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 888 | |
| 889 | The option must be followed by an argument, which is appended to the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 890 | list in \member{dest}. If no default value for \member{dest} is supplied, an |
| 891 | empty list is automatically created when \module{optparse} first encounters this |
| 892 | option on the command-line. If \code{nargs} {\textgreater} 1, multiple arguments are |
| 893 | consumed, and a tuple of length \code{nargs} is appended to \member{dest}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 894 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 895 | The defaults for \member{type} and \member{dest} are the same as for the |
| 896 | \code{store} action. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 897 | |
| 898 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 899 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 900 | parser.add_option("-t", "--tracks", action="append", type="int") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 901 | \end{verbatim} |
| 902 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 903 | If \code{"-t3"} is seen on the command-line, \module{optparse} does the equivalent of: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 904 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 905 | options.tracks = [] |
| 906 | options.tracks.append(int("3")) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 907 | \end{verbatim} |
| 908 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 909 | If, a little later on, \code{"-{}-tracks=4"} is seen, it does: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 910 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 911 | options.tracks.append(int("4")) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 912 | \end{verbatim} |
| 913 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 914 | \item {} |
| 915 | \code{count} {[}relevant: \member{dest}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 916 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 917 | Increment the integer stored at \member{dest}. If no default value is |
| 918 | supplied, \member{dest} is set to zero before being incremented the first |
| 919 | time. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 920 | |
| 921 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 922 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 923 | parser.add_option("-v", action="count", dest="verbosity") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 924 | \end{verbatim} |
| 925 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 926 | The first time \code{"-v"} is seen on the command line, \module{optparse} does the |
| 927 | equivalent of: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 928 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 929 | options.verbosity = 0 |
| 930 | options.verbosity += 1 |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 931 | \end{verbatim} |
| 932 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 933 | Every subsequent occurrence of \code{"-v"} results in |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 934 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 935 | options.verbosity += 1 |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 936 | \end{verbatim} |
| 937 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 938 | \item {} |
| 939 | \code{callback} {[}required: \code{callback}; |
| 940 | relevant: \member{type}, \code{nargs}, \code{callback{\_}args}, \code{callback{\_}kwargs}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 941 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 942 | Call the function specified by \code{callback}. The signature of |
| 943 | this function should be |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 944 | \begin{verbatim} |
| 945 | func(option : Option, |
| 946 | opt : string, |
| 947 | value : any, |
| 948 | parser : OptionParser, |
| 949 | *args, **kwargs) |
| 950 | \end{verbatim} |
| 951 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 952 | See section~\ref{optparse-option-callbacks}, Option Callbacks for more detail. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 953 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 954 | \item {} |
| 955 | \member{help} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 956 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 957 | Prints a complete help message for all the options in the |
| 958 | current option parser. The help message is constructed from |
| 959 | the \var{usage} string passed to OptionParser's constructor and |
| 960 | the \member{help} string passed to every option. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 961 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 962 | If no \member{help} string is supplied for an option, it will still be |
| 963 | listed in the help message. To omit an option entirely, use |
| 964 | the special value \code{optparse.SUPPRESS{\_}HELP}. |
| 965 | |
| 966 | \module{optparse} automatically adds a \member{help} option to all OptionParsers, so |
| 967 | you do not normally need to create one. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 968 | |
| 969 | Example: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 970 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 971 | from optparse import OptionParser, SUPPRESS_HELP |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 972 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 973 | parser = OptionParser() |
| 974 | parser.add_option("-h", "--help", action="help"), |
| 975 | parser.add_option("-v", action="store_true", dest="verbose", |
| 976 | help="Be moderately verbose") |
| 977 | parser.add_option("--file", dest="filename", |
| 978 | help="Input file to read data from"), |
| 979 | parser.add_option("--secret", help=SUPPRESS_HELP) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 980 | \end{verbatim} |
| 981 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 982 | If \module{optparse} sees either \code{"-h"} or \code{"-{}-help"} on the command line, it |
| 983 | will print something like the following help message to stdout |
| 984 | (assuming \code{sys.argv{[}0]} is \code{"foo.py"}): |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 985 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 986 | usage: foo.py [options] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 987 | |
| 988 | options: |
| 989 | -h, --help Show this help message and exit |
| 990 | -v Be moderately verbose |
| 991 | --file=FILENAME Input file to read data from |
| 992 | \end{verbatim} |
| 993 | |
| 994 | After printing the help message, \module{optparse} terminates your process |
| 995 | with \code{sys.exit(0)}. |
| 996 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 997 | \item {} |
| 998 | \code{version} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 999 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1000 | Prints the version number supplied to the OptionParser to stdout and |
| 1001 | exits. The version number is actually formatted and printed by the |
| 1002 | \code{print{\_}version()} method of OptionParser. Generally only relevant |
| 1003 | if the \code{version} argument is supplied to the OptionParser |
| 1004 | constructor. As with \member{help} options, you will rarely create |
| 1005 | \code{version} options, since \module{optparse} automatically adds them when needed. |
| 1006 | |
| 1007 | \end{itemize} |
| 1008 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1009 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1010 | \subsubsection{Standard option types\label{optparse-standard-option-types}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1011 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1012 | \module{optparse} has six built-in option types: \code{string}, \code{int}, \code{long}, |
| 1013 | \code{choice}, \code{float} and \code{complex}. If you need to add new option |
| 1014 | types, see section~\ref{optparse-extending}, Extending \module{optparse}. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1015 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1016 | Arguments to string options are not checked or converted in any way: the |
| 1017 | text on the command line is stored in the destination (or passed to the |
| 1018 | callback) as-is. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1019 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1020 | Integer arguments are passed to \code{int()} to convert them to Python |
| 1021 | integers. If \code{int()} fails, so will \module{optparse}, although with a more |
| 1022 | useful error message. (Internally, \module{optparse} raises OptionValueError; |
| 1023 | OptionParser catches this exception higher up and terminates your |
| 1024 | program with a useful error message.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1025 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1026 | Likewise, \code{float} arguments are passed to \code{float()} for conversion, |
| 1027 | \code{long} arguments to \code{long()}, and \code{complex} arguments to |
| 1028 | \code{complex()}. Apart from that, they are handled identically to integer |
| 1029 | arguments. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1030 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1031 | \code{choice} options are a subtype of \code{string} options. The \code{choices} |
| 1032 | option attribute (a sequence of strings) defines the set of allowed |
| 1033 | option arguments. \code{optparse.option.check{\_}choice()} compares |
| 1034 | user-supplied option arguments against this master list and raises |
| 1035 | OptionValueError if an invalid string is given. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1036 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1037 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1038 | \subsubsection{Querying and manipulating your option parser\label{optparse-querying-manipulating-option-parser}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1039 | |
| 1040 | Sometimes, it's useful to poke around your option parser and see what's |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1041 | there. OptionParser provides a couple of methods to help you out: |
| 1042 | \begin{description} |
| 1043 | \item[\code{has{\_}option(opt{\_}str)}] |
| 1044 | Return true if the OptionParser has an option with |
| 1045 | option string \code{opt{\_}str} (e.g., \code{"-q"} or \code{"-{}-verbose"}). |
| 1046 | \item[\code{get{\_}option(opt{\_}str)}] |
| 1047 | Returns the Option instance with the option string \code{opt{\_}str}, or |
| 1048 | \code{None} if no options have that option string. |
| 1049 | \item[\code{remove{\_}option(opt{\_}str)}] |
| 1050 | If the OptionParser has an option corresponding to \code{opt{\_}str}, |
| 1051 | that option is removed. If that option provided any other |
| 1052 | option strings, all of those option strings become invalid. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1053 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1054 | If \code{opt{\_}str} does not occur in any option belonging to this |
| 1055 | OptionParser, raises ValueError. |
| 1056 | \end{description} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1057 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1058 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1059 | \subsubsection{Conflicts between options\label{optparse-conflicts-between-options}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1060 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1061 | If you're not careful, it's easy to define options with conflicting |
| 1062 | option strings: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1063 | \begin{verbatim} |
| 1064 | parser.add_option("-n", "--dry-run", ...) |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1065 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1066 | parser.add_option("-n", "--noisy", ...) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1067 | \end{verbatim} |
| 1068 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1069 | (This is particularly true if you've defined your own OptionParser |
| 1070 | subclass with some standard options.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1071 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1072 | Every time you add an option, \module{optparse} checks for conflicts with existing |
| 1073 | options. If it finds any, it invokes the current conflict-handling |
| 1074 | mechanism. You can set the conflict-handling mechanism either in the |
| 1075 | constructor: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1076 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1077 | parser = OptionParser(..., conflict_handler="...") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1078 | \end{verbatim} |
| 1079 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1080 | or with a separate call: |
| 1081 | \begin{verbatim} |
| 1082 | parser.set_conflict_handler("...") |
| 1083 | \end{verbatim} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1084 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1085 | The available conflict-handling mechanisms are: |
| 1086 | \begin{quote} |
| 1087 | \begin{description} |
| 1088 | \item[\code{error} (default)] |
| 1089 | assume option conflicts are a programming error and raise |
| 1090 | OptionConflictError |
| 1091 | \item[\code{resolve}] |
| 1092 | resolve option conflicts intelligently (see below) |
| 1093 | \end{description} |
| 1094 | \end{quote} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1095 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1096 | As an example, let's define an OptionParser that resolves conflicts |
| 1097 | intelligently and add conflicting options to it: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1098 | \begin{verbatim} |
| 1099 | parser = OptionParser(conflict_handler="resolve") |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1100 | parser.add_option("-n", "--dry-run", ..., help="do no harm") |
| 1101 | parser.add_option("-n", "--noisy", ..., help="be noisy") |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1102 | \end{verbatim} |
| 1103 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1104 | At this point, \module{optparse} detects that a previously-added option is already |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1105 | using the \code{"-n"} option string. Since \code{conflict{\_}handler} is |
| 1106 | \code{"resolve"}, it resolves the situation by removing \code{"-n"} from the |
| 1107 | earlier option's list of option strings. Now \code{"-{}-dry-run"} is the |
| 1108 | only way for the user to activate that option. If the user asks for |
| 1109 | help, the help message will reflect that: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1110 | \begin{verbatim} |
| 1111 | options: |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1112 | --dry-run do no harm |
| 1113 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1114 | -n, --noisy be noisy |
| 1115 | \end{verbatim} |
| 1116 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1117 | It's possible to whittle away the option strings for a previously-added |
| 1118 | option until there are none left, and the user has no way of invoking |
| 1119 | that option from the command-line. In that case, \module{optparse} removes that |
| 1120 | option completely, so it doesn't show up in help text or anywhere else. |
| 1121 | Carrying on with our existing OptionParser: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1122 | \begin{verbatim} |
| 1123 | parser.add_option("--dry-run", ..., help="new dry-run option") |
| 1124 | \end{verbatim} |
| 1125 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1126 | At this point, the original \programopt{-n/-{}-dry-run} option is no longer |
| 1127 | accessible, so \module{optparse} removes it, leaving this help text: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1128 | \begin{verbatim} |
| 1129 | options: |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1130 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1131 | -n, --noisy be noisy |
| 1132 | --dry-run new dry-run option |
| 1133 | \end{verbatim} |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1134 | % $Id: reference.txt 415 2004-09-30 02:26:17Z greg $ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1135 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1136 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1137 | \subsection{Option Callbacks\label{optparse-option-callbacks}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1138 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1139 | When \module{optparse}'s built-in actions and types aren't quite enough for your |
| 1140 | needs, you have two choices: extend \module{optparse} or define a callback option. |
| 1141 | Extending \module{optparse} is more general, but overkill for a lot of simple |
| 1142 | cases. Quite often a simple callback is all you need. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1143 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1144 | There are two steps to defining a callback option: |
| 1145 | \begin{itemize} |
| 1146 | \item {} |
| 1147 | define the option itself using the \code{callback} action |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1148 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1149 | \item {} |
| 1150 | write the callback; this is a function (or method) that |
| 1151 | takes at least four arguments, as described below |
| 1152 | |
| 1153 | \end{itemize} |
| 1154 | |
| 1155 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1156 | \subsubsection{Defining a callback option\label{optparse-defining-callback-option}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1157 | |
| 1158 | As always, the easiest way to define a callback option is by using the |
| 1159 | \code{parser.add{\_}option()} method. Apart from \member{action}, the only option |
| 1160 | attribute you must specify is \code{callback}, the function to call: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1161 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1162 | parser.add_option("-c", action="callback", callback=my_callback) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1163 | \end{verbatim} |
| 1164 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1165 | \code{callback} is a function (or other callable object), so you must have |
| 1166 | already defined \code{my{\_}callback()} when you create this callback option. |
| 1167 | In this simple case, \module{optparse} doesn't even know if \programopt{-c} takes any |
| 1168 | arguments, which usually means that the option takes no arguments{---}the |
| 1169 | mere presence of \programopt{-c} on the command-line is all it needs to know. In |
| 1170 | some circumstances, though, you might want your callback to consume an |
| 1171 | arbitrary number of command-line arguments. This is where writing |
| 1172 | callbacks gets tricky; it's covered later in this section. |
| 1173 | |
| 1174 | \module{optparse} always passes four particular arguments to your callback, and it |
| 1175 | will only pass additional arguments if you specify them via |
| 1176 | \code{callback{\_}args} and \code{callback{\_}kwargs}. Thus, the minimal callback |
| 1177 | function signature is: |
| 1178 | \begin{verbatim} |
| 1179 | def my_callback(option, opt, value, parser): |
| 1180 | \end{verbatim} |
| 1181 | |
| 1182 | The four arguments to a callback are described below. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1183 | |
| 1184 | There are several other option attributes that you can supply when you |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1185 | define a callback option: |
| 1186 | \begin{description} |
| 1187 | \item[\member{type}] |
| 1188 | has its usual meaning: as with the \code{store} or \code{append} actions, |
| 1189 | it instructs \module{optparse} to consume one argument and convert it to |
| 1190 | \member{type}. Rather than storing the converted value(s) anywhere, |
| 1191 | though, \module{optparse} passes it to your callback function. |
| 1192 | \item[\code{nargs}] |
| 1193 | also has its usual meaning: if it is supplied and {\textgreater} 1, \module{optparse} will |
| 1194 | consume \code{nargs} arguments, each of which must be convertible to |
| 1195 | \member{type}. It then passes a tuple of converted values to your |
| 1196 | callback. |
| 1197 | \item[\code{callback{\_}args}] |
| 1198 | a tuple of extra positional arguments to pass to the callback |
| 1199 | \item[\code{callback{\_}kwargs}] |
| 1200 | a dictionary of extra keyword arguments to pass to the callback |
| 1201 | \end{description} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1202 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1203 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1204 | \subsubsection{How callbacks are called\label{optparse-how-callbacks-called}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1205 | |
| 1206 | All callbacks are called as follows: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1207 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1208 | func(option, opt_str, value, parser, *args, **kwargs) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1209 | \end{verbatim} |
| 1210 | |
| 1211 | where |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1212 | \begin{description} |
| 1213 | \item[\code{option}] |
| 1214 | is the Option instance that's calling the callback |
| 1215 | \item[\code{opt{\_}str}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1216 | is the option string seen on the command-line that's triggering the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1217 | callback. (If an abbreviated long option was used, \code{opt{\_}str} will |
| 1218 | be the full, canonical option string{---}e.g. if the user puts |
| 1219 | \code{"-{}-foo"} on the command-line as an abbreviation for |
| 1220 | \code{"-{}-foobar"}, then \code{opt{\_}str} will be \code{"-{}-foobar"}.) |
| 1221 | \item[\code{value}] |
| 1222 | is the argument to this option seen on the command-line. \module{optparse} will |
| 1223 | only expect an argument if \member{type} is set; the type of \code{value} |
| 1224 | will be the type implied by the option's type. If \member{type} for this |
| 1225 | option is \code{None} (no argument expected), then \code{value} will be |
| 1226 | \code{None}. If \code{nargs} {\textgreater} 1, \code{value} will be a tuple of values of |
| 1227 | the appropriate type. |
| 1228 | \item[\code{parser}] |
| 1229 | is the OptionParser instance driving the whole thing, mainly |
| 1230 | useful because you can access some other interesting data through |
| 1231 | its instance attributes: |
| 1232 | \begin{description} |
| 1233 | \item[\code{parser.largs}] |
| 1234 | the current list of leftover arguments, ie. arguments that have |
| 1235 | been consumed but are neither options nor option arguments. |
| 1236 | Feel free to modify \code{parser.largs}, e.g. by adding more |
| 1237 | arguments to it. (This list will become \var{args}, the second |
| 1238 | return value of \method{parse{\_}args()}.) |
| 1239 | \item[\code{parser.rargs}] |
| 1240 | the current list of remaining arguments, ie. with \code{opt{\_}str} and |
| 1241 | \code{value} (if applicable) removed, and only the arguments |
| 1242 | following them still there. Feel free to modify |
| 1243 | \code{parser.rargs}, e.g. by consuming more arguments. |
| 1244 | \item[\code{parser.values}] |
| 1245 | the object where option values are by default stored (an |
| 1246 | instance of optparse.OptionValues). This lets callbacks use the |
| 1247 | same mechanism as the rest of \module{optparse} for storing option values; |
| 1248 | you don't need to mess around with globals or closures. You can |
| 1249 | also access or modify the value(s) of any options already |
| 1250 | encountered on the command-line. |
| 1251 | \end{description} |
Raymond Hettinger | 79e0531 | 2004-12-31 01:07:27 +0000 | [diff] [blame] | 1252 | \item[\code{args}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1253 | is a tuple of arbitrary positional arguments supplied via the |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1254 | \code{callback{\_}args} option attribute. |
| 1255 | \item[\code{kwargs}] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1256 | is a dictionary of arbitrary keyword arguments supplied via |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1257 | \code{callback{\_}kwargs}. |
| 1258 | \end{description} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1259 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1260 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1261 | \subsubsection{Raising errors in a callback\label{optparse-raising-errors-in-callback}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1262 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1263 | The callback function should raise OptionValueError if there are any |
| 1264 | problems with the option or its argument(s). \module{optparse} catches this and |
| 1265 | terminates the program, printing the error message you supply to |
| 1266 | stderr. Your message should be clear, concise, accurate, and mention |
| 1267 | the option at fault. Otherwise, the user will have a hard time |
| 1268 | figuring out what he did wrong. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1269 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1270 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1271 | \subsubsection{Callback example 1: trivial callback\label{optparse-callback-example-1}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1272 | |
| 1273 | Here's an example of a callback option that takes no arguments, and |
| 1274 | simply records that the option was seen: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1275 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1276 | def record_foo_seen(option, opt_str, value, parser): |
| 1277 | parser.saw_foo = True |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1278 | |
| 1279 | parser.add_option("--foo", action="callback", callback=record_foo_seen) |
| 1280 | \end{verbatim} |
| 1281 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1282 | Of course, you could do that with the \code{store{\_}true} action. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1283 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1284 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1285 | \subsubsection{Callback example 2: check option order\label{optparse-callback-example-2}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1286 | |
| 1287 | Here's a slightly more interesting example: record the fact that |
| 1288 | \code{"-a"} is seen, but blow up if it comes after \code{"-b"} in the |
| 1289 | command-line. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1290 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1291 | def check_order(option, opt_str, value, parser): |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1292 | if parser.values.b: |
| 1293 | raise OptionValueError("can't use -a after -b") |
| 1294 | parser.values.a = 1 |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1295 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1296 | parser.add_option("-a", action="callback", callback=check_order) |
| 1297 | parser.add_option("-b", action="store_true", dest="b") |
| 1298 | \end{verbatim} |
| 1299 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1300 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1301 | \subsubsection{Callback example 3: check option order (generalized)\label{optparse-callback-example-3}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1302 | |
| 1303 | If you want to re-use this callback for several similar options (set a |
| 1304 | flag, but blow up if \code{"-b"} has already been seen), it needs a bit of |
| 1305 | work: the error message and the flag that it sets must be |
| 1306 | generalized. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1307 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1308 | def check_order(option, opt_str, value, parser): |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1309 | if parser.values.b: |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1310 | raise OptionValueError("can't use %s after -b" % opt_str) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1311 | setattr(parser.values, option.dest, 1) |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1312 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1313 | parser.add_option("-a", action="callback", callback=check_order, dest='a') |
| 1314 | parser.add_option("-b", action="store_true", dest="b") |
| 1315 | parser.add_option("-c", action="callback", callback=check_order, dest='c') |
| 1316 | \end{verbatim} |
| 1317 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1318 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1319 | \subsubsection{Callback example 4: check arbitrary condition\label{optparse-callback-example-4}} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1320 | |
| 1321 | Of course, you could put any condition in there{---}you're not limited |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1322 | to checking the values of already-defined options. For example, if |
| 1323 | you have options that should not be called when the moon is full, all |
| 1324 | you have to do is this: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1325 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1326 | def check_moon(option, opt_str, value, parser): |
| 1327 | if is_moon_full(): |
| 1328 | raise OptionValueError("%s option invalid when moon is full" |
| 1329 | % opt_str) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1330 | setattr(parser.values, option.dest, 1) |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1331 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1332 | parser.add_option("--foo", |
| 1333 | action="callback", callback=check_moon, dest="foo") |
| 1334 | \end{verbatim} |
| 1335 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1336 | (The definition of \code{is{\_}moon{\_}full()} is left as an exercise for the |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1337 | reader.) |
| 1338 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1339 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1340 | \subsubsection{Callback example 5: fixed arguments\label{optparse-callback-example-5}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1341 | |
| 1342 | Things get slightly more interesting when you define callback options |
| 1343 | that take a fixed number of arguments. Specifying that a callback |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1344 | option takes arguments is similar to defining a \code{store} or \code{append} |
| 1345 | option: if you define \member{type}, then the option takes one argument that |
| 1346 | must be convertible to that type; if you further define \code{nargs}, then |
| 1347 | the option takes \code{nargs} arguments. |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1348 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1349 | Here's an example that just emulates the standard \code{store} action: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1350 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1351 | def store_value(option, opt_str, value, parser): |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1352 | setattr(parser.values, option.dest, value) |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1353 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1354 | parser.add_option("--foo", |
| 1355 | action="callback", callback=store_value, |
| 1356 | type="int", nargs=3, dest="foo") |
| 1357 | \end{verbatim} |
| 1358 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1359 | Note that \module{optparse} takes care of consuming 3 arguments and converting them |
| 1360 | to integers for you; all you have to do is store them. (Or whatever; |
| 1361 | obviously you don't need a callback for this example.) |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1362 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1363 | |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1364 | \subsubsection{Callback example 6: variable arguments\label{optparse-callback-example-6}} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1365 | |
| 1366 | Things get hairy when you want an option to take a variable number of |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1367 | arguments. For this case, you must write a callback, as \module{optparse} doesn't |
| 1368 | provide any built-in capabilities for it. And you have to deal with |
| 1369 | certain intricacies of conventional \UNIX{} command-line parsing that \module{optparse} |
| 1370 | normally handles for you. In particular, callbacks should implement |
| 1371 | the conventional rules for bare \code{"-{}-"} and \code{"-"} arguments: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1372 | \begin{itemize} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1373 | \item {} |
| 1374 | either \code{"-{}-"} or \code{"-"} can be option arguments |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1375 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1376 | \item {} |
| 1377 | bare \code{"-{}-"} (if not the argument to some option): halt command-line |
| 1378 | processing and discard the \code{"-{}-"} |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1379 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1380 | \item {} |
| 1381 | bare \code{"-"} (if not the argument to some option): halt command-line |
| 1382 | processing but keep the \code{"-"} (append it to \code{parser.largs}) |
| 1383 | |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1384 | \end{itemize} |
| 1385 | |
| 1386 | If you want an option that takes a variable number of arguments, there |
| 1387 | are several subtle, tricky issues to worry about. The exact |
| 1388 | implementation you choose will be based on which trade-offs you're |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1389 | willing to make for your application (which is why \module{optparse} doesn't support |
| 1390 | this sort of thing directly). |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1391 | |
| 1392 | Nevertheless, here's a stab at a callback for an option with variable |
| 1393 | arguments: |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1394 | \begin{verbatim} |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1395 | def vararg_callback(option, opt_str, value, parser): |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1396 | assert value is None |
| 1397 | done = 0 |
| 1398 | value = [] |
| 1399 | rargs = parser.rargs |
| 1400 | while rargs: |
| 1401 | arg = rargs[0] |
| 1402 | |
| 1403 | # Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f", |
| 1404 | # etc. Note that this also stops on "-3" or "-3.0", so if |
| 1405 | # your option takes numeric values, you will need to handle |
| 1406 | # this. |
| 1407 | if ((arg[:2] == "--" and len(arg) > 2) or |
| 1408 | (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")): |
| 1409 | break |
| 1410 | else: |
| 1411 | value.append(arg) |
| 1412 | del rargs[0] |
| 1413 | |
| 1414 | setattr(parser.values, option.dest, value) |
| 1415 | |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1416 | [...] |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1417 | parser.add_option("-c", "--callback", |
| 1418 | action="callback", callback=varargs) |
| 1419 | \end{verbatim} |
| 1420 | |
| 1421 | The main weakness with this particular implementation is that negative |
Greg Ward | b6f7fb7 | 2004-09-28 01:30:23 +0000 | [diff] [blame] | 1422 | numbers in the arguments following \code{"-c"} will be interpreted as |
| 1423 | further options (probably causing an error), rather than as arguments to |
| 1424 | \code{"-c"}. Fixing this is left as an exercise for the reader. |
Greg Ward | e644a1b | 2004-10-01 01:16:39 +0000 | [diff] [blame] | 1425 | % $Id: callbacks.txt 415 2004-09-30 02:26:17Z greg $ |
Neal Norwitz | 488609e | 2003-01-06 16:51:37 +0000 | [diff] [blame] | 1426 | |