| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" | |
| "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
| <meta name="generator" content="AsciiDoc 8.5.2" /> | |
| <title>parse-options API</title> | |
| <style type="text/css"> | |
| /* Debug borders */ | |
| p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 { | |
| /* | |
| border: 1px solid red; | |
| */ | |
| } | |
| body { | |
| margin: 1em 5% 1em 5%; | |
| } | |
| a { | |
| color: blue; | |
| text-decoration: underline; | |
| } | |
| a:visited { | |
| color: fuchsia; | |
| } | |
| em { | |
| font-style: italic; | |
| color: navy; | |
| } | |
| strong { | |
| font-weight: bold; | |
| color: #083194; | |
| } | |
| tt { | |
| color: navy; | |
| } | |
| h1, h2, h3, h4, h5, h6 { | |
| color: #527bbd; | |
| font-family: sans-serif; | |
| margin-top: 1.2em; | |
| margin-bottom: 0.5em; | |
| line-height: 1.3; | |
| } | |
| h1, h2, h3 { | |
| border-bottom: 2px solid silver; | |
| } | |
| h2 { | |
| padding-top: 0.5em; | |
| } | |
| h3 { | |
| float: left; | |
| } | |
| h3 + * { | |
| clear: left; | |
| } | |
| div.sectionbody { | |
| font-family: serif; | |
| margin-left: 0; | |
| } | |
| hr { | |
| border: 1px solid silver; | |
| } | |
| p { | |
| margin-top: 0.5em; | |
| margin-bottom: 0.5em; | |
| } | |
| ul, ol, li > p { | |
| margin-top: 0; | |
| } | |
| pre { | |
| padding: 0; | |
| margin: 0; | |
| } | |
| span#author { | |
| color: #527bbd; | |
| font-family: sans-serif; | |
| font-weight: bold; | |
| font-size: 1.1em; | |
| } | |
| span#email { | |
| } | |
| span#revnumber, span#revdate, span#revremark { | |
| font-family: sans-serif; | |
| } | |
| div#footer { | |
| font-family: sans-serif; | |
| font-size: small; | |
| border-top: 2px solid silver; | |
| padding-top: 0.5em; | |
| margin-top: 4.0em; | |
| } | |
| div#footer-text { | |
| float: left; | |
| padding-bottom: 0.5em; | |
| } | |
| div#footer-badges { | |
| float: right; | |
| padding-bottom: 0.5em; | |
| } | |
| div#preamble { | |
| margin-top: 1.5em; | |
| margin-bottom: 1.5em; | |
| } | |
| div.tableblock, div.imageblock, div.exampleblock, div.verseblock, | |
| div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, | |
| div.admonitionblock { | |
| margin-top: 1.0em; | |
| margin-bottom: 1.5em; | |
| } | |
| div.admonitionblock { | |
| margin-top: 2.0em; | |
| margin-bottom: 2.0em; | |
| margin-right: 10%; | |
| color: #606060; | |
| } | |
| div.content { /* Block element content. */ | |
| padding: 0; | |
| } | |
| /* Block element titles. */ | |
| div.title, caption.title { | |
| color: #527bbd; | |
| font-family: sans-serif; | |
| font-weight: bold; | |
| text-align: left; | |
| margin-top: 1.0em; | |
| margin-bottom: 0.5em; | |
| } | |
| div.title + * { | |
| margin-top: 0; | |
| } | |
| td div.title:first-child { | |
| margin-top: 0.0em; | |
| } | |
| div.content div.title:first-child { | |
| margin-top: 0.0em; | |
| } | |
| div.content + div.title { | |
| margin-top: 0.0em; | |
| } | |
| div.sidebarblock > div.content { | |
| background: #ffffee; | |
| border: 1px solid silver; | |
| padding: 0.5em; | |
| } | |
| div.listingblock > div.content { | |
| border: 1px solid silver; | |
| background: #f4f4f4; | |
| padding: 0.5em; | |
| } | |
| div.quoteblock, div.verseblock { | |
| padding-left: 1.0em; | |
| margin-left: 1.0em; | |
| margin-right: 10%; | |
| border-left: 5px solid #dddddd; | |
| color: #777777; | |
| } | |
| div.quoteblock > div.attribution { | |
| padding-top: 0.5em; | |
| text-align: right; | |
| } | |
| div.verseblock > div.content { | |
| white-space: pre; | |
| } | |
| div.verseblock > div.attribution { | |
| padding-top: 0.75em; | |
| text-align: left; | |
| } | |
| /* DEPRECATED: Pre version 8.2.7 verse style literal block. */ | |
| div.verseblock + div.attribution { | |
| text-align: left; | |
| } | |
| div.admonitionblock .icon { | |
| vertical-align: top; | |
| font-size: 1.1em; | |
| font-weight: bold; | |
| text-decoration: underline; | |
| color: #527bbd; | |
| padding-right: 0.5em; | |
| } | |
| div.admonitionblock td.content { | |
| padding-left: 0.5em; | |
| border-left: 3px solid #dddddd; | |
| } | |
| div.exampleblock > div.content { | |
| border-left: 3px solid #dddddd; | |
| padding-left: 0.5em; | |
| } | |
| div.imageblock div.content { padding-left: 0; } | |
| span.image img { border-style: none; } | |
| a.image:visited { color: white; } | |
| dl { | |
| margin-top: 0.8em; | |
| margin-bottom: 0.8em; | |
| } | |
| dt { | |
| margin-top: 0.5em; | |
| margin-bottom: 0; | |
| font-style: normal; | |
| color: navy; | |
| } | |
| dd > *:first-child { | |
| margin-top: 0.1em; | |
| } | |
| ul, ol { | |
| list-style-position: outside; | |
| } | |
| ol.arabic { | |
| list-style-type: decimal; | |
| } | |
| ol.loweralpha { | |
| list-style-type: lower-alpha; | |
| } | |
| ol.upperalpha { | |
| list-style-type: upper-alpha; | |
| } | |
| ol.lowerroman { | |
| list-style-type: lower-roman; | |
| } | |
| ol.upperroman { | |
| list-style-type: upper-roman; | |
| } | |
| div.compact ul, div.compact ol, | |
| div.compact p, div.compact p, | |
| div.compact div, div.compact div { | |
| margin-top: 0.1em; | |
| margin-bottom: 0.1em; | |
| } | |
| div.tableblock > table { | |
| border: 3px solid #527bbd; | |
| } | |
| thead, p.table.header { | |
| font-family: sans-serif; | |
| font-weight: bold; | |
| } | |
| tfoot { | |
| font-weight: bold; | |
| } | |
| td > div.verse { | |
| white-space: pre; | |
| } | |
| p.table { | |
| margin-top: 0; | |
| } | |
| /* Because the table frame attribute is overriden by CSS in most browsers. */ | |
| div.tableblock > table[frame="void"] { | |
| border-style: none; | |
| } | |
| div.tableblock > table[frame="hsides"] { | |
| border-left-style: none; | |
| border-right-style: none; | |
| } | |
| div.tableblock > table[frame="vsides"] { | |
| border-top-style: none; | |
| border-bottom-style: none; | |
| } | |
| div.hdlist { | |
| margin-top: 0.8em; | |
| margin-bottom: 0.8em; | |
| } | |
| div.hdlist tr { | |
| padding-bottom: 15px; | |
| } | |
| dt.hdlist1.strong, td.hdlist1.strong { | |
| font-weight: bold; | |
| } | |
| td.hdlist1 { | |
| vertical-align: top; | |
| font-style: normal; | |
| padding-right: 0.8em; | |
| color: navy; | |
| } | |
| td.hdlist2 { | |
| vertical-align: top; | |
| } | |
| div.hdlist.compact tr { | |
| margin: 0; | |
| padding-bottom: 0; | |
| } | |
| .comment { | |
| background: yellow; | |
| } | |
| .footnote, .footnoteref { | |
| font-size: 0.8em; | |
| } | |
| span.footnote, span.footnoteref { | |
| vertical-align: super; | |
| } | |
| #footnotes { | |
| margin: 20px 0 20px 0; | |
| padding: 7px 0 0 0; | |
| } | |
| #footnotes div.footnote { | |
| margin: 0 0 5px 0; | |
| } | |
| #footnotes hr { | |
| border: none; | |
| border-top: 1px solid silver; | |
| height: 1px; | |
| text-align: left; | |
| margin-left: 0; | |
| width: 20%; | |
| min-width: 100px; | |
| } | |
| @media print { | |
| div#footer-badges { display: none; } | |
| } | |
| div#toc { | |
| margin-bottom: 2.5em; | |
| } | |
| div#toctitle { | |
| color: #527bbd; | |
| font-family: sans-serif; | |
| font-size: 1.1em; | |
| font-weight: bold; | |
| margin-top: 1.0em; | |
| margin-bottom: 0.1em; | |
| } | |
| div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { | |
| margin-top: 0; | |
| margin-bottom: 0; | |
| } | |
| div.toclevel2 { | |
| margin-left: 2em; | |
| font-size: 0.9em; | |
| } | |
| div.toclevel3 { | |
| margin-left: 4em; | |
| font-size: 0.9em; | |
| } | |
| div.toclevel4 { | |
| margin-left: 6em; | |
| font-size: 0.9em; | |
| } | |
| /* Workarounds for IE6's broken and incomplete CSS2. */ | |
| div.sidebar-content { | |
| background: #ffffee; | |
| border: 1px solid silver; | |
| padding: 0.5em; | |
| } | |
| div.sidebar-title, div.image-title { | |
| color: #527bbd; | |
| font-family: sans-serif; | |
| font-weight: bold; | |
| margin-top: 0.0em; | |
| margin-bottom: 0.5em; | |
| } | |
| div.listingblock div.content { | |
| border: 1px solid silver; | |
| background: #f4f4f4; | |
| padding: 0.5em; | |
| } | |
| div.quoteblock-attribution { | |
| padding-top: 0.5em; | |
| text-align: right; | |
| } | |
| div.verseblock-content { | |
| white-space: pre; | |
| } | |
| div.verseblock-attribution { | |
| padding-top: 0.75em; | |
| text-align: left; | |
| } | |
| div.exampleblock-content { | |
| border-left: 3px solid #dddddd; | |
| padding-left: 0.5em; | |
| } | |
| /* IE6 sets dynamically generated links as visited. */ | |
| div#toc a:visited { color: blue; } | |
| </style> | |
| <script type="text/javascript"> | |
| /*<+'])'); | |
| // Function that scans the DOM tree for header elements (the DOM2 | |
| // nodeIterator API would be a better technique but not supported by all | |
| // browsers). | |
| var iterate = function (el) { | |
| for (var i = el.firstChild; i != null; i = i.nextSibling) { | |
| if (i.nodeType == 1 /* Node.ELEMENT_NODE */) { | |
| var mo = re.exec(i.tagName); | |
| if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") { | |
| result[result.length] = new TocEntry(i, getText(i), mo[1]-1); | |
| } | |
| iterate(i); | |
| } | |
| } | |
| } | |
| iterate(el); | |
| return result; | |
| } | |
| var toc = document.getElementById("toc"); | |
| var entries = tocEntries(document.getElementById("content"), toclevels); | |
| for (var i = 0; i < entries.length; ++i) { | |
| var entry = entries[i]; | |
| if (entry.element.id == "") | |
| entry.element.id = "_toc_" + i; | |
| var a = document.createElement("a"); | |
| a.href = "#" + entry.element.id; | |
| a.appendChild(document.createTextNode(entry.text)); | |
| var div = document.createElement("div"); | |
| div.appendChild(a); | |
| div.className = "toclevel" + entry.toclevel; | |
| toc.appendChild(div); | |
| } | |
| if (entries.length == 0) | |
| toc.parentNode.removeChild(toc); | |
| }, | |
| ///////////////////////////////////////////////////////////////////// | |
| // Footnotes generator | |
| ///////////////////////////////////////////////////////////////////// | |
| /* Based on footnote generation code from: | |
| * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html | |
| */ | |
| footnotes: function () { | |
| var cont = document.getElementById("content"); | |
| var noteholder = document.getElementById("footnotes"); | |
| var spans = cont.getElementsByTagName("span"); | |
| var refs = {}; | |
| var n = 0; | |
| for (i=0; i<spans.length; i++) { | |
| if (spans[i].className == "footnote") { | |
| n++; | |
| // Use [\s\S] in place of . so multi-line matches work. | |
| // Because JavaScript has no s (dotall) regex flag. | |
| note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1]; | |
| noteholder.innerHTML += | |
| "<div class='footnote' id='_footnote_" + n + "'>" + | |
| "<a href='#_footnoteref_" + n + "' title='Return to text'>" + | |
| n + "</a>. " + note + "</div>"; | |
| spans[i].innerHTML = | |
| "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n + | |
| "' title='View footnote' class='footnote'>" + n + "</a>]"; | |
| var id =spans[i].getAttribute("id"); | |
| if (id != null) refs["#"+id] = n; | |
| } | |
| } | |
| if (n == 0) | |
| noteholder.parentNode.removeChild(noteholder); | |
| else { | |
| // Process footnoterefs. | |
| for (i=0; i<spans.length; i++) { | |
| if (spans[i].className == "footnoteref") { | |
| var href = spans[i].getElementsByTagName("a")[0].getAttribute("href"); | |
| href = href.match(/#.*/)[0]; // Because IE return full URL. | |
| n = refs[href]; | |
| spans[i].innerHTML = | |
| "[<a href='#_footnote_" + n + | |
| "' title='View footnote' class='footnote'>" + n + "</a>]"; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| /*]]>*/ | |
| </script> | |
| </head> | |
| <body> | |
| <div id="header"> | |
| <h1>parse-options API</h1> | |
| </div> | |
| <div id="content"> | |
| <div id="preamble"> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>The parse-options API is used to parse and massage options in git | |
| and to provide a usage help with consistent look.</p></div> | |
| </div> | |
| </div> | |
| <h2 id="_basics">Basics</h2> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>The argument vector <tt>argv[]</tt> may usually contain mandatory or optional | |
| <em>non-option arguments</em>, e.g. a filename or a branch, and <em>options</em>. | |
| Options are optional arguments that start with a dash and | |
| that allow to change the behavior of a command.</p></div> | |
| <div class="ulist"><ul> | |
| <li> | |
| <p> | |
| There are basically three types of options: | |
| <em>boolean</em> options, | |
| options with (mandatory) <em>arguments</em> and | |
| options with <em>optional arguments</em> | |
| (i.e. a boolean option that can be adjusted). | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| There are basically two forms of options: | |
| <em>Short options</em> consist of one dash (<tt>-</tt>) and one alphanumeric | |
| character. | |
| <em>Long options</em> begin with two dashes (<tt>--</tt>) and some | |
| alphanumeric characters. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Options are case-sensitive. | |
| Please define <em>lower-case long options</em> only. | |
| </p> | |
| </li> | |
| </ul></div> | |
| <div class="paragraph"><p>The parse-options API allows:</p></div> | |
| <div class="ulist"><ul> | |
| <li> | |
| <p> | |
| <em>sticked</em> and <em>separate form</em> of options with arguments. | |
| <tt>-oArg</tt> is sticked, <tt>-o Arg</tt> is separate form. | |
| <tt>--option=Arg</tt> is sticked, <tt>--option Arg</tt> is separate form. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Long options may be <em>abbreviated</em>, as long as the abbreviation | |
| is unambiguous. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Short options may be bundled, e.g. <tt>-a -b</tt> can be specified as <tt>-ab</tt>. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Boolean long options can be <em>negated</em> (or <em>unset</em>) by prepending | |
| <tt>no-</tt>, e.g. <tt>--no-abbrev</tt> instead of <tt>--abbrev</tt>. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Options and non-option arguments can clearly be separated using the <tt>--</tt> | |
| option, e.g. <tt>-a -b --option -- --this-is-a-file</tt> indicates that | |
| <tt>--this-is-a-file</tt> must not be processed as an option. | |
| </p> | |
| </li> | |
| </ul></div> | |
| </div> | |
| <h2 id="_steps_to_parse_options">Steps to parse options</h2> | |
| <div class="sectionbody"> | |
| <div class="olist arabic"><ol class="arabic"> | |
| <li> | |
| <p> | |
| <tt>#include "parse-options.h"</tt> | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| define a NULL-terminated | |
| <tt>static const char * const builtin_foo_usage[]</tt> array | |
| containing alternative usage strings | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| define <tt>builtin_foo_options</tt> array as described below | |
| in section <em>Data Structure</em>. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| in <tt>cmd_foo(int argc, const char **argv, const char *prefix)</tt> | |
| call | |
| </p> | |
| <div class="literalblock"> | |
| <div class="content"> | |
| <pre><tt>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);</tt></pre> | |
| </div></div> | |
| <div class="paragraph"><p><tt>parse_options()</tt> will filter out the processed options of <tt>argv[]</tt> and leave the | |
| non-option arguments in <tt>argv[]</tt>. | |
| <tt>argc</tt> is updated appropriately because of the assignment.</p></div> | |
| <div class="paragraph"><p>You can also pass NULL instead of a usage array as the fifth parameter of | |
| parse_options(), to avoid displaying a help screen with usage info and | |
| option list. This should only be done if necessary, e.g. to implement | |
| a limited parser for only a subset of the options that needs to be run | |
| before the full parser, which in turn shows the full help message.</p></div> | |
| <div class="paragraph"><p>Flags are the bitwise-or of:</p></div> | |
| <div class="dlist"><dl> | |
| <dt class="hdlist1"> | |
| <tt>PARSE_OPT_KEEP_DASHDASH</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Keep the <tt>--</tt> that usually separates options from | |
| non-option arguments. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>PARSE_OPT_STOP_AT_NON_OPTION</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Usually the whole argument vector is massaged and reordered. | |
| Using this flag, processing is stopped at the first non-option | |
| argument. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>PARSE_OPT_KEEP_ARGV0</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Keep the first argument, which contains the program name. It’s | |
| removed from argv[] by default. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>PARSE_OPT_KEEP_UNKNOWN</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Keep unknown arguments instead of erroring out. This doesn’t | |
| work for all combinations of arguments as users might expect | |
| it to do. E.g. if the first argument in <tt>--unknown --known</tt> | |
| takes a value (which we can’t know), the second one is | |
| mistakenly interpreted as a known option. Similarly, if | |
| <tt>PARSE_OPT_STOP_AT_NON_OPTION</tt> is set, the second argument in | |
| <tt>--unknown value</tt> will be mistakenly interpreted as a | |
| non-option, not as a value belonging to the unknown option, | |
| the parser early. That’s why parse_options() errors out if | |
| both options are set. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>PARSE_OPT_NO_INTERNAL_HELP</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| By default, parse_options() handles <tt>-h</tt>, <tt>--help</tt> and | |
| <tt>--help-all</tt> internally, by showing a help screen. This option | |
| turns it off and allows one to add custom handlers for these | |
| options, or to just leave them unknown. | |
| </p> | |
| </dd> | |
| </dl></div> | |
| </li> | |
| </ol></div> | |
| </div> | |
| <h2 id="_data_structure">Data Structure</h2> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>The main data structure is an array of the <tt>option</tt> struct, | |
| say <tt>static struct option builtin_add_options[]</tt>. | |
| There are some macros to easily define options:</p></div> | |
| <div class="dlist"><dl> | |
| <dt class="hdlist1"> | |
| <tt>OPT__ABBREV(&int_var)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>--abbrev[=<n>]</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT__COLOR(&int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>--color[=<when>]</tt> and <tt>--no-color</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT__DRY_RUN(&int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>-n, --dry-run</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT__FORCE(&int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>-f, --force</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT__QUIET(&int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>-q, --quiet</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT__VERBOSE(&int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Add <tt>-v, --verbose</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_GROUP(description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Start an option group. <tt>description</tt> is a short string that | |
| describes the group or an empty string. | |
| Start the description with an upper-case letter. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_BOOLEAN(short, long, &int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a boolean option. | |
| <tt>int_var</tt> is incremented on each use. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_BIT(short, long, &int_var, description, mask)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a boolean option. | |
| If used, <tt>int_var</tt> is bitwise-ored with <tt>mask</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_NEGBIT(short, long, &int_var, description, mask)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a boolean option. | |
| If used, <tt>int_var</tt> is bitwise-anded with the inverted <tt>mask</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_SET_INT(short, long, &int_var, description, integer)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a boolean option. | |
| If used, set <tt>int_var</tt> to <tt>integer</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_SET_PTR(short, long, &ptr_var, description, ptr)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a boolean option. | |
| If used, set <tt>ptr_var</tt> to <tt>ptr</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_STRING(short, long, &str_var, arg_str, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option with string argument. | |
| The string argument is put into <tt>str_var</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_INTEGER(short, long, &int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option with integer argument. | |
| The integer is put into <tt>int_var</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_DATE(short, long, &int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option with date argument, see <tt>approxidate()</tt>. | |
| The timestamp is put into <tt>int_var</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option with argument. | |
| The argument will be fed into the function given by <tt>func_ptr</tt> | |
| and the result will be put into <tt>var</tt>. | |
| See <em>Option Callbacks</em> below for a more elaborate description. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_FILENAME(short, long, &var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option with a filename argument. | |
| The filename will be prefixed by passing the filename along with | |
| the prefix argument of <tt>parse_options()</tt> to <tt>prefix_filename()</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_ARGUMENT(long, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce a long-option argument that will be kept in <tt>argv[]</tt>. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_NUMBER_CALLBACK(&var, description, func_ptr)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Recognize numerical options like -123 and feed the integer as | |
| if it was an argument to the function given by <tt>func_ptr</tt>. | |
| The result will be put into <tt>var</tt>. There can be only one such | |
| option definition. It cannot be negated and it takes no | |
| arguments. Short options that happen to be digits take | |
| precedence over it. | |
| </p> | |
| </dd> | |
| <dt class="hdlist1"> | |
| <tt>OPT_COLOR_FLAG(short, long, &int_var, description)</tt> | |
| </dt> | |
| <dd> | |
| <p> | |
| Introduce an option that takes an optional argument that can | |
| have one of three values: "always", "never", or "auto". If the | |
| argument is not given, it defaults to "always". The <tt>--no-</tt> form | |
| works like <tt>--long=never</tt>; it cannot take an argument. If | |
| "always", set <tt>int_var</tt> to 1; if "never", set <tt>int_var</tt> to 0; if | |
| "auto", set <tt>int_var</tt> to 1 if stdout is a tty or a pager, | |
| 0 otherwise. | |
| </p> | |
| </dd> | |
| </dl></div> | |
| <div class="paragraph"><p>The last element of the array must be <tt>OPT_END()</tt>.</p></div> | |
| <div class="paragraph"><p>If not stated otherwise, interpret the arguments as follows:</p></div> | |
| <div class="ulist"><ul> | |
| <li> | |
| <p> | |
| <tt>short</tt> is a character for the short option | |
| (e.g. <tt>'e'</tt> for <tt>-e</tt>, use <tt>0</tt> to omit), | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| <tt>long</tt> is a string for the long option | |
| (e.g. <tt>"example"</tt> for <tt>--example</tt>, use <tt>NULL</tt> to omit), | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| <tt>int_var</tt> is an integer variable, | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| <tt>str_var</tt> is a string variable (<tt>char *</tt>), | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| <tt>arg_str</tt> is the string that is shown as argument | |
| (e.g. <tt>"branch"</tt> will result in <tt><branch></tt>). | |
| If set to <tt>NULL</tt>, three dots (<tt>…</tt>) will be displayed. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| <tt>description</tt> is a short string to describe the effect of the option. | |
| It shall begin with a lower-case letter and a full stop (<tt>.</tt>) shall be | |
| omitted at the end. | |
| </p> | |
| </li> | |
| </ul></div> | |
| </div> | |
| <h2 id="_option_callbacks">Option Callbacks</h2> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>The function must be defined in this form:</p></div> | |
| <div class="literalblock"> | |
| <div class="content"> | |
| <pre><tt>int func(const struct option *opt, const char *arg, int unset)</tt></pre> | |
| </div></div> | |
| <div class="paragraph"><p>The callback mechanism is as follows:</p></div> | |
| <div class="ulist"><ul> | |
| <li> | |
| <p> | |
| Inside <tt>func</tt>, the only interesting member of the structure | |
| given by <tt>opt</tt> is the void pointer <tt>opt->value</tt>. | |
| <tt>*opt->value</tt> will be the value that is saved into <tt>var</tt>, if you | |
| use <tt>OPT_CALLBACK()</tt>. | |
| For example, do <tt>*(unsigned long *)opt->value = 42;</tt> to get 42 | |
| into an <tt>unsigned long</tt> variable. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| Return value <tt>0</tt> indicates success and non-zero return | |
| value will invoke <tt>usage_with_options()</tt> and, thus, die. | |
| </p> | |
| </li> | |
| <li> | |
| <p> | |
| If the user negates the option, <tt>arg</tt> is <tt>NULL</tt> and <tt>unset</tt> is 1. | |
| </p> | |
| </li> | |
| </ul></div> | |
| </div> | |
| <h2 id="_sophisticated_option_parsing">Sophisticated option parsing</h2> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>If you need, for example, option callbacks with optional arguments | |
| or without arguments at all, or if you need other special cases, | |
| that are not handled by the macros above, you need to specify the | |
| members of the <tt>option</tt> structure manually.</p></div> | |
| <div class="paragraph"><p>This is not covered in this document, but well documented | |
| in <tt>parse-options.h</tt> itself.</p></div> | |
| </div> | |
| <h2 id="_examples">Examples</h2> | |
| <div class="sectionbody"> | |
| <div class="paragraph"><p>See <tt>test-parse-options.c</tt> and | |
| <tt>builtin-add.c</tt>, | |
| <tt>builtin-clone.c</tt>, | |
| <tt>builtin-commit.c</tt>, | |
| <tt>builtin-fetch.c</tt>, | |
| <tt>builtin-fsck.c</tt>, | |
| <tt>builtin-rm.c</tt> | |
| for real-world examples.</p></div> | |
| </div> | |
| </div> | |
| <div id="footnotes"><hr /></div> | |
| <div id="footer"> | |
| <div id="footer-text"> | |
| Last updated 2011-09-21 23:01:14 PDT | |
| </div> | |
| </div> | |
| </body> | |
| </html> |