Autogenerated HTML docs for v2.45.0-rc1-1-g9f32d
[git-htmldocs.git] / technical / api-parse-options.html
blob1b211081f449dcb2b4bfc5efccd2ed273adfcbd8
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
3 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
5 <head>
6 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
7 <meta name="generator" content="AsciiDoc 10.2.0" />
8 <title>parse-options API</title>
9 <style type="text/css">
10 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */
12 /* Default font. */
13 body {
14 font-family: Georgia,serif;
17 /* Title font. */
18 h1, h2, h3, h4, h5, h6,
19 div.title, caption.title,
20 thead, p.table.header,
21 #toctitle,
22 #author, #revnumber, #revdate, #revremark,
23 #footer {
24 font-family: Arial,Helvetica,sans-serif;
27 body {
28 margin: 1em 5% 1em 5%;
31 a {
32 color: blue;
33 text-decoration: underline;
35 a:visited {
36 color: fuchsia;
39 em {
40 font-style: italic;
41 color: navy;
44 strong {
45 font-weight: bold;
46 color: #083194;
49 h1, h2, h3, h4, h5, h6 {
50 color: #527bbd;
51 margin-top: 1.2em;
52 margin-bottom: 0.5em;
53 line-height: 1.3;
56 h1, h2, h3 {
57 border-bottom: 2px solid silver;
59 h2 {
60 padding-top: 0.5em;
62 h3 {
63 float: left;
65 h3 + * {
66 clear: left;
68 h5 {
69 font-size: 1.0em;
72 div.sectionbody {
73 margin-left: 0;
76 hr {
77 border: 1px solid silver;
80 p {
81 margin-top: 0.5em;
82 margin-bottom: 0.5em;
85 ul, ol, li > p {
86 margin-top: 0;
88 ul > li { color: #aaa; }
89 ul > li > * { color: black; }
91 .monospaced, code, pre {
92 font-family: "Courier New", Courier, monospace;
93 font-size: inherit;
94 color: navy;
95 padding: 0;
96 margin: 0;
98 pre {
99 white-space: pre-wrap;
102 #author {
103 color: #527bbd;
104 font-weight: bold;
105 font-size: 1.1em;
107 #email {
109 #revnumber, #revdate, #revremark {
112 #footer {
113 font-size: small;
114 border-top: 2px solid silver;
115 padding-top: 0.5em;
116 margin-top: 4.0em;
118 #footer-text {
119 float: left;
120 padding-bottom: 0.5em;
122 #footer-badges {
123 float: right;
124 padding-bottom: 0.5em;
127 #preamble {
128 margin-top: 1.5em;
129 margin-bottom: 1.5em;
131 div.imageblock, div.exampleblock, div.verseblock,
132 div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock,
133 div.admonitionblock {
134 margin-top: 1.0em;
135 margin-bottom: 1.5em;
137 div.admonitionblock {
138 margin-top: 2.0em;
139 margin-bottom: 2.0em;
140 margin-right: 10%;
141 color: #606060;
144 div.content { /* Block element content. */
145 padding: 0;
148 /* Block element titles. */
149 div.title, caption.title {
150 color: #527bbd;
151 font-weight: bold;
152 text-align: left;
153 margin-top: 1.0em;
154 margin-bottom: 0.5em;
156 div.title + * {
157 margin-top: 0;
160 td div.title:first-child {
161 margin-top: 0.0em;
163 div.content div.title:first-child {
164 margin-top: 0.0em;
166 div.content + div.title {
167 margin-top: 0.0em;
170 div.sidebarblock > div.content {
171 background: #ffffee;
172 border: 1px solid #dddddd;
173 border-left: 4px solid #f0f0f0;
174 padding: 0.5em;
177 div.listingblock > div.content {
178 border: 1px solid #dddddd;
179 border-left: 5px solid #f0f0f0;
180 background: #f8f8f8;
181 padding: 0.5em;
184 div.quoteblock, div.verseblock {
185 padding-left: 1.0em;
186 margin-left: 1.0em;
187 margin-right: 10%;
188 border-left: 5px solid #f0f0f0;
189 color: #888;
192 div.quoteblock > div.attribution {
193 padding-top: 0.5em;
194 text-align: right;
197 div.verseblock > pre.content {
198 font-family: inherit;
199 font-size: inherit;
201 div.verseblock > div.attribution {
202 padding-top: 0.75em;
203 text-align: left;
205 /* DEPRECATED: Pre version 8.2.7 verse style literal block. */
206 div.verseblock + div.attribution {
207 text-align: left;
210 div.admonitionblock .icon {
211 vertical-align: top;
212 font-size: 1.1em;
213 font-weight: bold;
214 text-decoration: underline;
215 color: #527bbd;
216 padding-right: 0.5em;
218 div.admonitionblock td.content {
219 padding-left: 0.5em;
220 border-left: 3px solid #dddddd;
223 div.exampleblock > div.content {
224 border-left: 3px solid #dddddd;
225 padding-left: 0.5em;
228 div.imageblock div.content { padding-left: 0; }
229 span.image img { border-style: none; vertical-align: text-bottom; }
230 a.image:visited { color: white; }
232 dl {
233 margin-top: 0.8em;
234 margin-bottom: 0.8em;
236 dt {
237 margin-top: 0.5em;
238 margin-bottom: 0;
239 font-style: normal;
240 color: navy;
242 dd > *:first-child {
243 margin-top: 0.1em;
246 ul, ol {
247 list-style-position: outside;
249 ol.arabic {
250 list-style-type: decimal;
252 ol.loweralpha {
253 list-style-type: lower-alpha;
255 ol.upperalpha {
256 list-style-type: upper-alpha;
258 ol.lowerroman {
259 list-style-type: lower-roman;
261 ol.upperroman {
262 list-style-type: upper-roman;
265 div.compact ul, div.compact ol,
266 div.compact p, div.compact p,
267 div.compact div, div.compact div {
268 margin-top: 0.1em;
269 margin-bottom: 0.1em;
272 tfoot {
273 font-weight: bold;
275 td > div.verse {
276 white-space: pre;
279 div.hdlist {
280 margin-top: 0.8em;
281 margin-bottom: 0.8em;
283 div.hdlist tr {
284 padding-bottom: 15px;
286 dt.hdlist1.strong, td.hdlist1.strong {
287 font-weight: bold;
289 td.hdlist1 {
290 vertical-align: top;
291 font-style: normal;
292 padding-right: 0.8em;
293 color: navy;
295 td.hdlist2 {
296 vertical-align: top;
298 div.hdlist.compact tr {
299 margin: 0;
300 padding-bottom: 0;
303 .comment {
304 background: yellow;
307 .footnote, .footnoteref {
308 font-size: 0.8em;
311 span.footnote, span.footnoteref {
312 vertical-align: super;
315 #footnotes {
316 margin: 20px 0 20px 0;
317 padding: 7px 0 0 0;
320 #footnotes div.footnote {
321 margin: 0 0 5px 0;
324 #footnotes hr {
325 border: none;
326 border-top: 1px solid silver;
327 height: 1px;
328 text-align: left;
329 margin-left: 0;
330 width: 20%;
331 min-width: 100px;
334 div.colist td {
335 padding-right: 0.5em;
336 padding-bottom: 0.3em;
337 vertical-align: top;
339 div.colist td img {
340 margin-top: 0.3em;
343 @media print {
344 #footer-badges { display: none; }
347 #toc {
348 margin-bottom: 2.5em;
351 #toctitle {
352 color: #527bbd;
353 font-size: 1.1em;
354 font-weight: bold;
355 margin-top: 1.0em;
356 margin-bottom: 0.1em;
359 div.toclevel0, div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 {
360 margin-top: 0;
361 margin-bottom: 0;
363 div.toclevel2 {
364 margin-left: 2em;
365 font-size: 0.9em;
367 div.toclevel3 {
368 margin-left: 4em;
369 font-size: 0.9em;
371 div.toclevel4 {
372 margin-left: 6em;
373 font-size: 0.9em;
376 span.aqua { color: aqua; }
377 span.black { color: black; }
378 span.blue { color: blue; }
379 span.fuchsia { color: fuchsia; }
380 span.gray { color: gray; }
381 span.green { color: green; }
382 span.lime { color: lime; }
383 span.maroon { color: maroon; }
384 span.navy { color: navy; }
385 span.olive { color: olive; }
386 span.purple { color: purple; }
387 span.red { color: red; }
388 span.silver { color: silver; }
389 span.teal { color: teal; }
390 span.white { color: white; }
391 span.yellow { color: yellow; }
393 span.aqua-background { background: aqua; }
394 span.black-background { background: black; }
395 span.blue-background { background: blue; }
396 span.fuchsia-background { background: fuchsia; }
397 span.gray-background { background: gray; }
398 span.green-background { background: green; }
399 span.lime-background { background: lime; }
400 span.maroon-background { background: maroon; }
401 span.navy-background { background: navy; }
402 span.olive-background { background: olive; }
403 span.purple-background { background: purple; }
404 span.red-background { background: red; }
405 span.silver-background { background: silver; }
406 span.teal-background { background: teal; }
407 span.white-background { background: white; }
408 span.yellow-background { background: yellow; }
410 span.big { font-size: 2em; }
411 span.small { font-size: 0.6em; }
413 span.underline { text-decoration: underline; }
414 span.overline { text-decoration: overline; }
415 span.line-through { text-decoration: line-through; }
417 div.unbreakable { page-break-inside: avoid; }
421 * xhtml11 specific
423 * */
425 div.tableblock {
426 margin-top: 1.0em;
427 margin-bottom: 1.5em;
429 div.tableblock > table {
430 border: 3px solid #527bbd;
432 thead, p.table.header {
433 font-weight: bold;
434 color: #527bbd;
436 p.table {
437 margin-top: 0;
439 /* Because the table frame attribute is overridden by CSS in most browsers. */
440 div.tableblock > table[frame="void"] {
441 border-style: none;
443 div.tableblock > table[frame="hsides"] {
444 border-left-style: none;
445 border-right-style: none;
447 div.tableblock > table[frame="vsides"] {
448 border-top-style: none;
449 border-bottom-style: none;
454 * html5 specific
456 * */
458 table.tableblock {
459 margin-top: 1.0em;
460 margin-bottom: 1.5em;
462 thead, p.tableblock.header {
463 font-weight: bold;
464 color: #527bbd;
466 p.tableblock {
467 margin-top: 0;
469 table.tableblock {
470 border-width: 3px;
471 border-spacing: 0px;
472 border-style: solid;
473 border-color: #527bbd;
474 border-collapse: collapse;
476 th.tableblock, td.tableblock {
477 border-width: 1px;
478 padding: 4px;
479 border-style: solid;
480 border-color: #527bbd;
483 table.tableblock.frame-topbot {
484 border-left-style: hidden;
485 border-right-style: hidden;
487 table.tableblock.frame-sides {
488 border-top-style: hidden;
489 border-bottom-style: hidden;
491 table.tableblock.frame-none {
492 border-style: hidden;
495 th.tableblock.halign-left, td.tableblock.halign-left {
496 text-align: left;
498 th.tableblock.halign-center, td.tableblock.halign-center {
499 text-align: center;
501 th.tableblock.halign-right, td.tableblock.halign-right {
502 text-align: right;
505 th.tableblock.valign-top, td.tableblock.valign-top {
506 vertical-align: top;
508 th.tableblock.valign-middle, td.tableblock.valign-middle {
509 vertical-align: middle;
511 th.tableblock.valign-bottom, td.tableblock.valign-bottom {
512 vertical-align: bottom;
517 * manpage specific
519 * */
521 body.manpage h1 {
522 padding-top: 0.5em;
523 padding-bottom: 0.5em;
524 border-top: 2px solid silver;
525 border-bottom: 2px solid silver;
527 body.manpage h2 {
528 border-style: none;
530 body.manpage div.sectionbody {
531 margin-left: 3em;
534 @media print {
535 body.manpage div#toc { display: none; }
539 </style>
540 <script type="text/javascript">
541 /*<![CDATA[*/
542 var asciidoc = { // Namespace.
544 /////////////////////////////////////////////////////////////////////
545 // Table Of Contents generator
546 /////////////////////////////////////////////////////////////////////
548 /* Author: Mihai Bazon, September 2002
549 * http://students.infoiasi.ro/~mishoo
551 * Table Of Content generator
552 * Version: 0.4
554 * Feel free to use this script under the terms of the GNU General Public
555 * License, as long as you do not remove or alter this notice.
558 /* modified by Troy D. Hanson, September 2006. License: GPL */
559 /* modified by Stuart Rackham, 2006, 2009. License: GPL */
561 // toclevels = 1..4.
562 toc: function (toclevels) {
564 function getText(el) {
565 var text = "";
566 for (var i = el.firstChild; i != null; i = i.nextSibling) {
567 if (i.nodeType == 3 /* Node.TEXT_NODE */) // IE doesn't speak constants.
568 text += i.data;
569 else if (i.firstChild != null)
570 text += getText(i);
572 return text;
575 function TocEntry(el, text, toclevel) {
576 this.element = el;
577 this.text = text;
578 this.toclevel = toclevel;
581 function tocEntries(el, toclevels) {
582 var result = new Array;
583 var re = new RegExp('[hH]([1-'+(toclevels+1)+'])');
584 // Function that scans the DOM tree for header elements (the DOM2
585 // nodeIterator API would be a better technique but not supported by all
586 // browsers).
587 var iterate = function (el) {
588 for (var i = el.firstChild; i != null; i = i.nextSibling) {
589 if (i.nodeType == 1 /* Node.ELEMENT_NODE */) {
590 var mo = re.exec(i.tagName);
591 if (mo && (i.getAttribute("class") || i.getAttribute("className")) != "float") {
592 result[result.length] = new TocEntry(i, getText(i), mo[1]-1);
594 iterate(i);
598 iterate(el);
599 return result;
602 var toc = document.getElementById("toc");
603 if (!toc) {
604 return;
607 // Delete existing TOC entries in case we're reloading the TOC.
608 var tocEntriesToRemove = [];
609 var i;
610 for (i = 0; i < toc.childNodes.length; i++) {
611 var entry = toc.childNodes[i];
612 if (entry.nodeName.toLowerCase() == 'div'
613 && entry.getAttribute("class")
614 && entry.getAttribute("class").match(/^toclevel/))
615 tocEntriesToRemove.push(entry);
617 for (i = 0; i < tocEntriesToRemove.length; i++) {
618 toc.removeChild(tocEntriesToRemove[i]);
621 // Rebuild TOC entries.
622 var entries = tocEntries(document.getElementById("content"), toclevels);
623 for (var i = 0; i < entries.length; ++i) {
624 var entry = entries[i];
625 if (entry.element.id == "")
626 entry.element.id = "_toc_" + i;
627 var a = document.createElement("a");
628 a.href = "#" + entry.element.id;
629 a.appendChild(document.createTextNode(entry.text));
630 var div = document.createElement("div");
631 div.appendChild(a);
632 div.className = "toclevel" + entry.toclevel;
633 toc.appendChild(div);
635 if (entries.length == 0)
636 toc.parentNode.removeChild(toc);
640 /////////////////////////////////////////////////////////////////////
641 // Footnotes generator
642 /////////////////////////////////////////////////////////////////////
644 /* Based on footnote generation code from:
645 * http://www.brandspankingnew.net/archive/2005/07/format_footnote.html
648 footnotes: function () {
649 // Delete existing footnote entries in case we're reloading the footnodes.
650 var i;
651 var noteholder = document.getElementById("footnotes");
652 if (!noteholder) {
653 return;
655 var entriesToRemove = [];
656 for (i = 0; i < noteholder.childNodes.length; i++) {
657 var entry = noteholder.childNodes[i];
658 if (entry.nodeName.toLowerCase() == 'div' && entry.getAttribute("class") == "footnote")
659 entriesToRemove.push(entry);
661 for (i = 0; i < entriesToRemove.length; i++) {
662 noteholder.removeChild(entriesToRemove[i]);
665 // Rebuild footnote entries.
666 var cont = document.getElementById("content");
667 var spans = cont.getElementsByTagName("span");
668 var refs = {};
669 var n = 0;
670 for (i=0; i<spans.length; i++) {
671 if (spans[i].className == "footnote") {
672 n++;
673 var note = spans[i].getAttribute("data-note");
674 if (!note) {
675 // Use [\s\S] in place of . so multi-line matches work.
676 // Because JavaScript has no s (dotall) regex flag.
677 note = spans[i].innerHTML.match(/\s*\[([\s\S]*)]\s*/)[1];
678 spans[i].innerHTML =
679 "[<a id='_footnoteref_" + n + "' href='#_footnote_" + n +
680 "' title='View footnote' class='footnote'>" + n + "</a>]";
681 spans[i].setAttribute("data-note", note);
683 noteholder.innerHTML +=
684 "<div class='footnote' id='_footnote_" + n + "'>" +
685 "<a href='#_footnoteref_" + n + "' title='Return to text'>" +
686 n + "</a>. " + note + "</div>";
687 var id =spans[i].getAttribute("id");
688 if (id != null) refs["#"+id] = n;
691 if (n == 0)
692 noteholder.parentNode.removeChild(noteholder);
693 else {
694 // Process footnoterefs.
695 for (i=0; i<spans.length; i++) {
696 if (spans[i].className == "footnoteref") {
697 var href = spans[i].getElementsByTagName("a")[0].getAttribute("href");
698 href = href.match(/#.*/)[0]; // Because IE return full URL.
699 n = refs[href];
700 spans[i].innerHTML =
701 "[<a href='#_footnote_" + n +
702 "' title='View footnote' class='footnote'>" + n + "</a>]";
708 install: function(toclevels) {
709 var timerId;
711 function reinstall() {
712 asciidoc.footnotes();
713 if (toclevels) {
714 asciidoc.toc(toclevels);
718 function reinstallAndRemoveTimer() {
719 clearInterval(timerId);
720 reinstall();
723 timerId = setInterval(reinstall, 500);
724 if (document.addEventListener)
725 document.addEventListener("DOMContentLoaded", reinstallAndRemoveTimer, false);
726 else
727 window.onload = reinstallAndRemoveTimer;
731 asciidoc.install();
732 /*]]>*/
733 </script>
734 </head>
735 <body class="article">
736 <div id="header">
737 <h1>parse-options API</h1>
738 <span id="revdate">2024-04-24</span>
739 </div>
740 <div id="content">
741 <div id="preamble">
742 <div class="sectionbody">
743 <div class="paragraph"><p>The parse-options API is used to parse and massage options in Git
744 and to provide a usage help with consistent look.</p></div>
745 </div>
746 </div>
747 <div class="sect1">
748 <h2 id="_basics">Basics</h2>
749 <div class="sectionbody">
750 <div class="paragraph"><p>The argument vector <code>argv[]</code> may usually contain mandatory or optional
751 <em>non-option arguments</em>, e.g. a filename or a branch, <em>options</em>, and
752 <em>subcommands</em>.
753 Options are optional arguments that start with a dash and
754 that allow to change the behavior of a command.</p></div>
755 <div class="ulist"><ul>
756 <li>
758 There are basically three types of options:
759 <em>boolean</em> options,
760 options with (mandatory) <em>arguments</em> and
761 options with <em>optional arguments</em>
762 (i.e. a boolean option that can be adjusted).
763 </p>
764 </li>
765 <li>
767 There are basically two forms of options:
768 <em>Short options</em> consist of one dash (<code>-</code>) and one alphanumeric
769 character.
770 <em>Long options</em> begin with two dashes (<code>--</code>) and some
771 alphanumeric characters.
772 </p>
773 </li>
774 <li>
776 Options are case-sensitive.
777 Please define <em>lower-case long options</em> only.
778 </p>
779 </li>
780 </ul></div>
781 <div class="paragraph"><p>The parse-options API allows:</p></div>
782 <div class="ulist"><ul>
783 <li>
785 <em>stuck</em> and <em>separate form</em> of options with arguments.
786 <code>-oArg</code> is stuck, <code>-o Arg</code> is separate form.
787 <code>--option=Arg</code> is stuck, <code>--option Arg</code> is separate form.
788 </p>
789 </li>
790 <li>
792 Long options may be <em>abbreviated</em>, as long as the abbreviation
793 is unambiguous.
794 </p>
795 </li>
796 <li>
798 Short options may be bundled, e.g. <code>-a -b</code> can be specified as <code>-ab</code>.
799 </p>
800 </li>
801 <li>
803 Boolean long options can be <em>negated</em> (or <em>unset</em>) by prepending
804 <code>no-</code>, e.g. <code>--no-abbrev</code> instead of <code>--abbrev</code>. Conversely,
805 options that begin with <code>no-</code> can be <em>negated</em> by removing it.
806 Other long options can be unset (e.g., set string to NULL, set
807 integer to 0) by prepending <code>no-</code>.
808 </p>
809 </li>
810 <li>
812 Options and non-option arguments can clearly be separated using the <code>--</code>
813 option, e.g. <code>-a -b --option -- --this-is-a-file</code> indicates that
814 <code>--this-is-a-file</code> must not be processed as an option.
815 </p>
816 </li>
817 </ul></div>
818 <div class="paragraph"><p>Subcommands are special in a couple of ways:</p></div>
819 <div class="ulist"><ul>
820 <li>
822 Subcommands only have long form, and they have no double dash prefix, no
823 negated form, and no description, and they don&#8217;t take any arguments, and
824 can&#8217;t be abbreviated.
825 </p>
826 </li>
827 <li>
829 There must be exactly one subcommand among the arguments, or zero if the
830 command has a default operation mode.
831 </p>
832 </li>
833 <li>
835 All arguments following the subcommand are considered to be arguments of
836 the subcommand, and, conversely, arguments meant for the subcommand may
837 not precede the subcommand.
838 </p>
839 </li>
840 </ul></div>
841 <div class="paragraph"><p>Therefore, if the options array contains at least one subcommand and
842 <code>parse_options()</code> encounters the first dashless argument, it will either:</p></div>
843 <div class="ulist"><ul>
844 <li>
846 stop and return, if that dashless argument is a known subcommand, setting
847 <code>value</code> to the function pointer associated with that subcommand, storing
848 the name of the subcommand in argv[0], and leaving the rest of the
849 arguments unprocessed, or
850 </p>
851 </li>
852 <li>
854 stop and return, if it was invoked with the <code>PARSE_OPT_SUBCOMMAND_OPTIONAL</code>
855 flag and that dashless argument doesn&#8217;t match any subcommands, leaving
856 <code>value</code> unchanged and the rest of the arguments unprocessed, or
857 </p>
858 </li>
859 <li>
861 show error and usage, and abort.
862 </p>
863 </li>
864 </ul></div>
865 </div>
866 </div>
867 <div class="sect1">
868 <h2 id="_steps_to_parse_options">Steps to parse options</h2>
869 <div class="sectionbody">
870 <div class="olist arabic"><ol class="arabic">
871 <li>
873 <code>#include "parse-options.h"</code>
874 </p>
875 </li>
876 <li>
878 define a NULL-terminated
879 <code>static const char * const builtin_foo_usage[]</code> array
880 containing alternative usage strings
881 </p>
882 </li>
883 <li>
885 define <code>builtin_foo_options</code> array as described below
886 in section <em>Data Structure</em>.
887 </p>
888 </li>
889 <li>
891 in <code>cmd_foo(int argc, const char **argv, const char *prefix)</code>
892 call
893 </p>
894 <div class="literalblock">
895 <div class="content">
896 <pre><code>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);</code></pre>
897 </div></div>
898 <div class="paragraph"><p><code>parse_options()</code> will filter out the processed options of <code>argv[]</code> and leave the
899 non-option arguments in <code>argv[]</code>.
900 <code>argc</code> is updated appropriately because of the assignment.</p></div>
901 <div class="paragraph"><p>You can also pass NULL instead of a usage array as the fifth parameter of
902 parse_options(), to avoid displaying a help screen with usage info and
903 option list. This should only be done if necessary, e.g. to implement
904 a limited parser for only a subset of the options that needs to be run
905 before the full parser, which in turn shows the full help message.</p></div>
906 <div class="paragraph"><p>Flags are the bitwise-or of:</p></div>
907 <div class="dlist"><dl>
908 <dt class="hdlist1">
909 <code>PARSE_OPT_KEEP_DASHDASH</code>
910 </dt>
911 <dd>
913 Keep the <code>--</code> that usually separates options from
914 non-option arguments.
915 </p>
916 </dd>
917 <dt class="hdlist1">
918 <code>PARSE_OPT_STOP_AT_NON_OPTION</code>
919 </dt>
920 <dd>
922 Usually the whole argument vector is massaged and reordered.
923 Using this flag, processing is stopped at the first non-option
924 argument.
925 </p>
926 </dd>
927 <dt class="hdlist1">
928 <code>PARSE_OPT_KEEP_ARGV0</code>
929 </dt>
930 <dd>
932 Keep the first argument, which contains the program name. It&#8217;s
933 removed from argv[] by default.
934 </p>
935 </dd>
936 <dt class="hdlist1">
937 <code>PARSE_OPT_KEEP_UNKNOWN_OPT</code>
938 </dt>
939 <dd>
941 Keep unknown options instead of erroring out. This doesn&#8217;t
942 work for all combinations of arguments as users might expect
943 it to do. E.g. if the first argument in <code>--unknown --known</code>
944 takes a value (which we can&#8217;t know), the second one is
945 mistakenly interpreted as a known option. Similarly, if
946 <code>PARSE_OPT_STOP_AT_NON_OPTION</code> is set, the second argument in
947 <code>--unknown value</code> will be mistakenly interpreted as a
948 non-option, not as a value belonging to the unknown option,
949 the parser early. That&#8217;s why parse_options() errors out if
950 both options are set.
951 Note that non-option arguments are always kept, even without
952 this flag.
953 </p>
954 </dd>
955 <dt class="hdlist1">
956 <code>PARSE_OPT_NO_INTERNAL_HELP</code>
957 </dt>
958 <dd>
960 By default, parse_options() handles <code>-h</code>, <code>--help</code> and
961 <code>--help-all</code> internally, by showing a help screen. This option
962 turns it off and allows one to add custom handlers for these
963 options, or to just leave them unknown.
964 </p>
965 </dd>
966 <dt class="hdlist1">
967 <code>PARSE_OPT_SUBCOMMAND_OPTIONAL</code>
968 </dt>
969 <dd>
971 Don&#8217;t error out when no subcommand is specified.
972 </p>
973 </dd>
974 </dl></div>
975 </li>
976 </ol></div>
977 <div class="paragraph"><p>Note that <code>PARSE_OPT_STOP_AT_NON_OPTION</code> is incompatible with subcommands;
978 while <code>PARSE_OPT_KEEP_DASHDASH</code> and <code>PARSE_OPT_KEEP_UNKNOWN_OPT</code> can only be
979 used with subcommands when combined with <code>PARSE_OPT_SUBCOMMAND_OPTIONAL</code>.</p></div>
980 </div>
981 </div>
982 <div class="sect1">
983 <h2 id="_data_structure">Data Structure</h2>
984 <div class="sectionbody">
985 <div class="paragraph"><p>The main data structure is an array of the <code>option</code> struct,
986 say <code>static struct option builtin_add_options[]</code>.
987 There are some macros to easily define options:</p></div>
988 <div class="dlist"><dl>
989 <dt class="hdlist1">
990 <code>OPT__ABBREV(&amp;int_var)</code>
991 </dt>
992 <dd>
994 Add <code>--abbrev[=&lt;n&gt;]</code>.
995 </p>
996 </dd>
997 <dt class="hdlist1">
998 <code>OPT__COLOR(&amp;int_var, description)</code>
999 </dt>
1000 <dd>
1002 Add <code>--color[=&lt;when&gt;]</code> and <code>--no-color</code>.
1003 </p>
1004 </dd>
1005 <dt class="hdlist1">
1006 <code>OPT__DRY_RUN(&amp;int_var, description)</code>
1007 </dt>
1008 <dd>
1010 Add <code>-n, --dry-run</code>.
1011 </p>
1012 </dd>
1013 <dt class="hdlist1">
1014 <code>OPT__FORCE(&amp;int_var, description)</code>
1015 </dt>
1016 <dd>
1018 Add <code>-f, --force</code>.
1019 </p>
1020 </dd>
1021 <dt class="hdlist1">
1022 <code>OPT__QUIET(&amp;int_var, description)</code>
1023 </dt>
1024 <dd>
1026 Add <code>-q, --quiet</code>.
1027 </p>
1028 </dd>
1029 <dt class="hdlist1">
1030 <code>OPT__VERBOSE(&amp;int_var, description)</code>
1031 </dt>
1032 <dd>
1034 Add <code>-v, --verbose</code>.
1035 </p>
1036 </dd>
1037 <dt class="hdlist1">
1038 <code>OPT_GROUP(description)</code>
1039 </dt>
1040 <dd>
1042 Start an option group. <code>description</code> is a short string that
1043 describes the group or an empty string.
1044 Start the description with an upper-case letter.
1045 </p>
1046 </dd>
1047 <dt class="hdlist1">
1048 <code>OPT_BOOL(short, long, &amp;int_var, description)</code>
1049 </dt>
1050 <dd>
1052 Introduce a boolean option. <code>int_var</code> is set to one with
1053 <code>--option</code> and set to zero with <code>--no-option</code>.
1054 </p>
1055 </dd>
1056 <dt class="hdlist1">
1057 <code>OPT_COUNTUP(short, long, &amp;int_var, description)</code>
1058 </dt>
1059 <dd>
1061 Introduce a count-up option.
1062 Each use of <code>--option</code> increments <code>int_var</code>, starting from zero
1063 (even if initially negative), and <code>--no-option</code> resets it to
1064 zero. To determine if <code>--option</code> or <code>--no-option</code> was encountered at
1065 all, initialize <code>int_var</code> to a negative value, and if it is still
1066 negative after parse_options(), then neither <code>--option</code> nor
1067 <code>--no-option</code> was seen.
1068 </p>
1069 </dd>
1070 <dt class="hdlist1">
1071 <code>OPT_BIT(short, long, &amp;int_var, description, mask)</code>
1072 </dt>
1073 <dd>
1075 Introduce a boolean option.
1076 If used, <code>int_var</code> is bitwise-ored with <code>mask</code>.
1077 </p>
1078 </dd>
1079 <dt class="hdlist1">
1080 <code>OPT_NEGBIT(short, long, &amp;int_var, description, mask)</code>
1081 </dt>
1082 <dd>
1084 Introduce a boolean option.
1085 If used, <code>int_var</code> is bitwise-anded with the inverted <code>mask</code>.
1086 </p>
1087 </dd>
1088 <dt class="hdlist1">
1089 <code>OPT_SET_INT(short, long, &amp;int_var, description, integer)</code>
1090 </dt>
1091 <dd>
1093 Introduce an integer option.
1094 <code>int_var</code> is set to <code>integer</code> with <code>--option</code>, and
1095 reset to zero with <code>--no-option</code>.
1096 </p>
1097 </dd>
1098 <dt class="hdlist1">
1099 <code>OPT_STRING(short, long, &amp;str_var, arg_str, description)</code>
1100 </dt>
1101 <dd>
1103 Introduce an option with string argument.
1104 The string argument is put into <code>str_var</code>.
1105 </p>
1106 </dd>
1107 <dt class="hdlist1">
1108 <code>OPT_STRING_LIST(short, long, &amp;struct string_list, arg_str, description)</code>
1109 </dt>
1110 <dd>
1112 Introduce an option with string argument.
1113 The string argument is stored as an element in <code>string_list</code>.
1114 Use of <code>--no-option</code> will clear the list of preceding values.
1115 </p>
1116 </dd>
1117 <dt class="hdlist1">
1118 <code>OPT_INTEGER(short, long, &amp;int_var, description)</code>
1119 </dt>
1120 <dd>
1122 Introduce an option with integer argument.
1123 The integer is put into <code>int_var</code>.
1124 </p>
1125 </dd>
1126 <dt class="hdlist1">
1127 <code>OPT_MAGNITUDE(short, long, &amp;unsigned_long_var, description)</code>
1128 </dt>
1129 <dd>
1131 Introduce an option with a size argument. The argument must be a
1132 non-negative integer and may include a suffix of <em>k</em>, <em>m</em> or <em>g</em> to
1133 scale the provided value by 1024, 1024<sup>2 or 1024</sup>3 respectively.
1134 The scaled value is put into <code>unsigned_long_var</code>.
1135 </p>
1136 </dd>
1137 <dt class="hdlist1">
1138 <code>OPT_EXPIRY_DATE(short, long, &amp;timestamp_t_var, description)</code>
1139 </dt>
1140 <dd>
1142 Introduce an option with expiry date argument, see <code>parse_expiry_date()</code>.
1143 The timestamp is put into <code>timestamp_t_var</code>.
1144 </p>
1145 </dd>
1146 <dt class="hdlist1">
1147 <code>OPT_CALLBACK(short, long, &amp;var, arg_str, description, func_ptr)</code>
1148 </dt>
1149 <dd>
1151 Introduce an option with argument.
1152 The argument will be fed into the function given by <code>func_ptr</code>
1153 and the result will be put into <code>var</code>.
1154 See <em>Option Callbacks</em> below for a more elaborate description.
1155 </p>
1156 </dd>
1157 <dt class="hdlist1">
1158 <code>OPT_FILENAME(short, long, &amp;var, description)</code>
1159 </dt>
1160 <dd>
1162 Introduce an option with a filename argument.
1163 The filename will be prefixed by passing the filename along with
1164 the prefix argument of <code>parse_options()</code> to <code>prefix_filename()</code>.
1165 </p>
1166 </dd>
1167 <dt class="hdlist1">
1168 <code>OPT_NUMBER_CALLBACK(&amp;var, description, func_ptr)</code>
1169 </dt>
1170 <dd>
1172 Recognize numerical options like -123 and feed the integer as
1173 if it was an argument to the function given by <code>func_ptr</code>.
1174 The result will be put into <code>var</code>. There can be only one such
1175 option definition. It cannot be negated and it takes no
1176 arguments. Short options that happen to be digits take
1177 precedence over it.
1178 </p>
1179 </dd>
1180 <dt class="hdlist1">
1181 <code>OPT_COLOR_FLAG(short, long, &amp;int_var, description)</code>
1182 </dt>
1183 <dd>
1185 Introduce an option that takes an optional argument that can
1186 have one of three values: "always", "never", or "auto". If the
1187 argument is not given, it defaults to "always". The <code>--no-</code> form
1188 works like <code>--long=never</code>; it cannot take an argument. If
1189 "always", set <code>int_var</code> to 1; if "never", set <code>int_var</code> to 0; if
1190 "auto", set <code>int_var</code> to 1 if stdout is a tty or a pager,
1191 0 otherwise.
1192 </p>
1193 </dd>
1194 <dt class="hdlist1">
1195 <code>OPT_NOOP_NOARG(short, long)</code>
1196 </dt>
1197 <dd>
1199 Introduce an option that has no effect and takes no arguments.
1200 Use it to hide deprecated options that are still to be recognized
1201 and ignored silently.
1202 </p>
1203 </dd>
1204 <dt class="hdlist1">
1205 <code>OPT_PASSTHRU(short, long, &amp;char_var, arg_str, description, flags)</code>
1206 </dt>
1207 <dd>
1209 Introduce an option that will be reconstructed into a char* string,
1210 which must be initialized to NULL. This is useful when you need to
1211 pass the command-line option to another command. Any previous value
1212 will be overwritten, so this should only be used for options where
1213 the last one specified on the command line wins.
1214 </p>
1215 </dd>
1216 <dt class="hdlist1">
1217 <code>OPT_PASSTHRU_ARGV(short, long, &amp;strvec_var, arg_str, description, flags)</code>
1218 </dt>
1219 <dd>
1221 Introduce an option where all instances of it on the command-line will
1222 be reconstructed into a strvec. This is useful when you need to
1223 pass the command-line option, which can be specified multiple times,
1224 to another command.
1225 </p>
1226 </dd>
1227 <dt class="hdlist1">
1228 <code>OPT_CMDMODE(short, long, &amp;int_var, description, enum_val)</code>
1229 </dt>
1230 <dd>
1232 Define an "operation mode" option, only one of which in the same
1233 group of "operating mode" options that share the same <code>int_var</code>
1234 can be given by the user. <code>int_var</code> is set to <code>enum_val</code> when the
1235 option is used, but an error is reported if other "operating mode"
1236 option has already set its value to the same <code>int_var</code>.
1237 In new commands consider using subcommands instead.
1238 </p>
1239 </dd>
1240 <dt class="hdlist1">
1241 <code>OPT_SUBCOMMAND(long, &amp;fn_ptr, subcommand_fn)</code>
1242 </dt>
1243 <dd>
1245 Define a subcommand. <code>subcommand_fn</code> is put into <code>fn_ptr</code> when
1246 this subcommand is used.
1247 </p>
1248 </dd>
1249 </dl></div>
1250 <div class="paragraph"><p>The last element of the array must be <code>OPT_END()</code>.</p></div>
1251 <div class="paragraph"><p>If not stated otherwise, interpret the arguments as follows:</p></div>
1252 <div class="ulist"><ul>
1253 <li>
1255 <code>short</code> is a character for the short option
1256 (e.g. <code>'e'</code> for <code>-e</code>, use <code>0</code> to omit),
1257 </p>
1258 </li>
1259 <li>
1261 <code>long</code> is a string for the long option
1262 (e.g. <code>"example"</code> for <code>--example</code>, use <code>NULL</code> to omit),
1263 </p>
1264 </li>
1265 <li>
1267 <code>int_var</code> is an integer variable,
1268 </p>
1269 </li>
1270 <li>
1272 <code>str_var</code> is a string variable (<code>char *</code>),
1273 </p>
1274 </li>
1275 <li>
1277 <code>arg_str</code> is the string that is shown as argument
1278 (e.g. <code>"branch"</code> will result in <code>&lt;branch&gt;</code>).
1279 If set to <code>NULL</code>, three dots (<code>...</code>) will be displayed.
1280 </p>
1281 </li>
1282 <li>
1284 <code>description</code> is a short string to describe the effect of the option.
1285 It shall begin with a lower-case letter and a full stop (<code>.</code>) shall be
1286 omitted at the end.
1287 </p>
1288 </li>
1289 </ul></div>
1290 </div>
1291 </div>
1292 <div class="sect1">
1293 <h2 id="_option_callbacks">Option Callbacks</h2>
1294 <div class="sectionbody">
1295 <div class="paragraph"><p>The function must be defined in this form:</p></div>
1296 <div class="literalblock">
1297 <div class="content">
1298 <pre><code>int func(const struct option *opt, const char *arg, int unset)</code></pre>
1299 </div></div>
1300 <div class="paragraph"><p>The callback mechanism is as follows:</p></div>
1301 <div class="ulist"><ul>
1302 <li>
1304 Inside <code>func</code>, the only interesting member of the structure
1305 given by <code>opt</code> is the void pointer <code>opt-&gt;value</code>.
1306 <code>*opt-&gt;value</code> will be the value that is saved into <code>var</code>, if you
1307 use <code>OPT_CALLBACK()</code>.
1308 For example, do <code>*(unsigned long *)opt-&gt;value = 42;</code> to get 42
1309 into an <code>unsigned long</code> variable.
1310 </p>
1311 </li>
1312 <li>
1314 Return value <code>0</code> indicates success and non-zero return
1315 value will invoke <code>usage_with_options()</code> and, thus, die.
1316 </p>
1317 </li>
1318 <li>
1320 If the user negates the option, <code>arg</code> is <code>NULL</code> and <code>unset</code> is 1.
1321 </p>
1322 </li>
1323 </ul></div>
1324 </div>
1325 </div>
1326 <div class="sect1">
1327 <h2 id="_sophisticated_option_parsing">Sophisticated option parsing</h2>
1328 <div class="sectionbody">
1329 <div class="paragraph"><p>If you need, for example, option callbacks with optional arguments
1330 or without arguments at all, or if you need other special cases,
1331 that are not handled by the macros above, you need to specify the
1332 members of the <code>option</code> structure manually.</p></div>
1333 <div class="paragraph"><p>This is not covered in this document, but well documented
1334 in <code>parse-options.h</code> itself.</p></div>
1335 </div>
1336 </div>
1337 <div class="sect1">
1338 <h2 id="_examples">Examples</h2>
1339 <div class="sectionbody">
1340 <div class="paragraph"><p>See <code>test-parse-options.c</code> and
1341 <code>builtin/add.c</code>,
1342 <code>builtin/clone.c</code>,
1343 <code>builtin/commit.c</code>,
1344 <code>builtin/fetch.c</code>,
1345 <code>builtin/fsck.c</code>,
1346 <code>builtin/rm.c</code>
1347 for real-world examples.</p></div>
1348 </div>
1349 </div>
1350 </div>
1351 <div id="footnotes"><hr /></div>
1352 <div id="footer">
1353 <div id="footer-text">
1354 Last updated
1355 2022-09-21 15:44:34 PDT
1356 </div>
1357 </div>
1358 </body>
1359 </html>