blob: 342e1094049ad45c72e2a23d6c9c4b4b6c7bb501 [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 Hamano4604fe52008-09-19 06:37:106<meta name="generator" content="AsciiDoc 8.2.5" />
Junio C Hamano3dac5042007-12-15 08:40:547<style type="text/css">
8/* Debug borders */
9p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 {
10/*
11 border: 1px solid red;
12*/
13}
14
15body {
16 margin: 1em 5% 1em 5%;
17}
18
Junio C Hamano4604fe52008-09-19 06:37:1019a {
20 color: blue;
21 text-decoration: underline;
22}
23a:visited {
24 color: fuchsia;
25}
Junio C Hamano3dac5042007-12-15 08:40:5426
27em {
28 font-style: italic;
29}
30
31strong {
32 font-weight: bold;
33}
34
35tt {
36 color: navy;
37}
38
39h1, h2, h3, h4, h5, h6 {
40 color: #527bbd;
41 font-family: sans-serif;
42 margin-top: 1.2em;
43 margin-bottom: 0.5em;
44 line-height: 1.3;
45}
46
Junio C Hamano4604fe52008-09-19 06:37:1047h1, h2, h3 {
Junio C Hamano3dac5042007-12-15 08:40:5448 border-bottom: 2px solid silver;
49}
50h2 {
Junio C Hamano3dac5042007-12-15 08:40:5451 padding-top: 0.5em;
52}
Junio C Hamano4604fe52008-09-19 06:37:1053h3 {
54 float: left;
55}
56h3 + * {
57 clear: left;
58}
Junio C Hamano3dac5042007-12-15 08:40:5459
60div.sectionbody {
61 font-family: serif;
62 margin-left: 0;
63}
64
65hr {
66 border: 1px solid silver;
67}
68
69p {
70 margin-top: 0.5em;
71 margin-bottom: 0.5em;
72}
73
74pre {
75 padding: 0;
76 margin: 0;
77}
78
79span#author {
80 color: #527bbd;
81 font-family: sans-serif;
82 font-weight: bold;
Junio C Hamano4604fe52008-09-19 06:37:1083 font-size: 1.1em;
Junio C Hamano3dac5042007-12-15 08:40:5484}
85span#email {
86}
87span#revision {
88 font-family: sans-serif;
89}
90
91div#footer {
92 font-family: sans-serif;
93 font-size: small;
94 border-top: 2px solid silver;
95 padding-top: 0.5em;
96 margin-top: 4.0em;
97}
98div#footer-text {
99 float: left;
100 padding-bottom: 0.5em;
101}
102div#footer-badges {
103 float: right;
104 padding-bottom: 0.5em;
105}
106
107div#preamble,
108div.tableblock, div.imageblock, div.exampleblock, div.verseblock,
109div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
110div.admonitionblock {
111 margin-right: 10%;
112 margin-top: 1.5em;
113 margin-bottom: 1.5em;
114}
115div.admonitionblock {
116 margin-top: 2.5em;
117 margin-bottom: 2.5em;
118}
119
120div.content { /* Block element content. */
121 padding: 0;
122}
123
124/* Block element titles. */
125div.title, caption.title {
126 font-family: sans-serif;
127 font-weight: bold;
128 text-align: left;
129 margin-top: 1.0em;
130 margin-bottom: 0.5em;
131}
132div.title + * {
133 margin-top: 0;
134}
135
136td div.title:first-child {
137 margin-top: 0.0em;
138}
139div.content div.title:first-child {
140 margin-top: 0.0em;
141}
142div.content + div.title {
143 margin-top: 0.0em;
144}
145
146div.sidebarblock > div.content {
147 background: #ffffee;
148 border: 1px solid silver;
149 padding: 0.5em;
150}
151
Junio C Hamano4604fe52008-09-19 06:37:10152div.listingblock {
153 margin-right: 0%;
154}
Junio C Hamano3dac5042007-12-15 08:40:54155div.listingblock > div.content {
156 border: 1px solid silver;
157 background: #f4f4f4;
158 padding: 0.5em;
159}
160
161div.quoteblock > div.content {
162 padding-left: 2.0em;
163}
Junio C Hamano4604fe52008-09-19 06:37:10164
165div.attribution {
Junio C Hamano3dac5042007-12-15 08:40:54166 text-align: right;
167}
Junio C Hamano4604fe52008-09-19 06:37:10168div.verseblock + div.attribution {
169 text-align: left;
170}
Junio C Hamano3dac5042007-12-15 08:40:54171
172div.admonitionblock .icon {
173 vertical-align: top;
174 font-size: 1.1em;
175 font-weight: bold;
176 text-decoration: underline;
177 color: #527bbd;
178 padding-right: 0.5em;
179}
180div.admonitionblock td.content {
181 padding-left: 0.5em;
182 border-left: 2px solid silver;
183}
184
185div.exampleblock > div.content {
186 border-left: 2px solid silver;
187 padding: 0.5em;
188}
189
190div.verseblock div.content {
191 white-space: pre;
192}
193
194div.imageblock div.content { padding-left: 0; }
195div.imageblock img { border: 1px solid silver; }
196span.image img { border-style: none; }
197
198dl {
199 margin-top: 0.8em;
200 margin-bottom: 0.8em;
201}
202dt {
203 margin-top: 0.5em;
204 margin-bottom: 0;
205 font-style: italic;
206}
207dd > *:first-child {
208 margin-top: 0;
209}
210
211ul, ol {
212 list-style-position: outside;
213}
Junio C Hamano4604fe52008-09-19 06:37:10214div.olist2 ol {
Junio C Hamano3dac5042007-12-15 08:40:54215 list-style-type: lower-alpha;
216}
217
218div.tableblock > table {
Junio C Hamano4604fe52008-09-19 06:37:10219 border: 3px solid #527bbd;
Junio C Hamano3dac5042007-12-15 08:40:54220}
221thead {
222 font-family: sans-serif;
223 font-weight: bold;
224}
225tfoot {
226 font-weight: bold;
227}
228
229div.hlist {
230 margin-top: 0.8em;
231 margin-bottom: 0.8em;
232}
Junio C Hamano4604fe52008-09-19 06:37:10233div.hlist td {
234 padding-bottom: 5px;
235}
Junio C Hamano3dac5042007-12-15 08:40:54236td.hlist1 {
237 vertical-align: top;
238 font-style: italic;
239 padding-right: 0.8em;
240}
241td.hlist2 {
242 vertical-align: top;
243}
244
245@media print {
246 div#footer-badges { display: none; }
247}
Junio C Hamano4604fe52008-09-19 06:37:10248
249div#toctitle {
250 color: #527bbd;
251 font-family: sans-serif;
252 font-size: 1.1em;
253 font-weight: bold;
254 margin-top: 1.0em;
255 margin-bottom: 0.1em;
256}
257
258div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
259 margin-top: 0;
260 margin-bottom: 0;
261}
262div.toclevel2 {
263 margin-left: 2em;
264 font-size: 0.9em;
265}
266div.toclevel3 {
267 margin-left: 4em;
268 font-size: 0.9em;
269}
270div.toclevel4 {
271 margin-left: 6em;
272 font-size: 0.9em;
273}
Junio C Hamano3dac5042007-12-15 08:40:54274/* Workarounds for IE6's broken and incomplete CSS2. */
275
276div.sidebar-content {
277 background: #ffffee;
278 border: 1px solid silver;
279 padding: 0.5em;
280}
281div.sidebar-title, div.image-title {
282 font-family: sans-serif;
283 font-weight: bold;
284 margin-top: 0.0em;
285 margin-bottom: 0.5em;
286}
287
288div.listingblock div.content {
289 border: 1px solid silver;
290 background: #f4f4f4;
291 padding: 0.5em;
292}
293
294div.quoteblock-content {
295 padding-left: 2.0em;
296}
297
298div.exampleblock-content {
299 border-left: 2px solid silver;
300 padding-left: 0.5em;
301}
Junio C Hamano4604fe52008-09-19 06:37:10302
303/* IE6 sets dynamically generated links as visited. */
304div#toc a:visited { color: blue; }
Junio C Hamano3dac5042007-12-15 08:40:54305</style>
306<title>parse-options API</title>
307</head>
308<body>
309<div id="header">
310<h1>parse-options API</h1>
311</div>
312<div id="preamble">
313<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10314<div class="para"><p>The parse-options API is used to parse and massage options in git
315and to provide a usage help with consistent look.</p></div>
Junio C Hamano3dac5042007-12-15 08:40:54316</div>
317</div>
Junio C Hamano4604fe52008-09-19 06:37:10318<h2 id="_basics">Basics</h2>
Junio C Hamano4224f992008-06-23 07:14:08319<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10320<div class="para"><p>The argument vector <tt>argv[]</tt> may usually contain mandatory or optional
Junio C Hamano4224f992008-06-23 07:14:08321<em>non-option arguments</em>, e.g. a filename or a branch, and <em>options</em>.
322Options are optional arguments that start with a dash and
Junio C Hamano4604fe52008-09-19 06:37:10323that allow to change the behavior of a command.</p></div>
324<div class="ilist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08325<li>
326<p>
327There are basically three types of options:
328 <em>boolean</em> options,
329 options with (mandatory) <em>arguments</em> and
330 options with <em>optional arguments</em>
331 (i.e. a boolean option that can be adjusted).
332</p>
333</li>
334<li>
335<p>
336There are basically two forms of options:
337 <em>Short options</em> consist of one dash (<tt>-</tt>) and one alphanumeric
338 character.
339 <em>Long options</em> begin with two dashes (<tt>--</tt>) and some
340 alphanumeric characters.
341</p>
342</li>
343<li>
344<p>
345Options are case-sensitive.
346 Please define <em>lower-case long options</em> only.
347</p>
348</li>
Junio C Hamano4604fe52008-09-19 06:37:10349</ul></div>
350<div class="para"><p>The parse-options API allows:</p></div>
351<div class="ilist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08352<li>
353<p>
354<em>sticked</em> and <em>separate form</em> of options with arguments.
355 <tt>-oArg</tt> is sticked, <tt>-o Arg</tt> is separate form.
356 <tt>--option=Arg</tt> is sticked, <tt>--option Arg</tt> is separate form.
357</p>
358</li>
359<li>
360<p>
361Long options may be <em>abbreviated</em>, as long as the abbreviation
362 is unambiguous.
363</p>
364</li>
365<li>
366<p>
367Short options may be bundled, e.g. <tt>-a -b</tt> can be specified as <tt>-ab</tt>.
368</p>
369</li>
370<li>
371<p>
372Boolean long options can be <em>negated</em> (or <em>unset</em>) by prepending
373 <tt>no-</tt>, e.g. <tt>--no-abbrev</tt> instead of <tt>--abbrev</tt>.
374</p>
375</li>
376<li>
377<p>
378Options and non-option arguments can clearly be separated using the <tt>--</tt>
379 option, e.g. <tt>-a -b --option -- --this-is-a-file</tt> indicates that
380 <tt>--this-is-a-file</tt> must not be processed as an option.
381</p>
382</li>
Junio C Hamano4604fe52008-09-19 06:37:10383</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08384</div>
Junio C Hamano4604fe52008-09-19 06:37:10385<h2 id="_steps_to_parse_options">Steps to parse options</h2>
Junio C Hamano4224f992008-06-23 07:14:08386<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10387<div class="olist"><ol>
Junio C Hamano4224f992008-06-23 07:14:08388<li>
389<p>
390<tt>#include "parse-options.h"</tt>
391</p>
392</li>
393<li>
394<p>
395define a NULL-terminated
396 <tt>static const char * const builtin_foo_usage[]</tt> array
397 containing alternative usage strings
398</p>
399</li>
400<li>
401<p>
402define <tt>builtin_foo_options</tt> array as described below
403 in section <em>Data Structure</em>.
404</p>
405</li>
406<li>
407<p>
408in <tt>cmd_foo(int argc, const char **argv, const char *prefix)</tt>
409 call
410</p>
411<div class="literalblock">
412<div class="content">
Junio C Hamano3d141512009-06-01 01:22:40413<pre><tt>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);</tt></pre>
Junio C Hamano4224f992008-06-23 07:14:08414</div></div>
Junio C Hamano4604fe52008-09-19 06:37:10415<div class="para"><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:08416non-option arguments in <tt>argv[]</tt>.
Junio C Hamano4604fe52008-09-19 06:37:10417<tt>argc</tt> is updated appropriately because of the assignment.</p></div>
Junio C Hamano3d141512009-06-01 01:22:40418<div class="para"><p>You can also pass NULL instead of a usage array as the fifth parameter of
Junio C Hamano18f51402009-03-10 16:39:13419parse_options(), to avoid displaying a help screen with usage info and
420option list. This should only be done if necessary, e.g. to implement
421a limited parser for only a subset of the options that needs to be run
422before the full parser, which in turn shows the full help message.</p></div>
Junio C Hamano4604fe52008-09-19 06:37:10423<div class="para"><p>Flags are the bitwise-or of:</p></div>
424<div class="vlist"><dl>
Junio C Hamano4224f992008-06-23 07:14:08425<dt>
426<tt>PARSE_OPT_KEEP_DASHDASH</tt>
427</dt>
428<dd>
429<p>
430 Keep the <tt>--</tt> that usually separates options from
431 non-option arguments.
432</p>
433</dd>
434<dt>
435<tt>PARSE_OPT_STOP_AT_NON_OPTION</tt>
436</dt>
437<dd>
438<p>
439 Usually the whole argument vector is massaged and reordered.
440 Using this flag, processing is stopped at the first non-option
441 argument.
442</p>
443</dd>
Junio C Hamano18f51402009-03-10 16:39:13444<dt>
445<tt>PARSE_OPT_KEEP_ARGV0</tt>
446</dt>
447<dd>
448<p>
449 Keep the first argument, which contains the program name. It's
450 removed from argv[] by default.
451</p>
452</dd>
453<dt>
454<tt>PARSE_OPT_KEEP_UNKNOWN</tt>
455</dt>
456<dd>
457<p>
458 Keep unknown arguments instead of erroring out. This doesn't
459 work for all combinations of arguments as users might expect
460 it to do. E.g. if the first argument in <tt>&#8212;unknown &#8212;known</tt>
461 takes a value (which we can't know), the second one is
462 mistakenly interpreted as a known option. Similarly, if
463 <tt>PARSE_OPT_STOP_AT_NON_OPTION</tt> is set, the second argument in
464 <tt>&#8212;unknown value</tt> will be mistakenly interpreted as a
465 non-option, not as a value belonging to the unknown option,
466 the parser early. That's why parse_options() errors out if
467 both options are set.
468</p>
469</dd>
470<dt>
471<tt>PARSE_OPT_NO_INTERNAL_HELP</tt>
472</dt>
473<dd>
474<p>
475 By default, parse_options() handles <tt>-h</tt>, <tt>&#8212;help</tt> and
476 <tt>&#8212;help-all</tt> internally, by showing a help screen. This option
477 turns it off and allows one to add custom handlers for these
478 options, or to just leave them unknown.
479</p>
480</dd>
Junio C Hamano4604fe52008-09-19 06:37:10481</dl></div>
Junio C Hamano4224f992008-06-23 07:14:08482</li>
Junio C Hamano4604fe52008-09-19 06:37:10483</ol></div>
Junio C Hamano4224f992008-06-23 07:14:08484</div>
Junio C Hamano4604fe52008-09-19 06:37:10485<h2 id="_data_structure">Data Structure</h2>
Junio C Hamano4224f992008-06-23 07:14:08486<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10487<div class="para"><p>The main data structure is an array of the <tt>option</tt> struct,
Junio C Hamano4224f992008-06-23 07:14:08488say <tt>static struct option builtin_add_options[]</tt>.
Junio C Hamano4604fe52008-09-19 06:37:10489There are some macros to easily define options:</p></div>
490<div class="vlist"><dl>
Junio C Hamano4224f992008-06-23 07:14:08491<dt>
492<tt>OPT__ABBREV(&amp;int_var)</tt>
493</dt>
494<dd>
495<p>
496 Add <tt>--abbrev[=&lt;n&gt;]</tt>.
497</p>
498</dd>
499<dt>
Junio C Hamano4aa0bcc2010-03-03 05:13:12500<tt>OPT__COLOR(&amp;int_var, description)</tt>
501</dt>
502<dd>
503<p>
504 Add <tt>--color[=&lt;when&gt;]</tt> and <tt>&#8212;no-color</tt>.
505</p>
506</dd>
507<dt>
Junio C Hamano4224f992008-06-23 07:14:08508<tt>OPT__DRY_RUN(&amp;int_var)</tt>
509</dt>
510<dd>
511<p>
512 Add <tt>-n, --dry-run</tt>.
513</p>
514</dd>
515<dt>
516<tt>OPT__QUIET(&amp;int_var)</tt>
517</dt>
518<dd>
519<p>
520 Add <tt>-q, --quiet</tt>.
521</p>
522</dd>
523<dt>
524<tt>OPT__VERBOSE(&amp;int_var)</tt>
525</dt>
526<dd>
527<p>
528 Add <tt>-v, --verbose</tt>.
529</p>
530</dd>
531<dt>
532<tt>OPT_GROUP(description)</tt>
533</dt>
534<dd>
535<p>
536 Start an option group. <tt>description</tt> is a short string that
537 describes the group or an empty string.
538 Start the description with an upper-case letter.
539</p>
540</dd>
541<dt>
542<tt>OPT_BOOLEAN(short, long, &amp;int_var, description)</tt>
543</dt>
544<dd>
545<p>
546 Introduce a boolean option.
547 <tt>int_var</tt> is incremented on each use.
548</p>
549</dd>
550<dt>
551<tt>OPT_BIT(short, long, &amp;int_var, description, mask)</tt>
552</dt>
553<dd>
554<p>
555 Introduce a boolean option.
556 If used, <tt>int_var</tt> is bitwise-ored with <tt>mask</tt>.
557</p>
558</dd>
559<dt>
Junio C Hamano7ae18252009-05-23 09:53:32560<tt>OPT_NEGBIT(short, long, &amp;int_var, description, mask)</tt>
561</dt>
562<dd>
563<p>
564 Introduce a boolean option.
565 If used, <tt>int_var</tt> is bitwise-anded with the inverted <tt>mask</tt>.
566</p>
567</dd>
568<dt>
Junio C Hamano4224f992008-06-23 07:14:08569<tt>OPT_SET_INT(short, long, &amp;int_var, description, integer)</tt>
570</dt>
571<dd>
572<p>
573 Introduce a boolean option.
574 If used, set <tt>int_var</tt> to <tt>integer</tt>.
575</p>
576</dd>
577<dt>
578<tt>OPT_SET_PTR(short, long, &amp;ptr_var, description, ptr)</tt>
579</dt>
580<dd>
581<p>
582 Introduce a boolean option.
583 If used, set <tt>ptr_var</tt> to <tt>ptr</tt>.
584</p>
585</dd>
586<dt>
587<tt>OPT_STRING(short, long, &amp;str_var, arg_str, description)</tt>
588</dt>
589<dd>
590<p>
591 Introduce an option with string argument.
592 The string argument is put into <tt>str_var</tt>.
593</p>
594</dd>
595<dt>
596<tt>OPT_INTEGER(short, long, &amp;int_var, description)</tt>
597</dt>
598<dd>
599<p>
600 Introduce an option with integer argument.
601 The integer is put into <tt>int_var</tt>.
602</p>
603</dd>
604<dt>
605<tt>OPT_DATE(short, long, &amp;int_var, description)</tt>
606</dt>
607<dd>
608<p>
609 Introduce an option with date argument, see <tt>approxidate()</tt>.
610 The timestamp is put into <tt>int_var</tt>.
611</p>
612</dd>
613<dt>
614<tt>OPT_CALLBACK(short, long, &amp;var, arg_str, description, func_ptr)</tt>
615</dt>
616<dd>
617<p>
618 Introduce an option with argument.
619 The argument will be fed into the function given by <tt>func_ptr</tt>
620 and the result will be put into <tt>var</tt>.
621 See <em>Option Callbacks</em> below for a more elaborate description.
622</p>
623</dd>
624<dt>
Junio C Hamano3d141512009-06-01 01:22:40625<tt>OPT_FILENAME(short, long, &amp;var, description)</tt>
626</dt>
627<dd>
628<p>
629 Introduce an option with a filename argument.
630 The filename will be prefixed by passing the filename along with
631 the prefix argument of <tt>parse_options()</tt> to <tt>prefix_filename()</tt>.
632</p>
633</dd>
634<dt>
Junio C Hamano4224f992008-06-23 07:14:08635<tt>OPT_ARGUMENT(long, description)</tt>
636</dt>
637<dd>
638<p>
639 Introduce a long-option argument that will be kept in <tt>argv[]</tt>.
640</p>
641</dd>
Junio C Hamano7ae18252009-05-23 09:53:32642<dt>
643<tt>OPT_NUMBER_CALLBACK(&amp;var, description, func_ptr)</tt>
644</dt>
645<dd>
646<p>
647 Recognize numerical options like -123 and feed the integer as
648 if it was an argument to the function given by <tt>func_ptr</tt>.
649 The result will be put into <tt>var</tt>. There can be only one such
650 option definition. It cannot be negated and it takes no
651 arguments. Short options that happen to be digits take
652 precedence over it.
653</p>
654</dd>
Junio C Hamano4aa0bcc2010-03-03 05:13:12655<dt>
656<tt>OPT_COLOR_FLAG(short, long, &amp;int_var, description)</tt>
657</dt>
658<dd>
659<p>
660 Introduce an option that takes an optional argument that can
661 have one of three values: "always", "never", or "auto". If the
662 argument is not given, it defaults to "always". The <tt>&#8212;no-</tt> form
663 works like <tt>&#8212;long=never</tt>; it cannot take an argument. If
664 "always", set <tt>int_var</tt> to 1; if "never", set <tt>int_var</tt> to 0; if
665 "auto", set <tt>int_var</tt> to 1 if stdout is a tty or a pager,
666 0 otherwise.
667</p>
668</dd>
Junio C Hamano4604fe52008-09-19 06:37:10669</dl></div>
670<div class="para"><p>The last element of the array must be <tt>OPT_END()</tt>.</p></div>
671<div class="para"><p>If not stated otherwise, interpret the arguments as follows:</p></div>
672<div class="ilist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08673<li>
674<p>
675<tt>short</tt> is a character for the short option
676 (e.g. <tt>'e'</tt> for <tt>-e</tt>, use <tt>0</tt> to omit),
677</p>
678</li>
679<li>
680<p>
681<tt>long</tt> is a string for the long option
682 (e.g. <tt>"example"</tt> for <tt>--example</tt>, use <tt>NULL</tt> to omit),
683</p>
684</li>
685<li>
686<p>
687<tt>int_var</tt> is an integer variable,
688</p>
689</li>
690<li>
691<p>
692<tt>str_var</tt> is a string variable (<tt>char *</tt>),
693</p>
694</li>
695<li>
696<p>
697<tt>arg_str</tt> is the string that is shown as argument
698 (e.g. <tt>"branch"</tt> will result in <tt>&lt;branch&gt;</tt>).
699 If set to <tt>NULL</tt>, three dots (<tt>&#8230;</tt>) will be displayed.
700</p>
701</li>
702<li>
703<p>
704<tt>description</tt> is a short string to describe the effect of the option.
705 It shall begin with a lower-case letter and a full stop (<tt>.</tt>) shall be
706 omitted at the end.
707</p>
708</li>
Junio C Hamano4604fe52008-09-19 06:37:10709</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08710</div>
Junio C Hamano4604fe52008-09-19 06:37:10711<h2 id="_option_callbacks">Option Callbacks</h2>
Junio C Hamano4224f992008-06-23 07:14:08712<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10713<div class="para"><p>The function must be defined in this form:</p></div>
Junio C Hamano4224f992008-06-23 07:14:08714<div class="literalblock">
715<div class="content">
716<pre><tt>int func(const struct option *opt, const char *arg, int unset)</tt></pre>
717</div></div>
Junio C Hamano4604fe52008-09-19 06:37:10718<div class="para"><p>The callback mechanism is as follows:</p></div>
719<div class="ilist"><ul>
Junio C Hamano4224f992008-06-23 07:14:08720<li>
721<p>
Junio C Hamanod4f35a92009-05-17 05:39:29722Inside <tt>func</tt>, the only interesting member of the structure
Junio C Hamano4224f992008-06-23 07:14:08723 given by <tt>opt</tt> is the void pointer <tt>opt-&gt;value</tt>.
724 <tt>*opt-&gt;value</tt> will be the value that is saved into <tt>var</tt>, if you
725 use <tt>OPT_CALLBACK()</tt>.
726 For example, do <tt>*(unsigned long *)opt-&gt;value = 42;</tt> to get 42
727 into an <tt>unsigned long</tt> variable.
728</p>
729</li>
730<li>
731<p>
732Return value <tt>0</tt> indicates success and non-zero return
733 value will invoke <tt>usage_with_options()</tt> and, thus, die.
734</p>
735</li>
736<li>
737<p>
738If the user negates the option, <tt>arg</tt> is <tt>NULL</tt> and <tt>unset</tt> is 1.
739</p>
740</li>
Junio C Hamano4604fe52008-09-19 06:37:10741</ul></div>
Junio C Hamano4224f992008-06-23 07:14:08742</div>
Junio C Hamano4604fe52008-09-19 06:37:10743<h2 id="_sophisticated_option_parsing">Sophisticated option parsing</h2>
Junio C Hamano4224f992008-06-23 07:14:08744<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10745<div class="para"><p>If you need, for example, option callbacks with optional arguments
Junio C Hamano4224f992008-06-23 07:14:08746or without arguments at all, or if you need other special cases,
747that are not handled by the macros above, you need to specify the
Junio C Hamano4604fe52008-09-19 06:37:10748members of the <tt>option</tt> structure manually.</p></div>
749<div class="para"><p>This is not covered in this document, but well documented
750in <tt>parse-options.h</tt> itself.</p></div>
Junio C Hamano4224f992008-06-23 07:14:08751</div>
Junio C Hamano4604fe52008-09-19 06:37:10752<h2 id="_examples">Examples</h2>
Junio C Hamano4224f992008-06-23 07:14:08753<div class="sectionbody">
Junio C Hamano4604fe52008-09-19 06:37:10754<div class="para"><p>See <tt>test-parse-options.c</tt> and
Junio C Hamano4224f992008-06-23 07:14:08755<tt>builtin-add.c</tt>,
756<tt>builtin-clone.c</tt>,
757<tt>builtin-commit.c</tt>,
758<tt>builtin-fetch.c</tt>,
759<tt>builtin-fsck.c</tt>,
760<tt>builtin-rm.c</tt>
Junio C Hamano4604fe52008-09-19 06:37:10761for real-world examples.</p></div>
Junio C Hamano4224f992008-06-23 07:14:08762</div>
Junio C Hamano3dac5042007-12-15 08:40:54763<div id="footer">
764<div id="footer-text">
Junio C Hamano4aa0bcc2010-03-03 05:13:12765Last updated 2010-03-03 05:12:43 UTC
Junio C Hamano3dac5042007-12-15 08:40:54766</div>
767</div>
768</body>
769</html>