1 <!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">
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
6 <meta name=
"generator" content=
"AsciiDoc 8.5.2" />
7 <title>parse-options API
</title>
8 <style type=
"text/css">
10 p
, li
, dt
, dd
, div
, pre
, h1
, h2
, h3
, h4
, h5
, h6
{
12 border: 1px solid red;
17 margin: 1em 5% 1em 5%;
22 text-decoration: underline
;
42 h1
, h2
, h3
, h4
, h5
, h6
{
44 font-family: sans-serif
;
51 border-bottom: 2px solid silver
;
69 border: 1px solid silver
;
88 font-family: sans-serif
;
94 span#revnumber
, span#revdate
, span#revremark
{
95 font-family: sans-serif
;
99 font-family: sans-serif
;
101 border-top: 2px solid silver
;
107 padding-bottom: 0.5em;
111 padding-bottom: 0.5em;
116 margin-bottom: 1.5em;
118 div
.tableblock
, div
.imageblock
, div
.exampleblock
, div
.verseblock
,
119 div
.quoteblock
, div
.literalblock
, div
.listingblock
, div
.sidebarblock
,
120 div
.admonitionblock
{
122 margin-bottom: 1.5em;
124 div
.admonitionblock
{
126 margin-bottom: 2.0em;
131 div
.content
{ /* Block element content. */
135 /* Block element titles. */
136 div
.title
, caption
.title
{
138 font-family: sans-serif
;
142 margin-bottom: 0.5em;
148 td div
.title:first-child
{
151 div
.content div
.title:first-child
{
154 div
.content
+ div
.title
{
158 div
.sidebarblock
> div
.content
{
160 border: 1px solid silver
;
164 div
.listingblock
> div
.content
{
165 border: 1px solid silver
;
170 div
.quoteblock
, div
.verseblock
{
174 border-left: 5px solid
#dddddd;
178 div
.quoteblock
> div
.attribution
{
183 div
.verseblock
> div
.content
{
186 div
.verseblock
> div
.attribution
{
190 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
191 div
.verseblock
+ div
.attribution
{
195 div
.admonitionblock
.icon
{
199 text-decoration: underline
;
201 padding-right: 0.5em;
203 div
.admonitionblock td
.content
{
205 border-left: 3px solid
#dddddd;
208 div
.exampleblock
> div
.content
{
209 border-left: 3px solid
#dddddd;
213 div
.imageblock div
.content
{ padding-left: 0; }
214 span
.image img
{ border-style: none
; }
215 a
.image:visited
{ color: white
; }
219 margin-bottom: 0.8em;
232 list-style-position: outside
;
235 list-style-type: decimal
;
238 list-style-type: lower-alpha
;
241 list-style-type: upper-alpha
;
244 list-style-type: lower-roman
;
247 list-style-type: upper-roman
;
250 div
.compact ul
, div
.compact ol
,
251 div
.compact p
, div
.compact p
,
252 div
.compact div
, div
.compact div
{
254 margin-bottom: 0.1em;
257 div
.tableblock
> table
{
258 border: 3px solid
#527bbd;
260 thead
, p
.table
.header
{
261 font-family: sans-serif
;
273 /* Because the table frame attribute is overriden by CSS in most browsers. */
274 div
.tableblock
> table
[frame
="void"] {
277 div
.tableblock
> table
[frame
="hsides"] {
278 border-left-style: none
;
279 border-right-style: none
;
281 div
.tableblock
> table
[frame
="vsides"] {
282 border-top-style: none
;
283 border-bottom-style: none
;
289 margin-bottom: 0.8em;
292 padding-bottom: 15px;
294 dt
.hdlist1
.strong
, td
.hdlist1
.strong
{
300 padding-right: 0.8em;
306 div
.hdlist
.compact tr
{
315 .footnote, .footnoteref {
319 span
.footnote
, span
.footnoteref
{
320 vertical-align: super
;
324 margin: 20px 0 20px 0;
328 #footnotes div
.footnote
{
334 border-top: 1px solid silver
;
344 div#footer-badges
{ display: none
; }
348 margin-bottom: 2.5em;
353 font-family: sans-serif
;
357 margin-bottom: 0.1em;
360 div
.toclevel1
, div
.toclevel2
, div
.toclevel3
, div
.toclevel4
{
376 /* Workarounds for IE6's broken and incomplete CSS2. */
378 div
.sidebar-content
{
380 border: 1px solid silver
;
383 div
.sidebar-title
, div
.image-title
{
385 font-family: sans-serif
;
388 margin-bottom: 0.5em;
391 div
.listingblock div
.content
{
392 border: 1px solid silver
;
397 div
.quoteblock-attribution
{
402 div
.verseblock-content
{
405 div
.verseblock-attribution
{
410 div
.exampleblock-content
{
411 border-left: 3px solid
#dddddd;
415 /* IE6 sets dynamically generated links as visited. */
416 div#toc
a:visited
{ color: blue
; }
418 <script type=
"text/javascript">
420 window
.onload = function(){asciidoc
.footnotes();}
421 var asciidoc
= { // Namespace.
423 /////////////////////////////////////////////////////////////////////
424 // Table Of Contents generator
425 /////////////////////////////////////////////////////////////////////
427 /* Author: Mihai Bazon, September 2002
428 * http://students.infoiasi.ro/~mishoo
430 * Table Of Content generator
433 * Feel free to use this script under the terms of the GNU General Public
434 * License, as long as you do not remove or alter this notice.
437 /* modified by Troy D. Hanson, September 2006. License: GPL */
438 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
441 toc: function (toclevels
) {
443 function getText(el
) {
445 for (var i
= el
.firstChild
; i
!= null; i
= i
.nextSibling
) {
446 if (i
.nodeType
== 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
448 else if (i
.firstChild
!= null)
454 function TocEntry(el
, text
, toclevel
) {
457 this.toclevel
= toclevel
;
460 function tocEntries(el
, toclevels
) {
461 var result
= new Array
;
462 var re
= new RegExp('[hH]([2-'+(toclevels
+1)+'])');
463 // Function that scans the DOM tree for header elements (the DOM2
464 // nodeIterator API would be a better technique but not supported by all
466 var iterate = function (el
) {
467 for (var i
= el
.firstChild
; i
!= null; i
= i
.nextSibling
) {
468 if (i
.nodeType
== 1 /* Node.ELEMENT_NODE */) {
469 var mo
= re
.exec(i
.tagName
);
470 if (mo
&& (i
.getAttribute("class") || i
.getAttribute("className")) != "float") {
471 result
[result
.length
] = new TocEntry(i
, getText(i
), mo
[1]-1);
481 var toc
= document
.getElementById("toc");
482 var entries
= tocEntries(document
.getElementById("content"), toclevels
);
483 for (var i
= 0; i
< entries
.length
; ++i
) {
484 var entry
= entries
[i
];
485 if (entry
.element
.id
== "")
486 entry
.element
.id
= "_toc_" + i
;
487 var a
= document
.createElement("a");
488 a
.href
= "#" + entry
.element
.id
;
489 a
.appendChild(document
.createTextNode(entry
.text
));
490 var div
= document
.createElement("div");
492 div
.className
= "toclevel" + entry
.toclevel
;
493 toc
.appendChild(div
);
495 if (entries
.length
== 0)
496 toc
.parentNode
.removeChild(toc
);
500 /////////////////////////////////////////////////////////////////////
501 // Footnotes generator
502 /////////////////////////////////////////////////////////////////////
504 /* Based on footnote generation code from:
505 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
508 footnotes: function () {
509 var cont
= document
.getElementById("content");
510 var noteholder
= document
.getElementById("footnotes");
511 var spans
= cont
.getElementsByTagName("span");
514 for (i
=0; i
<spans
.length
; i
++) {
515 if (spans
[i
].className
== "footnote") {
517 // Use [\s\S] in place of . so multi-line matches work.
518 // Because JavaScript has no s (dotall) regex flag.
519 note
= spans
[i
].innerHTML
.match(/\s*\[([\s\S]*)]\s*/)[1];
520 noteholder
.innerHTML
+=
521 "<div class='footnote' id='_footnote_" + n
+ "'>" +
522 "<a href='#_footnoteref_" + n
+ "' title='Return to text'>" +
523 n
+ "</a>. " + note
+ "</div>";
525 "[<a id='_footnoteref_" + n
+ "' href='#_footnote_" + n
+
526 "' title='View footnote' class='footnote'>" + n
+ "</a>]";
527 var id
=spans
[i
].getAttribute("id");
528 if (id
!= null) refs
["#"+id
] = n
;
532 noteholder
.parentNode
.removeChild(noteholder
);
534 // Process footnoterefs.
535 for (i
=0; i
<spans
.length
; i
++) {
536 if (spans
[i
].className
== "footnoteref") {
537 var href
= spans
[i
].getElementsByTagName("a")[0].getAttribute("href");
538 href
= href
.match(/#.*/)[0]; // Because IE return full URL.
541 "[<a href='#_footnote_" + n
+
542 "' title='View footnote' class='footnote'>" + n
+ "</a>]";
554 <h1>parse-options API
</h1>
558 <div class=
"sectionbody">
559 <div class=
"paragraph"><p>The parse-options API is used to parse and massage options in git
560 and to provide a usage help with consistent look.
</p></div>
563 <h2 id=
"_basics">Basics
</h2>
564 <div class=
"sectionbody">
565 <div class=
"paragraph"><p>The argument vector
<tt>argv[]
</tt> may usually contain mandatory or optional
566 <em>non-option arguments
</em>, e.g. a filename or a branch, and
<em>options
</em>.
567 Options are optional arguments that start with a dash and
568 that allow to change the behavior of a command.
</p></div>
569 <div class=
"ulist"><ul>
572 There are basically three types of options:
573 <em>boolean
</em> options,
574 options with (mandatory)
<em>arguments
</em> and
575 options with
<em>optional arguments
</em>
576 (i.e. a boolean option that can be adjusted).
581 There are basically two forms of options:
582 <em>Short options
</em> consist of one dash (
<tt>-
</tt>) and one alphanumeric
584 <em>Long options
</em> begin with two dashes (
<tt>--
</tt>) and some
585 alphanumeric characters.
590 Options are case-sensitive.
591 Please define
<em>lower-case long options
</em> only.
595 <div class=
"paragraph"><p>The parse-options API allows:
</p></div>
596 <div class=
"ulist"><ul>
599 <em>sticked
</em> and
<em>separate form
</em> of options with arguments.
600 <tt>-oArg
</tt> is sticked,
<tt>-o Arg
</tt> is separate form.
601 <tt>--option=Arg
</tt> is sticked,
<tt>--option Arg
</tt> is separate form.
606 Long options may be
<em>abbreviated
</em>, as long as the abbreviation
612 Short options may be bundled, e.g.
<tt>-a -b
</tt> can be specified as
<tt>-ab
</tt>.
617 Boolean long options can be
<em>negated
</em> (or
<em>unset
</em>) by prepending
618 <tt>no-
</tt>, e.g.
<tt>--no-abbrev
</tt> instead of
<tt>--abbrev
</tt>.
623 Options and non-option arguments can clearly be separated using the
<tt>--
</tt>
624 option, e.g.
<tt>-a -b --option -- --this-is-a-file
</tt> indicates that
625 <tt>--this-is-a-file
</tt> must not be processed as an option.
630 <h2 id=
"_steps_to_parse_options">Steps to parse options
</h2>
631 <div class=
"sectionbody">
632 <div class=
"olist arabic"><ol class=
"arabic">
635 <tt>#include
"parse-options.h"</tt>
640 define a NULL-terminated
641 <tt>static const char * const builtin_foo_usage[]
</tt> array
642 containing alternative usage strings
647 define
<tt>builtin_foo_options
</tt> array as described below
648 in section
<em>Data Structure
</em>.
653 in
<tt>cmd_foo(int argc, const char **argv, const char *prefix)
</tt>
656 <div class=
"literalblock">
657 <div class=
"content">
658 <pre><tt>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);
</tt></pre>
660 <div class=
"paragraph"><p><tt>parse_options()
</tt> will filter out the processed options of
<tt>argv[]
</tt> and leave the
661 non-option arguments in
<tt>argv[]
</tt>.
662 <tt>argc
</tt> is updated appropriately because of the assignment.
</p></div>
663 <div class=
"paragraph"><p>You can also pass NULL instead of a usage array as the fifth parameter of
664 parse_options(), to avoid displaying a help screen with usage info and
665 option list. This should only be done if necessary, e.g. to implement
666 a limited parser for only a subset of the options that needs to be run
667 before the full parser, which in turn shows the full help message.
</p></div>
668 <div class=
"paragraph"><p>Flags are the bitwise-or of:
</p></div>
669 <div class=
"dlist"><dl>
671 <tt>PARSE_OPT_KEEP_DASHDASH
</tt>
675 Keep the
<tt>--
</tt> that usually separates options from
676 non-option arguments.
680 <tt>PARSE_OPT_STOP_AT_NON_OPTION
</tt>
684 Usually the whole argument vector is massaged and reordered.
685 Using this flag, processing is stopped at the first non-option
690 <tt>PARSE_OPT_KEEP_ARGV0
</tt>
694 Keep the first argument, which contains the program name. It
’s
695 removed from argv[] by default.
699 <tt>PARSE_OPT_KEEP_UNKNOWN
</tt>
703 Keep unknown arguments instead of erroring out. This doesn
’t
704 work for all combinations of arguments as users might expect
705 it to do. E.g. if the first argument in
<tt>--unknown --known
</tt>
706 takes a value (which we can
’t know), the second one is
707 mistakenly interpreted as a known option. Similarly, if
708 <tt>PARSE_OPT_STOP_AT_NON_OPTION
</tt> is set, the second argument in
709 <tt>--unknown value
</tt> will be mistakenly interpreted as a
710 non-option, not as a value belonging to the unknown option,
711 the parser early. That
’s why parse_options() errors out if
712 both options are set.
716 <tt>PARSE_OPT_NO_INTERNAL_HELP
</tt>
720 By default, parse_options() handles
<tt>-h
</tt>,
<tt>--help
</tt> and
721 <tt>--help-all
</tt> internally, by showing a help screen. This option
722 turns it off and allows one to add custom handlers for these
723 options, or to just leave them unknown.
730 <h2 id=
"_data_structure">Data Structure
</h2>
731 <div class=
"sectionbody">
732 <div class=
"paragraph"><p>The main data structure is an array of the
<tt>option
</tt> struct,
733 say
<tt>static struct option builtin_add_options[]
</tt>.
734 There are some macros to easily define options:
</p></div>
735 <div class=
"dlist"><dl>
737 <tt>OPT__ABBREV(
&int_var)
</tt>
741 Add
<tt>--abbrev[=
<n
>]
</tt>.
745 <tt>OPT__COLOR(
&int_var, description)
</tt>
749 Add
<tt>--color[=
<when
>]
</tt> and
<tt>--no-color
</tt>.
753 <tt>OPT__DRY_RUN(
&int_var, description)
</tt>
757 Add
<tt>-n, --dry-run
</tt>.
761 <tt>OPT__FORCE(
&int_var, description)
</tt>
765 Add
<tt>-f, --force
</tt>.
769 <tt>OPT__QUIET(
&int_var, description)
</tt>
773 Add
<tt>-q, --quiet
</tt>.
777 <tt>OPT__VERBOSE(
&int_var, description)
</tt>
781 Add
<tt>-v, --verbose
</tt>.
785 <tt>OPT_GROUP(description)
</tt>
789 Start an option group.
<tt>description
</tt> is a short string that
790 describes the group or an empty string.
791 Start the description with an upper-case letter.
795 <tt>OPT_BOOLEAN(short, long,
&int_var, description)
</tt>
799 Introduce a boolean option.
800 <tt>int_var
</tt> is incremented on each use.
804 <tt>OPT_BIT(short, long,
&int_var, description, mask)
</tt>
808 Introduce a boolean option.
809 If used,
<tt>int_var
</tt> is bitwise-ored with
<tt>mask
</tt>.
813 <tt>OPT_NEGBIT(short, long,
&int_var, description, mask)
</tt>
817 Introduce a boolean option.
818 If used,
<tt>int_var
</tt> is bitwise-anded with the inverted
<tt>mask
</tt>.
822 <tt>OPT_SET_INT(short, long,
&int_var, description, integer)
</tt>
826 Introduce a boolean option.
827 If used, set
<tt>int_var
</tt> to
<tt>integer
</tt>.
831 <tt>OPT_SET_PTR(short, long,
&ptr_var, description, ptr)
</tt>
835 Introduce a boolean option.
836 If used, set
<tt>ptr_var
</tt> to
<tt>ptr
</tt>.
840 <tt>OPT_STRING(short, long,
&str_var, arg_str, description)
</tt>
844 Introduce an option with string argument.
845 The string argument is put into
<tt>str_var
</tt>.
849 <tt>OPT_INTEGER(short, long,
&int_var, description)
</tt>
853 Introduce an option with integer argument.
854 The integer is put into
<tt>int_var
</tt>.
858 <tt>OPT_DATE(short, long,
&int_var, description)
</tt>
862 Introduce an option with date argument, see
<tt>approxidate()
</tt>.
863 The timestamp is put into
<tt>int_var
</tt>.
867 <tt>OPT_CALLBACK(short, long,
&var, arg_str, description, func_ptr)
</tt>
871 Introduce an option with argument.
872 The argument will be fed into the function given by
<tt>func_ptr
</tt>
873 and the result will be put into
<tt>var
</tt>.
874 See
<em>Option Callbacks
</em> below for a more elaborate description.
878 <tt>OPT_FILENAME(short, long,
&var, description)
</tt>
882 Introduce an option with a filename argument.
883 The filename will be prefixed by passing the filename along with
884 the prefix argument of
<tt>parse_options()
</tt> to
<tt>prefix_filename()
</tt>.
888 <tt>OPT_ARGUMENT(long, description)
</tt>
892 Introduce a long-option argument that will be kept in
<tt>argv[]
</tt>.
896 <tt>OPT_NUMBER_CALLBACK(
&var, description, func_ptr)
</tt>
900 Recognize numerical options like -
123 and feed the integer as
901 if it was an argument to the function given by
<tt>func_ptr
</tt>.
902 The result will be put into
<tt>var
</tt>. There can be only one such
903 option definition. It cannot be negated and it takes no
904 arguments. Short options that happen to be digits take
909 <tt>OPT_COLOR_FLAG(short, long,
&int_var, description)
</tt>
913 Introduce an option that takes an optional argument that can
914 have one of three values:
"always",
"never", or
"auto". If the
915 argument is not given, it defaults to
"always". The
<tt>--no-
</tt> form
916 works like
<tt>--long=never
</tt>; it cannot take an argument. If
917 "always", set
<tt>int_var
</tt> to
1; if
"never", set
<tt>int_var
</tt> to
0; if
918 "auto", set
<tt>int_var
</tt> to
1 if stdout is a tty or a pager,
923 <div class=
"paragraph"><p>The last element of the array must be
<tt>OPT_END()
</tt>.
</p></div>
924 <div class=
"paragraph"><p>If not stated otherwise, interpret the arguments as follows:
</p></div>
925 <div class=
"ulist"><ul>
928 <tt>short
</tt> is a character for the short option
929 (e.g.
<tt>'e
'</tt> for
<tt>-e
</tt>, use
<tt>0</tt> to omit),
934 <tt>long
</tt> is a string for the long option
935 (e.g.
<tt>"example"</tt> for
<tt>--example
</tt>, use
<tt>NULL
</tt> to omit),
940 <tt>int_var
</tt> is an integer variable,
945 <tt>str_var
</tt> is a string variable (
<tt>char *
</tt>),
950 <tt>arg_str
</tt> is the string that is shown as argument
951 (e.g.
<tt>"branch"</tt> will result in
<tt><branch
></tt>).
952 If set to
<tt>NULL
</tt>, three dots (
<tt>…</tt>) will be displayed.
957 <tt>description
</tt> is a short string to describe the effect of the option.
958 It shall begin with a lower-case letter and a full stop (
<tt>.
</tt>) shall be
964 <h2 id=
"_option_callbacks">Option Callbacks
</h2>
965 <div class=
"sectionbody">
966 <div class=
"paragraph"><p>The function must be defined in this form:
</p></div>
967 <div class=
"literalblock">
968 <div class=
"content">
969 <pre><tt>int func(const struct option *opt, const char *arg, int unset)
</tt></pre>
971 <div class=
"paragraph"><p>The callback mechanism is as follows:
</p></div>
972 <div class=
"ulist"><ul>
975 Inside
<tt>func
</tt>, the only interesting member of the structure
976 given by
<tt>opt
</tt> is the void pointer
<tt>opt-
>value
</tt>.
977 <tt>*opt-
>value
</tt> will be the value that is saved into
<tt>var
</tt>, if you
978 use
<tt>OPT_CALLBACK()
</tt>.
979 For example, do
<tt>*(unsigned long *)opt-
>value =
42;
</tt> to get
42
980 into an
<tt>unsigned long
</tt> variable.
985 Return value
<tt>0</tt> indicates success and non-zero return
986 value will invoke
<tt>usage_with_options()
</tt> and, thus, die.
991 If the user negates the option,
<tt>arg
</tt> is
<tt>NULL
</tt> and
<tt>unset
</tt> is
1.
996 <h2 id=
"_sophisticated_option_parsing">Sophisticated option parsing
</h2>
997 <div class=
"sectionbody">
998 <div class=
"paragraph"><p>If you need, for example, option callbacks with optional arguments
999 or without arguments at all, or if you need other special cases,
1000 that are not handled by the macros above, you need to specify the
1001 members of the
<tt>option
</tt> structure manually.
</p></div>
1002 <div class=
"paragraph"><p>This is not covered in this document, but well documented
1003 in
<tt>parse-options.h
</tt> itself.
</p></div>
1005 <h2 id=
"_examples">Examples
</h2>
1006 <div class=
"sectionbody">
1007 <div class=
"paragraph"><p>See
<tt>test-parse-options.c
</tt> and
1008 <tt>builtin-add.c
</tt>,
1009 <tt>builtin-clone.c
</tt>,
1010 <tt>builtin-commit.c
</tt>,
1011 <tt>builtin-fetch.c
</tt>,
1012 <tt>builtin-fsck.c
</tt>,
1013 <tt>builtin-rm.c
</tt>
1014 for real-world examples.
</p></div>
1017 <div id=
"footnotes"><hr /></div>
1019 <div id=
"footer-text">
1020 Last updated
2011-
09-
21 23:
01:
14 PDT