blob: 49e8902666cb13b67c30000fbbc4487c3c10749e [file] [log] [blame]
Junio C Hamano3dac5042007-12-15 08:40:541<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
2 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Junio C Hamano97bcb482010-11-25 03:16:076<meta name="generator" content="AsciiDoc 8.4.5" />
7<title>parse-options API</title>
Junio C Hamano3dac5042007-12-15 08:40:548<style type="text/css">
9/* Debug borders */
10p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
11/*
12 border: 1px solid red;
13*/
14}
15
16body {
17 margin: 1em 5% 1em 5%;
18}
19
Junio C Hamano4604fe52008-09-19 06:37:1020a {
21 color: blue;
22 text-decoration: underline;
23}
24a:visited {
25 color: fuchsia;
26}
Junio C Hamano3dac5042007-12-15 08:40:5427
28em {
29 font-style: italic;
Junio C Hamano97bcb482010-11-25 03:16:0730 color: navy;
Junio C Hamano3dac5042007-12-15 08:40:5431}
32
33strong {
34 font-weight: bold;
Junio C Hamano97bcb482010-11-25 03:16:0735 color: #083194;
Junio C Hamano3dac5042007-12-15 08:40:5436}
37
38tt {
39 color: navy;
40}
41
42h1, h2, h3, h4, h5, h6 {
43 color: #527bbd;
44 font-family: sans-serif;
45 margin-top: 1.2em;
46 margin-bottom: 0.5em;
47 line-height: 1.3;
48}
49
Junio C Hamano4604fe52008-09-19 06:37:1050h1, h2, h3 {
Junio C Hamano3dac5042007-12-15 08:40:5451 border-bottom: 2px solid silver;
52}
53h2 {
Junio C Hamano3dac5042007-12-15 08:40:5454 padding-top: 0.5em;
55}
Junio C Hamano4604fe52008-09-19 06:37:1056h3 {
57 float: left;
58}
59h3 + * {
60 clear: left;
61}
Junio C Hamano3dac5042007-12-15 08:40:5462
63div.sectionbody {
64 font-family: serif;
65 margin-left: 0;
66}
67
68hr {
69 border: 1px solid silver;
70}
71
72p {
73 margin-top: 0.5em;
74 margin-bottom: 0.5em;
75}
76
Junio C Hamano97bcb482010-11-25 03:16:0777ul, ol, li > p {
78 margin-top: 0;
79}
80
Junio C Hamano3dac5042007-12-15 08:40:5481pre {
82 padding: 0;
83 margin: 0;
84}
85
86span#author {
87 color: #527bbd;
88 font-family: sans-serif;
89 font-weight: bold;
Junio C Hamano4604fe52008-09-19 06:37:1090 font-size: 1.1em;
Junio C Hamano3dac5042007-12-15 08:40:5491}
92span#email {
93}
Junio C Hamano97bcb482010-11-25 03:16:0794span#revnumber, span#revdate, span#revremark {
Junio C Hamano3dac5042007-12-15 08:40:5495 font-family: sans-serif;
96}
97
98div#footer {
99 font-family: sans-serif;
100 font-size: small;
101 border-top: 2px solid silver;
102 padding-top: 0.5em;
103 margin-top: 4.0em;
104}
105div#footer-text {
106 float: left;
107 padding-bottom: 0.5em;
108}
109div#footer-badges {
110 float: right;
111 padding-bottom: 0.5em;
112}
113
Junio C Hamano97bcb482010-11-25 03:16:07114div#preamble {
115 margin-top: 1.5em;
116 margin-bottom: 1.5em;
117}
Junio C Hamano3dac5042007-12-15 08:40:54118div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
119div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
120div.admonitionblock {
Junio C Hamano3dac5042007-12-15 08:40:54121 margin-top: 1.5em;
122 margin-bottom: 1.5em;
123}
124div.admonitionblock {
125 margin-top: 2.5em;
126 margin-bottom: 2.5em;
127}
128
129div.content { /* Block element content. */
130 padding: 0;
131}
132
133/* Block element titles. */
134div.title, caption.title {
Junio C Hamano97bcb482010-11-25 03:16:07135 color: #527bbd;
Junio C Hamano3dac5042007-12-15 08:40:54136 font-family: sans-serif;
137 font-weight: bold;
138 text-align: left;
139 margin-top: 1.0em;
140 margin-bottom: 0.5em;
141}
142div.title + * {
143 margin-top: 0;
144}
145
146td div.title:first-child {
147 margin-top: 0.0em;
148}
149div.content div.title:first-child {
150 margin-top: 0.0em;
151}
152div.content + div.title {
153 margin-top: 0.0em;
154}
155
156div.sidebarblock > div.content {
157 background: #ffffee;
158 border: 1px solid silver;
159 padding: 0.5em;
160}
161
162div.listingblock > div.content {
163 border: 1px solid silver;
164 background: #f4f4f4;
165 padding: 0.5em;
166}
167
Junio C Hamano97bcb482010-11-25 03:16:07168div.quoteblock {
Junio C Hamano3dac5042007-12-15 08:40:54169 padding-left: 2.0em;
Junio C Hamano97bcb482010-11-25 03:16:07170 margin-right: 10%;
Junio C Hamano3dac5042007-12-15 08:40:54171}
Junio C Hamano97bcb482010-11-25 03:16:07172div.quoteblock > div.attribution {
173 padding-top: 0.5em;
Junio C Hamano3dac5042007-12-15 08:40:54174 text-align: right;
175}
Junio C Hamano97bcb482010-11-25 03:16:07176
177div.verseblock {
178 padding-left: 2.0em;
179 margin-right: 10%;
180}
181div.verseblock > div.content {
182 white-space: pre;
183}
184div.verseblock > div.attribution {
185 padding-top: 0.75em;
186 text-align: left;
187}
188/* DEPRECATED: Pre version 8.2.7 verse style literal block. */
Junio C Hamano4604fe52008-09-19 06:37:10189div.verseblock + div.attribution {
190 text-align: left;
191}
Junio C Hamano3dac5042007-12-15 08:40:54192
193div.admonitionblock .icon {
194 vertical-align: top;
195 font-size: 1.1em;
196 font-weight: bold;
197 text-decoration: underline;
198 color: #527bbd;
199 padding-right: 0.5em;
200}
201div.admonitionblock td.content {
202 padding-left: 0.5em;
203 border-left: 2px solid silver;
204}
205
206div.exampleblock > div.content {
207 border-left: 2px solid silver;
208 padding: 0.5em;
209}
210
Junio C Hamano3dac5042007-12-15 08:40:54211div.imageblock div.content { padding-left: 0; }
Junio C Hamano3dac5042007-12-15 08:40:54212span.image img { border-style: none; }
Junio C Hamano97bcb482010-11-25 03:16:07213a.image:visited { color: white; }
Junio C Hamano3dac5042007-12-15 08:40:54214
215dl {
216 margin-top: 0.8em;
217 margin-bottom: 0.8em;
218}
219dt {
220 margin-top: 0.5em;
221 margin-bottom: 0;
Junio C Hamano97bcb482010-11-25 03:16:07222 font-style: normal;
223 color: navy;
Junio C Hamano3dac5042007-12-15 08:40:54224}
225dd > *:first-child {
Junio C Hamano97bcb482010-11-25 03:16:07226 margin-top: 0.1em;
Junio C Hamano3dac5042007-12-15 08:40:54227}
228
229ul, ol {
230 list-style-position: outside;
231}
Junio C Hamano97bcb482010-11-25 03:16:07232ol.arabic {
233 list-style-type: decimal;
234}
235ol.loweralpha {
Junio C Hamano3dac5042007-12-15 08:40:54236 list-style-type: lower-alpha;
237}
Junio C Hamano97bcb482010-11-25 03:16:07238ol.upperalpha {
239 list-style-type: upper-alpha;
240}
241ol.lowerroman {
242 list-style-type: lower-roman;
243}
244ol.upperroman {
245 list-style-type: upper-roman;
246}
247
248div.compact ul, div.compact ol,
249div.compact p, div.compact p,
250div.compact div, div.compact div {
251 margin-top: 0.1em;
252 margin-bottom: 0.1em;
253}
Junio C Hamano3dac5042007-12-15 08:40:54254
255div.tableblock > table {
Junio C Hamano4604fe52008-09-19 06:37:10256 border: 3px solid #527bbd;
Junio C Hamano3dac5042007-12-15 08:40:54257}
258thead {
259 font-family: sans-serif;
260 font-weight: bold;
261}
262tfoot {
263 font-weight: bold;
264}
Junio C Hamano97bcb482010-11-25 03:16:07265td > div.verse {
266 white-space: pre;
267}
268p.table {
269 margin-top: 0;
270}
271/* Because the table frame attribute is overriden by CSS in most browsers. */
272div.tableblock > table[frame="void"] {
273 border-style: none;
274}
275div.tableblock > table[frame="hsides"] {
276 border-left-style: none;
277 border-right-style: none;
278}
279div.tableblock > table[frame="vsides"] {
280 border-top-style: none;
281 border-bottom-style: none;
282}
Junio C Hamano3dac5042007-12-15 08:40:54283
Junio C Hamano97bcb482010-11-25 03:16:07284
285div.hdlist {
Junio C Hamano3dac5042007-12-15 08:40:54286 margin-top: 0.8em;
287 margin-bottom: 0.8em;
288}
Junio C Hamano97bcb482010-11-25 03:16:07289div.hdlist tr {
290 padding-bottom: 15px;
Junio C Hamano4604fe52008-09-19 06:37:10291}
Junio C Hamano97bcb482010-11-25 03:16:07292dt.hdlist1.strong, td.hdlist1.strong {
293 font-weight: bold;
294}
295td.hdlist1 {
Junio C Hamano3dac5042007-12-15 08:40:54296 vertical-align: top;
Junio C Hamano97bcb482010-11-25 03:16:07297 font-style: normal;
Junio C Hamano3dac5042007-12-15 08:40:54298 padding-right: 0.8em;
Junio C Hamano97bcb482010-11-25 03:16:07299 color: navy;
Junio C Hamano3dac5042007-12-15 08:40:54300}
Junio C Hamano97bcb482010-11-25 03:16:07301td.hdlist2 {
Junio C Hamano3dac5042007-12-15 08:40:54302 vertical-align: top;
303}
Junio C Hamano97bcb482010-11-25 03:16:07304div.hdlist.compact tr {
305 margin: 0;
306 padding-bottom: 0;
307}
308
309.comment {
310 background: yellow;
311}
Junio C Hamano3dac5042007-12-15 08:40:54312
313@media print {
314 div#footer-badges { display: none; }
315}
Junio C Hamano4604fe52008-09-19 06:37:10316
317div#toctitle {
318 color: #527bbd;
319 font-family: sans-serif;
320 font-size: 1.1em;
321 font-weight: bold;
322 margin-top: 1.0em;
323 margin-bottom: 0.1em;
324}
325
326div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
327 margin-top: 0;
328 margin-bottom: 0;
329}
330div.toclevel2 {
331 margin-left: 2em;
332 font-size: 0.9em;
333}
334div.toclevel3 {
335 margin-left: 4em;
336 font-size: 0.9em;
337}
338div.toclevel4 {
339 margin-left: 6em;
340 font-size: 0.9em;
341}
Junio C Hamano3dac5042007-12-15 08:40:54342/* Workarounds for IE6's broken and incomplete CSS2. */
343
344div.sidebar-content {
345 background: #ffffee;
346 border: 1px solid silver;
347 padding: 0.5em;
348}
349div.sidebar-title, div.image-title {
Junio C Hamano97bcb482010-11-25 03:16:07350 color: #527bbd;
Junio C Hamano3dac5042007-12-15 08:40:54351 font-family: sans-serif;
352 font-weight: bold;
353 margin-top: 0.0em;
354 margin-bottom: 0.5em;
355}
356
357div.listingblock div.content {
358 border: 1px solid silver;
359 background: #f4f4f4;
360 padding: 0.5em;
361}
362
Junio C Hamano97bcb482010-11-25 03:16:07363div.quoteblock-attribution {
364 padding-top: 0.5em;
365 text-align: right;
366}
367
368div.verseblock-content {
369 white-space: pre;
370}
371div.verseblock-attribution {
372 padding-top: 0.75em;
373 text-align: left;
Junio C Hamano3dac5042007-12-15 08:40:54374}
375
376div.exampleblock-content {
377 border-left: 2px solid silver;
378 padding-left: 0.5em;
379}
Junio C Hamano4604fe52008-09-19 06:37:10380
381/* IE6 sets dynamically generated links as visited. */
382div#toc a:visited { color: blue; }
Junio C Hamano3dac5042007-12-15 08:40:54383</style>
Junio C Hamano3dac5042007-12-15 08:40:54384</head>
385<body>
386<div id="header">
387<h1>parse-options API</h1>
388</div>
389<div id="preamble">
390<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07391<div class="paragraph"><p>The parse-options API is used to parse and massage options in git
Junio C Hamano4604fe52008-09-19 06:37:10392and to provide a usage help with consistent look.</p></div>
Junio C Hamano3dac5042007-12-15 08:40:54393</div>
394</div>
Junio C Hamano4604fe52008-09-19 06:37:10395<h2 id="_basics">Basics</h2>
Junio C Hamano4224f992008-06-23 07:14:08396<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07397<div class="paragraph"><p>The argument vector <tt>argv[]</tt> may usually contain mandatory or optional
Junio C Hamano4224f992008-06-23 07:14:08398<em>non-option arguments</em>, e.g. a filename or a branch, and <em>options</em>.
399Options are optional arguments that start with a dash and
Junio C Hamano4604fe52008-09-19 06:37:10400that allow to change the behavior of a command.</p></div>
Junio C Hamano97bcb482010-11-25 03:16:07401<div class="ulist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08402<li>
403<p>
404There are basically three types of options:
405 <em>boolean</em> options,
406 options with (mandatory) <em>arguments</em> and
407 options with <em>optional arguments</em>
408 (i.e. a boolean option that can be adjusted).
409</p>
410</li>
411<li>
412<p>
413There are basically two forms of options:
414 <em>Short options</em> consist of one dash (<tt>-</tt>) and one alphanumeric
415 character.
416 <em>Long options</em> begin with two dashes (<tt>--</tt>) and some
417 alphanumeric characters.
418</p>
419</li>
420<li>
421<p>
422Options are case-sensitive.
423 Please define <em>lower-case long options</em> only.
424</p>
425</li>
Junio C Hamano4604fe52008-09-19 06:37:10426</ul></div>
Junio C Hamano97bcb482010-11-25 03:16:07427<div class="paragraph"><p>The parse-options API allows:</p></div>
428<div class="ulist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08429<li>
430<p>
431<em>sticked</em> and <em>separate form</em> of options with arguments.
432 <tt>-oArg</tt> is sticked, <tt>-o Arg</tt> is separate form.
433 <tt>--option=Arg</tt> is sticked, <tt>--option Arg</tt> is separate form.
434</p>
435</li>
436<li>
437<p>
438Long options may be <em>abbreviated</em>, as long as the abbreviation
439 is unambiguous.
440</p>
441</li>
442<li>
443<p>
444Short options may be bundled, e.g. <tt>-a -b</tt> can be specified as <tt>-ab</tt>.
445</p>
446</li>
447<li>
448<p>
449Boolean long options can be <em>negated</em> (or <em>unset</em>) by prepending
450 <tt>no-</tt>, e.g. <tt>--no-abbrev</tt> instead of <tt>--abbrev</tt>.
451</p>
452</li>
453<li>
454<p>
455Options and non-option arguments can clearly be separated using the <tt>--</tt>
456 option, e.g. <tt>-a -b --option -- --this-is-a-file</tt> indicates that
457 <tt>--this-is-a-file</tt> must not be processed as an option.
458</p>
459</li>
Junio C Hamano4604fe52008-09-19 06:37:10460</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08461</div>
Junio C Hamano4604fe52008-09-19 06:37:10462<h2 id="_steps_to_parse_options">Steps to parse options</h2>
Junio C Hamano4224f992008-06-23 07:14:08463<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07464<div class="olist arabic"><ol class="arabic">
Junio C Hamano4224f992008-06-23 07:14:08465<li>
466<p>
467<tt>#include "parse-options.h"</tt>
468</p>
469</li>
470<li>
471<p>
472define a NULL-terminated
473 <tt>static const char * const builtin_foo_usage[]</tt> array
474 containing alternative usage strings
475</p>
476</li>
477<li>
478<p>
479define <tt>builtin_foo_options</tt> array as described below
480 in section <em>Data Structure</em>.
481</p>
482</li>
483<li>
484<p>
485in <tt>cmd_foo(int argc, const char **argv, const char *prefix)</tt>
486 call
487</p>
488<div class="literalblock">
489<div class="content">
Junio C Hamano3d141512009-06-01 01:22:40490<pre><tt>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);</tt></pre>
Junio C Hamano4224f992008-06-23 07:14:08491</div></div>
Junio C Hamano97bcb482010-11-25 03:16:07492<div class="paragraph"><p><tt>parse_options()</tt> will filter out the processed options of <tt>argv[]</tt> and leave the
Junio C Hamano4224f992008-06-23 07:14:08493non-option arguments in <tt>argv[]</tt>.
Junio C Hamano4604fe52008-09-19 06:37:10494<tt>argc</tt> is updated appropriately because of the assignment.</p></div>
Junio C Hamano97bcb482010-11-25 03:16:07495<div class="paragraph"><p>You can also pass NULL instead of a usage array as the fifth parameter of
Junio C Hamano18f51402009-03-10 16:39:13496parse_options(), to avoid displaying a help screen with usage info and
497option list. This should only be done if necessary, e.g. to implement
498a limited parser for only a subset of the options that needs to be run
499before the full parser, which in turn shows the full help message.</p></div>
Junio C Hamano97bcb482010-11-25 03:16:07500<div class="paragraph"><p>Flags are the bitwise-or of:</p></div>
501<div class="dlist"><dl>
502<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08503<tt>PARSE_OPT_KEEP_DASHDASH</tt>
504</dt>
505<dd>
506<p>
507 Keep the <tt>--</tt> that usually separates options from
508 non-option arguments.
509</p>
510</dd>
Junio C Hamano97bcb482010-11-25 03:16:07511<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08512<tt>PARSE_OPT_STOP_AT_NON_OPTION</tt>
513</dt>
514<dd>
515<p>
516 Usually the whole argument vector is massaged and reordered.
517 Using this flag, processing is stopped at the first non-option
518 argument.
519</p>
520</dd>
Junio C Hamano97bcb482010-11-25 03:16:07521<dt class="hdlist1">
Junio C Hamano18f51402009-03-10 16:39:13522<tt>PARSE_OPT_KEEP_ARGV0</tt>
523</dt>
524<dd>
525<p>
Junio C Hamano97bcb482010-11-25 03:16:07526 Keep the first argument, which contains the program name. It&#8217;s
Junio C Hamano18f51402009-03-10 16:39:13527 removed from argv[] by default.
528</p>
529</dd>
Junio C Hamano97bcb482010-11-25 03:16:07530<dt class="hdlist1">
Junio C Hamano18f51402009-03-10 16:39:13531<tt>PARSE_OPT_KEEP_UNKNOWN</tt>
532</dt>
533<dd>
534<p>
Junio C Hamano97bcb482010-11-25 03:16:07535 Keep unknown arguments instead of erroring out. This doesn&#8217;t
Junio C Hamano18f51402009-03-10 16:39:13536 work for all combinations of arguments as users might expect
Junio C Hamano97bcb482010-11-25 03:16:07537 it to do. E.g. if the first argument in <tt>--unknown --known</tt>
538 takes a value (which we can&#8217;t know), the second one is
Junio C Hamano18f51402009-03-10 16:39:13539 mistakenly interpreted as a known option. Similarly, if
540 <tt>PARSE_OPT_STOP_AT_NON_OPTION</tt> is set, the second argument in
Junio C Hamano97bcb482010-11-25 03:16:07541 <tt>--unknown value</tt> will be mistakenly interpreted as a
Junio C Hamano18f51402009-03-10 16:39:13542 non-option, not as a value belonging to the unknown option,
Junio C Hamano97bcb482010-11-25 03:16:07543 the parser early. That&#8217;s why parse_options() errors out if
Junio C Hamano18f51402009-03-10 16:39:13544 both options are set.
545</p>
546</dd>
Junio C Hamano97bcb482010-11-25 03:16:07547<dt class="hdlist1">
Junio C Hamano18f51402009-03-10 16:39:13548<tt>PARSE_OPT_NO_INTERNAL_HELP</tt>
549</dt>
550<dd>
551<p>
Junio C Hamano97bcb482010-11-25 03:16:07552 By default, parse_options() handles <tt>-h</tt>, <tt>--help</tt> and
553 <tt>--help-all</tt> internally, by showing a help screen. This option
Junio C Hamano18f51402009-03-10 16:39:13554 turns it off and allows one to add custom handlers for these
555 options, or to just leave them unknown.
556</p>
557</dd>
Junio C Hamano4604fe52008-09-19 06:37:10558</dl></div>
Junio C Hamano4224f992008-06-23 07:14:08559</li>
Junio C Hamano4604fe52008-09-19 06:37:10560</ol></div>
Junio C Hamano4224f992008-06-23 07:14:08561</div>
Junio C Hamano4604fe52008-09-19 06:37:10562<h2 id="_data_structure">Data Structure</h2>
Junio C Hamano4224f992008-06-23 07:14:08563<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07564<div class="paragraph"><p>The main data structure is an array of the <tt>option</tt> struct,
Junio C Hamano4224f992008-06-23 07:14:08565say <tt>static struct option builtin_add_options[]</tt>.
Junio C Hamano4604fe52008-09-19 06:37:10566There are some macros to easily define options:</p></div>
Junio C Hamano97bcb482010-11-25 03:16:07567<div class="dlist"><dl>
568<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08569<tt>OPT__ABBREV(&amp;int_var)</tt>
570</dt>
571<dd>
572<p>
573 Add <tt>--abbrev[=&lt;n&gt;]</tt>.
574</p>
575</dd>
Junio C Hamano97bcb482010-11-25 03:16:07576<dt class="hdlist1">
Junio C Hamano4aa0bcc2010-03-03 05:13:12577<tt>OPT__COLOR(&amp;int_var, description)</tt>
578</dt>
579<dd>
580<p>
Junio C Hamano97bcb482010-11-25 03:16:07581 Add <tt>--color[=&lt;when&gt;]</tt> and <tt>--no-color</tt>.
Junio C Hamano4aa0bcc2010-03-03 05:13:12582</p>
583</dd>
Junio C Hamano97bcb482010-11-25 03:16:07584<dt class="hdlist1">
585<tt>OPT__DRY_RUN(&amp;int_var, description)</tt>
Junio C Hamano4224f992008-06-23 07:14:08586</dt>
587<dd>
588<p>
589 Add <tt>-n, --dry-run</tt>.
590</p>
591</dd>
Junio C Hamano97bcb482010-11-25 03:16:07592<dt class="hdlist1">
593<tt>OPT__FORCE(&amp;int_var, description)</tt>
594</dt>
595<dd>
596<p>
597 Add <tt>-f, --force</tt>.
598</p>
599</dd>
600<dt class="hdlist1">
601<tt>OPT__QUIET(&amp;int_var, description)</tt>
Junio C Hamano4224f992008-06-23 07:14:08602</dt>
603<dd>
604<p>
605 Add <tt>-q, --quiet</tt>.
606</p>
607</dd>
Junio C Hamano97bcb482010-11-25 03:16:07608<dt class="hdlist1">
609<tt>OPT__VERBOSE(&amp;int_var, description)</tt>
Junio C Hamano4224f992008-06-23 07:14:08610</dt>
611<dd>
612<p>
613 Add <tt>-v, --verbose</tt>.
614</p>
615</dd>
Junio C Hamano97bcb482010-11-25 03:16:07616<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08617<tt>OPT_GROUP(description)</tt>
618</dt>
619<dd>
620<p>
621 Start an option group. <tt>description</tt> is a short string that
622 describes the group or an empty string.
623 Start the description with an upper-case letter.
624</p>
625</dd>
Junio C Hamano97bcb482010-11-25 03:16:07626<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08627<tt>OPT_BOOLEAN(short, long, &amp;int_var, description)</tt>
628</dt>
629<dd>
630<p>
631 Introduce a boolean option.
632 <tt>int_var</tt> is incremented on each use.
633</p>
634</dd>
Junio C Hamano97bcb482010-11-25 03:16:07635<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08636<tt>OPT_BIT(short, long, &amp;int_var, description, mask)</tt>
637</dt>
638<dd>
639<p>
640 Introduce a boolean option.
641 If used, <tt>int_var</tt> is bitwise-ored with <tt>mask</tt>.
642</p>
643</dd>
Junio C Hamano97bcb482010-11-25 03:16:07644<dt class="hdlist1">
Junio C Hamano7ae18252009-05-23 09:53:32645<tt>OPT_NEGBIT(short, long, &amp;int_var, description, mask)</tt>
646</dt>
647<dd>
648<p>
649 Introduce a boolean option.
650 If used, <tt>int_var</tt> is bitwise-anded with the inverted <tt>mask</tt>.
651</p>
652</dd>
Junio C Hamano97bcb482010-11-25 03:16:07653<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08654<tt>OPT_SET_INT(short, long, &amp;int_var, description, integer)</tt>
655</dt>
656<dd>
657<p>
658 Introduce a boolean option.
659 If used, set <tt>int_var</tt> to <tt>integer</tt>.
660</p>
661</dd>
Junio C Hamano97bcb482010-11-25 03:16:07662<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08663<tt>OPT_SET_PTR(short, long, &amp;ptr_var, description, ptr)</tt>
664</dt>
665<dd>
666<p>
667 Introduce a boolean option.
668 If used, set <tt>ptr_var</tt> to <tt>ptr</tt>.
669</p>
670</dd>
Junio C Hamano97bcb482010-11-25 03:16:07671<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08672<tt>OPT_STRING(short, long, &amp;str_var, arg_str, description)</tt>
673</dt>
674<dd>
675<p>
676 Introduce an option with string argument.
677 The string argument is put into <tt>str_var</tt>.
678</p>
679</dd>
Junio C Hamano97bcb482010-11-25 03:16:07680<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08681<tt>OPT_INTEGER(short, long, &amp;int_var, description)</tt>
682</dt>
683<dd>
684<p>
685 Introduce an option with integer argument.
686 The integer is put into <tt>int_var</tt>.
687</p>
688</dd>
Junio C Hamano97bcb482010-11-25 03:16:07689<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08690<tt>OPT_DATE(short, long, &amp;int_var, description)</tt>
691</dt>
692<dd>
693<p>
694 Introduce an option with date argument, see <tt>approxidate()</tt>.
695 The timestamp is put into <tt>int_var</tt>.
696</p>
697</dd>
Junio C Hamano97bcb482010-11-25 03:16:07698<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08699<tt>OPT_CALLBACK(short, long, &amp;var, arg_str, description, func_ptr)</tt>
700</dt>
701<dd>
702<p>
703 Introduce an option with argument.
704 The argument will be fed into the function given by <tt>func_ptr</tt>
705 and the result will be put into <tt>var</tt>.
706 See <em>Option Callbacks</em> below for a more elaborate description.
707</p>
708</dd>
Junio C Hamano97bcb482010-11-25 03:16:07709<dt class="hdlist1">
Junio C Hamano3d141512009-06-01 01:22:40710<tt>OPT_FILENAME(short, long, &amp;var, description)</tt>
711</dt>
712<dd>
713<p>
714 Introduce an option with a filename argument.
715 The filename will be prefixed by passing the filename along with
716 the prefix argument of <tt>parse_options()</tt> to <tt>prefix_filename()</tt>.
717</p>
718</dd>
Junio C Hamano97bcb482010-11-25 03:16:07719<dt class="hdlist1">
Junio C Hamano4224f992008-06-23 07:14:08720<tt>OPT_ARGUMENT(long, description)</tt>
721</dt>
722<dd>
723<p>
724 Introduce a long-option argument that will be kept in <tt>argv[]</tt>.
725</p>
726</dd>
Junio C Hamano97bcb482010-11-25 03:16:07727<dt class="hdlist1">
Junio C Hamano7ae18252009-05-23 09:53:32728<tt>OPT_NUMBER_CALLBACK(&amp;var, description, func_ptr)</tt>
729</dt>
730<dd>
731<p>
732 Recognize numerical options like -123 and feed the integer as
733 if it was an argument to the function given by <tt>func_ptr</tt>.
734 The result will be put into <tt>var</tt>. There can be only one such
735 option definition. It cannot be negated and it takes no
736 arguments. Short options that happen to be digits take
737 precedence over it.
738</p>
739</dd>
Junio C Hamano97bcb482010-11-25 03:16:07740<dt class="hdlist1">
Junio C Hamano4aa0bcc2010-03-03 05:13:12741<tt>OPT_COLOR_FLAG(short, long, &amp;int_var, description)</tt>
742</dt>
743<dd>
744<p>
745 Introduce an option that takes an optional argument that can
746 have one of three values: "always", "never", or "auto". If the
Junio C Hamano97bcb482010-11-25 03:16:07747 argument is not given, it defaults to "always". The <tt>--no-</tt> form
748 works like <tt>--long=never</tt>; it cannot take an argument. If
Junio C Hamano4aa0bcc2010-03-03 05:13:12749 "always", set <tt>int_var</tt> to 1; if "never", set <tt>int_var</tt> to 0; if
750 "auto", set <tt>int_var</tt> to 1 if stdout is a tty or a pager,
751 0 otherwise.
752</p>
753</dd>
Junio C Hamano4604fe52008-09-19 06:37:10754</dl></div>
Junio C Hamano97bcb482010-11-25 03:16:07755<div class="paragraph"><p>The last element of the array must be <tt>OPT_END()</tt>.</p></div>
756<div class="paragraph"><p>If not stated otherwise, interpret the arguments as follows:</p></div>
757<div class="ulist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08758<li>
759<p>
760<tt>short</tt> is a character for the short option
Junio C Hamano2db3e752010-09-03 21:33:06761 (e.g. <tt>&#39;e&#39;</tt> for <tt>-e</tt>, use <tt>0</tt> to omit),
Junio C Hamano4224f992008-06-23 07:14:08762</p>
763</li>
764<li>
765<p>
766<tt>long</tt> is a string for the long option
767 (e.g. <tt>"example"</tt> for <tt>--example</tt>, use <tt>NULL</tt> to omit),
768</p>
769</li>
770<li>
771<p>
772<tt>int_var</tt> is an integer variable,
773</p>
774</li>
775<li>
776<p>
777<tt>str_var</tt> is a string variable (<tt>char *</tt>),
778</p>
779</li>
780<li>
781<p>
782<tt>arg_str</tt> is the string that is shown as argument
783 (e.g. <tt>"branch"</tt> will result in <tt>&lt;branch&gt;</tt>).
784 If set to <tt>NULL</tt>, three dots (<tt>&#8230;</tt>) will be displayed.
785</p>
786</li>
787<li>
788<p>
789<tt>description</tt> is a short string to describe the effect of the option.
790 It shall begin with a lower-case letter and a full stop (<tt>.</tt>) shall be
791 omitted at the end.
792</p>
793</li>
Junio C Hamano4604fe52008-09-19 06:37:10794</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08795</div>
Junio C Hamano4604fe52008-09-19 06:37:10796<h2 id="_option_callbacks">Option Callbacks</h2>
Junio C Hamano4224f992008-06-23 07:14:08797<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07798<div class="paragraph"><p>The function must be defined in this form:</p></div>
Junio C Hamano4224f992008-06-23 07:14:08799<div class="literalblock">
800<div class="content">
801<pre><tt>int func(const struct option *opt, const char *arg, int unset)</tt></pre>
802</div></div>
Junio C Hamano97bcb482010-11-25 03:16:07803<div class="paragraph"><p>The callback mechanism is as follows:</p></div>
804<div class="ulist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08805<li>
806<p>
Junio C Hamanod4f35a92009-05-17 05:39:29807Inside <tt>func</tt>, the only interesting member of the structure
Junio C Hamano97bcb482010-11-25 03:16:07808 given by <tt>opt</tt> is the void pointer <tt>opt-&gt;value</tt>.
809 <tt>*opt-&gt;value</tt> will be the value that is saved into <tt>var</tt>, if you
Junio C Hamano4224f992008-06-23 07:14:08810 use <tt>OPT_CALLBACK()</tt>.
Junio C Hamano97bcb482010-11-25 03:16:07811 For example, do <tt>*(unsigned long *)opt-&gt;value = 42;</tt> to get 42
Junio C Hamano4224f992008-06-23 07:14:08812 into an <tt>unsigned long</tt> variable.
813</p>
814</li>
815<li>
816<p>
817Return value <tt>0</tt> indicates success and non-zero return
818 value will invoke <tt>usage_with_options()</tt> and, thus, die.
819</p>
820</li>
821<li>
822<p>
823If the user negates the option, <tt>arg</tt> is <tt>NULL</tt> and <tt>unset</tt> is 1.
824</p>
825</li>
Junio C Hamano4604fe52008-09-19 06:37:10826</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08827</div>
Junio C Hamano4604fe52008-09-19 06:37:10828<h2 id="_sophisticated_option_parsing">Sophisticated option parsing</h2>
Junio C Hamano4224f992008-06-23 07:14:08829<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07830<div class="paragraph"><p>If you need, for example, option callbacks with optional arguments
Junio C Hamano4224f992008-06-23 07:14:08831or without arguments at all, or if you need other special cases,
832that are not handled by the macros above, you need to specify the
Junio C Hamano4604fe52008-09-19 06:37:10833members of the <tt>option</tt> structure manually.</p></div>
Junio C Hamano97bcb482010-11-25 03:16:07834<div class="paragraph"><p>This is not covered in this document, but well documented
Junio C Hamano4604fe52008-09-19 06:37:10835in <tt>parse-options.h</tt> itself.</p></div>
Junio C Hamano4224f992008-06-23 07:14:08836</div>
Junio C Hamano4604fe52008-09-19 06:37:10837<h2 id="_examples">Examples</h2>
Junio C Hamano4224f992008-06-23 07:14:08838<div class="sectionbody">
Junio C Hamano97bcb482010-11-25 03:16:07839<div class="paragraph"><p>See <tt>test-parse-options.c</tt> and
Junio C Hamano4224f992008-06-23 07:14:08840<tt>builtin-add.c</tt>,
841<tt>builtin-clone.c</tt>,
842<tt>builtin-commit.c</tt>,
843<tt>builtin-fetch.c</tt>,
844<tt>builtin-fsck.c</tt>,
845<tt>builtin-rm.c</tt>
Junio C Hamano4604fe52008-09-19 06:37:10846for real-world examples.</p></div>
Junio C Hamano4224f992008-06-23 07:14:08847</div>
Junio C Hamano3dac5042007-12-15 08:40:54848<div id="footer">
849<div id="footer-text">
Junio C Hamano97bcb482010-11-25 03:16:07850Last updated 2010-11-25 03:13:43 UTC
Junio C Hamano3dac5042007-12-15 08:40:54851</div>
852</div>
853</body>
854</html>