| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="optparse-populating-parser.html" /> |
| 13 | <link rel="prev" href="optparse-reference-guide.html" /> |
| 14 | <link rel="parent" href="optparse-reference-guide.html" /> |
| 15 | <link rel="next" href="optparse-populating-parser.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.21.3.1 Creating the parser</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.21.3 Reference Guide" |
| 25 | href="optparse-reference-guide.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" |
| 28 | href="optparse-reference-guide.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.21.3.2 Populating the parser" |
| 31 | href="optparse-populating-parser.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="optparse-populating-parser.html">6.21.3.2 Populating the parser</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0082131000000000000000"></A><A NAME="optparse-creating-parser"></A> |
| 56 | <BR> |
| 57 | 6.21.3.1 Creating the parser |
| 58 | </H3> |
| 59 | |
| 60 | <P> |
| 61 | The first step in using <tt class="module">optparse</tt> is to create an OptionParser instance: |
| 62 | <div class="verbatim"><pre> |
| 63 | parser = OptionParser(...) |
| 64 | </pre></div> |
| 65 | |
| 66 | <P> |
| 67 | The OptionParser constructor has no required arguments, but a number of |
| 68 | optional keyword arguments. You should always pass them as keyword |
| 69 | arguments, i.e. do not rely on the order in which the arguments are |
| 70 | declared. |
| 71 | <BLOCKQUOTE> |
| 72 | </BLOCKQUOTE><DL> |
| 73 | <DT><STRONG><code>usage</code> (default: <code>"%prog [options]"</code>)</STRONG></DT> |
| 74 | <DD>The usage summary to print when your program is run incorrectly or |
| 75 | with a help option. When <tt class="module">optparse</tt> prints the usage string, it expands |
| 76 | <code>%prog</code> to <code>os.path.basename(sys.argv[0])</code> (or to <code>prog</code> if |
| 77 | you passed that keyword argument). To suppress a usage message, |
| 78 | pass the special value <code>optparse.SUPPRESS_USAGE</code>. |
| 79 | </DD> |
| 80 | <DT><STRONG><code>option_list</code> (default: <code>[]</code>)</STRONG></DT> |
| 81 | <DD>A list of Option objects to populate the parser with. The options |
| 82 | in <code>option_list</code> are added after any options in |
| 83 | <code>standard_option_list</code> (a class attribute that may be set by |
| 84 | OptionParser subclasses), but before any version or help options. |
| 85 | Deprecated; use <tt class="method">add_option()</tt> after creating the parser instead. |
| 86 | </DD> |
| 87 | <DT><STRONG><code>option_class</code> (default: optparse.Option)</STRONG></DT> |
| 88 | <DD>Class to use when adding options to the parser in <tt class="method">add_option()</tt>. |
| 89 | </DD> |
| 90 | <DT><STRONG><code>version</code> (default: <code>None</code>)</STRONG></DT> |
| 91 | <DD>A version string to print when the user supplies a version option. |
| 92 | If you supply a true value for <code>version</code>, <tt class="module">optparse</tt> automatically adds |
| 93 | a version option with the single option string <code>"-version"</code>. The |
| 94 | substring <code>"%prog"</code> is expanded the same as for <code>usage</code>. |
| 95 | </DD> |
| 96 | <DT><STRONG><code>conflict_handler</code> (default: <code>"error"</code>)</STRONG></DT> |
| 97 | <DD>Specifies what to do when options with conflicting option strings |
| 98 | are added to the parser; see section <A href="optparse-conflicts-between-options.html#optparse-conflicts-between-options">6.21.3</A>, Conflicts between options. |
| 99 | </DD> |
| 100 | <DT><STRONG><code>description</code> (default: <code>None</code>)</STRONG></DT> |
| 101 | <DD>A paragraph of text giving a brief overview of your program. <tt class="module">optparse</tt> |
| 102 | reformats this paragraph to fit the current terminal width and |
| 103 | prints it when the user requests help (after <code>usage</code>, but before |
| 104 | the list of options). |
| 105 | </DD> |
| 106 | <DT><STRONG><code>formatter</code> (default: a new IndentedHelpFormatter)</STRONG></DT> |
| 107 | <DD>An instance of optparse.HelpFormatter that will be used for |
| 108 | printing help text. <tt class="module">optparse</tt> provides two concrete classes for this |
| 109 | purpose: IndentedHelpFormatter and TitledHelpFormatter. |
| 110 | </DD> |
| 111 | <DT><STRONG><code>add_help_option</code> (default: <code>True</code>)</STRONG></DT> |
| 112 | <DD>If true, <tt class="module">optparse</tt> will add a help option (with option strings <code>"-h"</code> |
| 113 | and <code>"-help"</code>) to the parser. |
| 114 | </DD> |
| 115 | <DT><STRONG><code>prog</code></STRONG></DT> |
| 116 | <DD>The string to use when expanding <code>"%prog"</code> in <code>usage</code> and |
| 117 | <code>version</code> instead of <code>os.path.basename(sys.argv[0])</code>. |
| 118 | </DD> |
| 119 | </DL><BLOCKQUOTE> |
| 120 | </BLOCKQUOTE> |
| 121 | |
| 122 | <P> |
| 123 | |
| 124 | <DIV CLASS="navigation"> |
| 125 | <div class='online-navigation'> |
| 126 | <p></p><hr /> |
| 127 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 128 | <tr> |
| 129 | <td class='online-navigation'><a rel="prev" title="6.21.3 Reference Guide" |
| 130 | href="optparse-reference-guide.html"><img src='../icons/previous.png' |
| 131 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 132 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" |
| 133 | href="optparse-reference-guide.html"><img src='../icons/up.png' |
| 134 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 135 | <td class='online-navigation'><a rel="next" title="6.21.3.2 Populating the parser" |
| 136 | href="optparse-populating-parser.html"><img src='../icons/next.png' |
| 137 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 138 | <td align="center" width="100%">Python Library Reference</td> |
| 139 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 140 | href="contents.html"><img src='../icons/contents.png' |
| 141 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 142 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 143 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 144 | <td class='online-navigation'><a rel="index" title="Index" |
| 145 | href="genindex.html"><img src='../icons/index.png' |
| 146 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 147 | </tr></table> |
| 148 | <div class='online-navigation'> |
| 149 | <b class="navlabel">Previous:</b> |
| 150 | <a class="sectref" rel="prev" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> |
| 151 | <b class="navlabel">Up:</b> |
| 152 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> |
| 153 | <b class="navlabel">Next:</b> |
| 154 | <a class="sectref" rel="next" href="optparse-populating-parser.html">6.21.3.2 Populating the parser</A> |
| 155 | </div> |
| 156 | </div> |
| 157 | <hr /> |
| 158 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 159 | </DIV> |
| 160 | <!--End of Navigation Panel--> |
| 161 | <ADDRESS> |
| 162 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 163 | </ADDRESS> |
| 164 | </BODY> |
| 165 | </HTML> |