Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 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"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 6 | <meta name="generator" content="AsciiDoc 8.2.5" /> |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 7 | <style type="text/css"> |
| 8 | /* Debug borders */ |
| 9 | p, li, dt, dd, div, pre, h1, h2, h3, h4, h5, h6 { |
| 10 | /* |
| 11 | border: 1px solid red; |
| 12 | */ |
| 13 | } |
| 14 | |
| 15 | body { |
| 16 | margin: 1em 5% 1em 5%; |
| 17 | } |
| 18 | |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 19 | a { |
| 20 | color: blue; |
| 21 | text-decoration: underline; |
| 22 | } |
| 23 | a:visited { |
| 24 | color: fuchsia; |
| 25 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 26 | |
| 27 | em { |
| 28 | font-style: italic; |
| 29 | } |
| 30 | |
| 31 | strong { |
| 32 | font-weight: bold; |
| 33 | } |
| 34 | |
| 35 | tt { |
| 36 | color: navy; |
| 37 | } |
| 38 | |
| 39 | h1, 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 Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 47 | h1, h2, h3 { |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 48 | border-bottom: 2px solid silver; |
| 49 | } |
| 50 | h2 { |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 51 | padding-top: 0.5em; |
| 52 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 53 | h3 { |
| 54 | float: left; |
| 55 | } |
| 56 | h3 + * { |
| 57 | clear: left; |
| 58 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 59 | |
| 60 | div.sectionbody { |
| 61 | font-family: serif; |
| 62 | margin-left: 0; |
| 63 | } |
| 64 | |
| 65 | hr { |
| 66 | border: 1px solid silver; |
| 67 | } |
| 68 | |
| 69 | p { |
| 70 | margin-top: 0.5em; |
| 71 | margin-bottom: 0.5em; |
| 72 | } |
| 73 | |
| 74 | pre { |
| 75 | padding: 0; |
| 76 | margin: 0; |
| 77 | } |
| 78 | |
| 79 | span#author { |
| 80 | color: #527bbd; |
| 81 | font-family: sans-serif; |
| 82 | font-weight: bold; |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 83 | font-size: 1.1em; |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 84 | } |
| 85 | span#email { |
| 86 | } |
| 87 | span#revision { |
| 88 | font-family: sans-serif; |
| 89 | } |
| 90 | |
| 91 | div#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 | } |
| 98 | div#footer-text { |
| 99 | float: left; |
| 100 | padding-bottom: 0.5em; |
| 101 | } |
| 102 | div#footer-badges { |
| 103 | float: right; |
| 104 | padding-bottom: 0.5em; |
| 105 | } |
| 106 | |
| 107 | div#preamble, |
| 108 | div.tableblock, div.imageblock, div.exampleblock, div.verseblock, |
| 109 | div.quoteblock, div.literalblock, div.listingblock, div.sidebarblock, |
| 110 | div.admonitionblock { |
| 111 | margin-right: 10%; |
| 112 | margin-top: 1.5em; |
| 113 | margin-bottom: 1.5em; |
| 114 | } |
| 115 | div.admonitionblock { |
| 116 | margin-top: 2.5em; |
| 117 | margin-bottom: 2.5em; |
| 118 | } |
| 119 | |
| 120 | div.content { /* Block element content. */ |
| 121 | padding: 0; |
| 122 | } |
| 123 | |
| 124 | /* Block element titles. */ |
| 125 | div.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 | } |
| 132 | div.title + * { |
| 133 | margin-top: 0; |
| 134 | } |
| 135 | |
| 136 | td div.title:first-child { |
| 137 | margin-top: 0.0em; |
| 138 | } |
| 139 | div.content div.title:first-child { |
| 140 | margin-top: 0.0em; |
| 141 | } |
| 142 | div.content + div.title { |
| 143 | margin-top: 0.0em; |
| 144 | } |
| 145 | |
| 146 | div.sidebarblock > div.content { |
| 147 | background: #ffffee; |
| 148 | border: 1px solid silver; |
| 149 | padding: 0.5em; |
| 150 | } |
| 151 | |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 152 | div.listingblock { |
| 153 | margin-right: 0%; |
| 154 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 155 | div.listingblock > div.content { |
| 156 | border: 1px solid silver; |
| 157 | background: #f4f4f4; |
| 158 | padding: 0.5em; |
| 159 | } |
| 160 | |
| 161 | div.quoteblock > div.content { |
| 162 | padding-left: 2.0em; |
| 163 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 164 | |
| 165 | div.attribution { |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 166 | text-align: right; |
| 167 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 168 | div.verseblock + div.attribution { |
| 169 | text-align: left; |
| 170 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 171 | |
| 172 | div.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 | } |
| 180 | div.admonitionblock td.content { |
| 181 | padding-left: 0.5em; |
| 182 | border-left: 2px solid silver; |
| 183 | } |
| 184 | |
| 185 | div.exampleblock > div.content { |
| 186 | border-left: 2px solid silver; |
| 187 | padding: 0.5em; |
| 188 | } |
| 189 | |
| 190 | div.verseblock div.content { |
| 191 | white-space: pre; |
| 192 | } |
| 193 | |
| 194 | div.imageblock div.content { padding-left: 0; } |
| 195 | div.imageblock img { border: 1px solid silver; } |
| 196 | span.image img { border-style: none; } |
| 197 | |
| 198 | dl { |
| 199 | margin-top: 0.8em; |
| 200 | margin-bottom: 0.8em; |
| 201 | } |
| 202 | dt { |
| 203 | margin-top: 0.5em; |
| 204 | margin-bottom: 0; |
| 205 | font-style: italic; |
| 206 | } |
| 207 | dd > *:first-child { |
| 208 | margin-top: 0; |
| 209 | } |
| 210 | |
| 211 | ul, ol { |
| 212 | list-style-position: outside; |
| 213 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 214 | div.olist2 ol { |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 215 | list-style-type: lower-alpha; |
| 216 | } |
| 217 | |
| 218 | div.tableblock > table { |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 219 | border: 3px solid #527bbd; |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 220 | } |
| 221 | thead { |
| 222 | font-family: sans-serif; |
| 223 | font-weight: bold; |
| 224 | } |
| 225 | tfoot { |
| 226 | font-weight: bold; |
| 227 | } |
| 228 | |
| 229 | div.hlist { |
| 230 | margin-top: 0.8em; |
| 231 | margin-bottom: 0.8em; |
| 232 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 233 | div.hlist td { |
| 234 | padding-bottom: 5px; |
| 235 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 236 | td.hlist1 { |
| 237 | vertical-align: top; |
| 238 | font-style: italic; |
| 239 | padding-right: 0.8em; |
| 240 | } |
| 241 | td.hlist2 { |
| 242 | vertical-align: top; |
| 243 | } |
| 244 | |
| 245 | @media print { |
| 246 | div#footer-badges { display: none; } |
| 247 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 248 | |
| 249 | div#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 | |
| 258 | div.toclevel1, div.toclevel2, div.toclevel3, div.toclevel4 { |
| 259 | margin-top: 0; |
| 260 | margin-bottom: 0; |
| 261 | } |
| 262 | div.toclevel2 { |
| 263 | margin-left: 2em; |
| 264 | font-size: 0.9em; |
| 265 | } |
| 266 | div.toclevel3 { |
| 267 | margin-left: 4em; |
| 268 | font-size: 0.9em; |
| 269 | } |
| 270 | div.toclevel4 { |
| 271 | margin-left: 6em; |
| 272 | font-size: 0.9em; |
| 273 | } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 274 | /* Workarounds for IE6's broken and incomplete CSS2. */ |
| 275 | |
| 276 | div.sidebar-content { |
| 277 | background: #ffffee; |
| 278 | border: 1px solid silver; |
| 279 | padding: 0.5em; |
| 280 | } |
| 281 | div.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 | |
| 288 | div.listingblock div.content { |
| 289 | border: 1px solid silver; |
| 290 | background: #f4f4f4; |
| 291 | padding: 0.5em; |
| 292 | } |
| 293 | |
| 294 | div.quoteblock-content { |
| 295 | padding-left: 2.0em; |
| 296 | } |
| 297 | |
| 298 | div.exampleblock-content { |
| 299 | border-left: 2px solid silver; |
| 300 | padding-left: 0.5em; |
| 301 | } |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 302 | |
| 303 | /* IE6 sets dynamically generated links as visited. */ |
| 304 | div#toc a:visited { color: blue; } |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 305 | </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 Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 314 | <div class="para"><p>The parse-options API is used to parse and massage options in git |
| 315 | and to provide a usage help with consistent look.</p></div> |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 316 | </div> |
| 317 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 318 | <h2 id="_basics">Basics</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 319 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 320 | <div class="para"><p>The argument vector <tt>argv[]</tt> may usually contain mandatory or optional |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 321 | <em>non-option arguments</em>, e.g. a filename or a branch, and <em>options</em>. |
| 322 | Options are optional arguments that start with a dash and |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 323 | that allow to change the behavior of a command.</p></div> |
| 324 | <div class="ilist"><ul> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 325 | <li> |
| 326 | <p> |
| 327 | There 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> |
| 336 | There 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> |
| 345 | Options are case-sensitive. |
| 346 | Please define <em>lower-case long options</em> only. |
| 347 | </p> |
| 348 | </li> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 349 | </ul></div> |
| 350 | <div class="para"><p>The parse-options API allows:</p></div> |
| 351 | <div class="ilist"><ul> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 352 | <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> |
| 361 | Long options may be <em>abbreviated</em>, as long as the abbreviation |
| 362 | is unambiguous. |
| 363 | </p> |
| 364 | </li> |
| 365 | <li> |
| 366 | <p> |
| 367 | Short 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> |
| 372 | Boolean 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> |
| 378 | Options 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 Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 383 | </ul></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 384 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 385 | <h2 id="_steps_to_parse_options">Steps to parse options</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 386 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 387 | <div class="olist"><ol> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 388 | <li> |
| 389 | <p> |
| 390 | <tt>#include "parse-options.h"</tt> |
| 391 | </p> |
| 392 | </li> |
| 393 | <li> |
| 394 | <p> |
| 395 | define 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> |
| 402 | define <tt>builtin_foo_options</tt> array as described below |
| 403 | in section <em>Data Structure</em>. |
| 404 | </p> |
| 405 | </li> |
| 406 | <li> |
| 407 | <p> |
| 408 | in <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 Hamano | 3d14151 | 2009-06-01 01:22:40 | [diff] [blame^] | 413 | <pre><tt>argc = parse_options(argc, argv, prefix, builtin_foo_options, builtin_foo_usage, flags);</tt></pre> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 414 | </div></div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 415 | <div class="para"><p><tt>parse_options()</tt> will filter out the processed options of <tt>argv[]</tt> and leave the |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 416 | non-option arguments in <tt>argv[]</tt>. |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 417 | <tt>argc</tt> is updated appropriately because of the assignment.</p></div> |
Junio C Hamano | 3d14151 | 2009-06-01 01:22:40 | [diff] [blame^] | 418 | <div class="para"><p>You can also pass NULL instead of a usage array as the fifth parameter of |
Junio C Hamano | 18f5140 | 2009-03-10 16:39:13 | [diff] [blame] | 419 | parse_options(), to avoid displaying a help screen with usage info and |
| 420 | option list. This should only be done if necessary, e.g. to implement |
| 421 | a limited parser for only a subset of the options that needs to be run |
| 422 | before the full parser, which in turn shows the full help message.</p></div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 423 | <div class="para"><p>Flags are the bitwise-or of:</p></div> |
| 424 | <div class="vlist"><dl> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 425 | <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 Hamano | 18f5140 | 2009-03-10 16:39:13 | [diff] [blame] | 444 | <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>—unknown —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>—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>—help</tt> and |
| 476 | <tt>—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 Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 481 | </dl></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 482 | </li> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 483 | </ol></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 484 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 485 | <h2 id="_data_structure">Data Structure</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 486 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 487 | <div class="para"><p>The main data structure is an array of the <tt>option</tt> struct, |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 488 | say <tt>static struct option builtin_add_options[]</tt>. |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 489 | There are some macros to easily define options:</p></div> |
| 490 | <div class="vlist"><dl> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 491 | <dt> |
| 492 | <tt>OPT__ABBREV(&int_var)</tt> |
| 493 | </dt> |
| 494 | <dd> |
| 495 | <p> |
| 496 | Add <tt>--abbrev[=<n>]</tt>. |
| 497 | </p> |
| 498 | </dd> |
| 499 | <dt> |
| 500 | <tt>OPT__DRY_RUN(&int_var)</tt> |
| 501 | </dt> |
| 502 | <dd> |
| 503 | <p> |
| 504 | Add <tt>-n, --dry-run</tt>. |
| 505 | </p> |
| 506 | </dd> |
| 507 | <dt> |
| 508 | <tt>OPT__QUIET(&int_var)</tt> |
| 509 | </dt> |
| 510 | <dd> |
| 511 | <p> |
| 512 | Add <tt>-q, --quiet</tt>. |
| 513 | </p> |
| 514 | </dd> |
| 515 | <dt> |
| 516 | <tt>OPT__VERBOSE(&int_var)</tt> |
| 517 | </dt> |
| 518 | <dd> |
| 519 | <p> |
| 520 | Add <tt>-v, --verbose</tt>. |
| 521 | </p> |
| 522 | </dd> |
| 523 | <dt> |
| 524 | <tt>OPT_GROUP(description)</tt> |
| 525 | </dt> |
| 526 | <dd> |
| 527 | <p> |
| 528 | Start an option group. <tt>description</tt> is a short string that |
| 529 | describes the group or an empty string. |
| 530 | Start the description with an upper-case letter. |
| 531 | </p> |
| 532 | </dd> |
| 533 | <dt> |
| 534 | <tt>OPT_BOOLEAN(short, long, &int_var, description)</tt> |
| 535 | </dt> |
| 536 | <dd> |
| 537 | <p> |
| 538 | Introduce a boolean option. |
| 539 | <tt>int_var</tt> is incremented on each use. |
| 540 | </p> |
| 541 | </dd> |
| 542 | <dt> |
| 543 | <tt>OPT_BIT(short, long, &int_var, description, mask)</tt> |
| 544 | </dt> |
| 545 | <dd> |
| 546 | <p> |
| 547 | Introduce a boolean option. |
| 548 | If used, <tt>int_var</tt> is bitwise-ored with <tt>mask</tt>. |
| 549 | </p> |
| 550 | </dd> |
| 551 | <dt> |
Junio C Hamano | 7ae1825 | 2009-05-23 09:53:32 | [diff] [blame] | 552 | <tt>OPT_NEGBIT(short, long, &int_var, description, mask)</tt> |
| 553 | </dt> |
| 554 | <dd> |
| 555 | <p> |
| 556 | Introduce a boolean option. |
| 557 | If used, <tt>int_var</tt> is bitwise-anded with the inverted <tt>mask</tt>. |
| 558 | </p> |
| 559 | </dd> |
| 560 | <dt> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 561 | <tt>OPT_SET_INT(short, long, &int_var, description, integer)</tt> |
| 562 | </dt> |
| 563 | <dd> |
| 564 | <p> |
| 565 | Introduce a boolean option. |
| 566 | If used, set <tt>int_var</tt> to <tt>integer</tt>. |
| 567 | </p> |
| 568 | </dd> |
| 569 | <dt> |
| 570 | <tt>OPT_SET_PTR(short, long, &ptr_var, description, ptr)</tt> |
| 571 | </dt> |
| 572 | <dd> |
| 573 | <p> |
| 574 | Introduce a boolean option. |
| 575 | If used, set <tt>ptr_var</tt> to <tt>ptr</tt>. |
| 576 | </p> |
| 577 | </dd> |
| 578 | <dt> |
| 579 | <tt>OPT_STRING(short, long, &str_var, arg_str, description)</tt> |
| 580 | </dt> |
| 581 | <dd> |
| 582 | <p> |
| 583 | Introduce an option with string argument. |
| 584 | The string argument is put into <tt>str_var</tt>. |
| 585 | </p> |
| 586 | </dd> |
| 587 | <dt> |
| 588 | <tt>OPT_INTEGER(short, long, &int_var, description)</tt> |
| 589 | </dt> |
| 590 | <dd> |
| 591 | <p> |
| 592 | Introduce an option with integer argument. |
| 593 | The integer is put into <tt>int_var</tt>. |
| 594 | </p> |
| 595 | </dd> |
| 596 | <dt> |
| 597 | <tt>OPT_DATE(short, long, &int_var, description)</tt> |
| 598 | </dt> |
| 599 | <dd> |
| 600 | <p> |
| 601 | Introduce an option with date argument, see <tt>approxidate()</tt>. |
| 602 | The timestamp is put into <tt>int_var</tt>. |
| 603 | </p> |
| 604 | </dd> |
| 605 | <dt> |
| 606 | <tt>OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)</tt> |
| 607 | </dt> |
| 608 | <dd> |
| 609 | <p> |
| 610 | Introduce an option with argument. |
| 611 | The argument will be fed into the function given by <tt>func_ptr</tt> |
| 612 | and the result will be put into <tt>var</tt>. |
| 613 | See <em>Option Callbacks</em> below for a more elaborate description. |
| 614 | </p> |
| 615 | </dd> |
| 616 | <dt> |
Junio C Hamano | 3d14151 | 2009-06-01 01:22:40 | [diff] [blame^] | 617 | <tt>OPT_FILENAME(short, long, &var, description)</tt> |
| 618 | </dt> |
| 619 | <dd> |
| 620 | <p> |
| 621 | Introduce an option with a filename argument. |
| 622 | The filename will be prefixed by passing the filename along with |
| 623 | the prefix argument of <tt>parse_options()</tt> to <tt>prefix_filename()</tt>. |
| 624 | </p> |
| 625 | </dd> |
| 626 | <dt> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 627 | <tt>OPT_ARGUMENT(long, description)</tt> |
| 628 | </dt> |
| 629 | <dd> |
| 630 | <p> |
| 631 | Introduce a long-option argument that will be kept in <tt>argv[]</tt>. |
| 632 | </p> |
| 633 | </dd> |
Junio C Hamano | 7ae1825 | 2009-05-23 09:53:32 | [diff] [blame] | 634 | <dt> |
| 635 | <tt>OPT_NUMBER_CALLBACK(&var, description, func_ptr)</tt> |
| 636 | </dt> |
| 637 | <dd> |
| 638 | <p> |
| 639 | Recognize numerical options like -123 and feed the integer as |
| 640 | if it was an argument to the function given by <tt>func_ptr</tt>. |
| 641 | The result will be put into <tt>var</tt>. There can be only one such |
| 642 | option definition. It cannot be negated and it takes no |
| 643 | arguments. Short options that happen to be digits take |
| 644 | precedence over it. |
| 645 | </p> |
| 646 | </dd> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 647 | </dl></div> |
| 648 | <div class="para"><p>The last element of the array must be <tt>OPT_END()</tt>.</p></div> |
| 649 | <div class="para"><p>If not stated otherwise, interpret the arguments as follows:</p></div> |
| 650 | <div class="ilist"><ul> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 651 | <li> |
| 652 | <p> |
| 653 | <tt>short</tt> is a character for the short option |
| 654 | (e.g. <tt>'e'</tt> for <tt>-e</tt>, use <tt>0</tt> to omit), |
| 655 | </p> |
| 656 | </li> |
| 657 | <li> |
| 658 | <p> |
| 659 | <tt>long</tt> is a string for the long option |
| 660 | (e.g. <tt>"example"</tt> for <tt>--example</tt>, use <tt>NULL</tt> to omit), |
| 661 | </p> |
| 662 | </li> |
| 663 | <li> |
| 664 | <p> |
| 665 | <tt>int_var</tt> is an integer variable, |
| 666 | </p> |
| 667 | </li> |
| 668 | <li> |
| 669 | <p> |
| 670 | <tt>str_var</tt> is a string variable (<tt>char *</tt>), |
| 671 | </p> |
| 672 | </li> |
| 673 | <li> |
| 674 | <p> |
| 675 | <tt>arg_str</tt> is the string that is shown as argument |
| 676 | (e.g. <tt>"branch"</tt> will result in <tt><branch></tt>). |
| 677 | If set to <tt>NULL</tt>, three dots (<tt>…</tt>) will be displayed. |
| 678 | </p> |
| 679 | </li> |
| 680 | <li> |
| 681 | <p> |
| 682 | <tt>description</tt> is a short string to describe the effect of the option. |
| 683 | It shall begin with a lower-case letter and a full stop (<tt>.</tt>) shall be |
| 684 | omitted at the end. |
| 685 | </p> |
| 686 | </li> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 687 | </ul></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 688 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 689 | <h2 id="_option_callbacks">Option Callbacks</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 690 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 691 | <div class="para"><p>The function must be defined in this form:</p></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 692 | <div class="literalblock"> |
| 693 | <div class="content"> |
| 694 | <pre><tt>int func(const struct option *opt, const char *arg, int unset)</tt></pre> |
| 695 | </div></div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 696 | <div class="para"><p>The callback mechanism is as follows:</p></div> |
| 697 | <div class="ilist"><ul> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 698 | <li> |
| 699 | <p> |
Junio C Hamano | d4f35a9 | 2009-05-17 05:39:29 | [diff] [blame] | 700 | Inside <tt>func</tt>, the only interesting member of the structure |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 701 | given by <tt>opt</tt> is the void pointer <tt>opt->value</tt>. |
| 702 | <tt>*opt->value</tt> will be the value that is saved into <tt>var</tt>, if you |
| 703 | use <tt>OPT_CALLBACK()</tt>. |
| 704 | For example, do <tt>*(unsigned long *)opt->value = 42;</tt> to get 42 |
| 705 | into an <tt>unsigned long</tt> variable. |
| 706 | </p> |
| 707 | </li> |
| 708 | <li> |
| 709 | <p> |
| 710 | Return value <tt>0</tt> indicates success and non-zero return |
| 711 | value will invoke <tt>usage_with_options()</tt> and, thus, die. |
| 712 | </p> |
| 713 | </li> |
| 714 | <li> |
| 715 | <p> |
| 716 | If the user negates the option, <tt>arg</tt> is <tt>NULL</tt> and <tt>unset</tt> is 1. |
| 717 | </p> |
| 718 | </li> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 719 | </ul></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 720 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 721 | <h2 id="_sophisticated_option_parsing">Sophisticated option parsing</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 722 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 723 | <div class="para"><p>If you need, for example, option callbacks with optional arguments |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 724 | or without arguments at all, or if you need other special cases, |
| 725 | that are not handled by the macros above, you need to specify the |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 726 | members of the <tt>option</tt> structure manually.</p></div> |
| 727 | <div class="para"><p>This is not covered in this document, but well documented |
| 728 | in <tt>parse-options.h</tt> itself.</p></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 729 | </div> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 730 | <h2 id="_examples">Examples</h2> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 731 | <div class="sectionbody"> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 732 | <div class="para"><p>See <tt>test-parse-options.c</tt> and |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 733 | <tt>builtin-add.c</tt>, |
| 734 | <tt>builtin-clone.c</tt>, |
| 735 | <tt>builtin-commit.c</tt>, |
| 736 | <tt>builtin-fetch.c</tt>, |
| 737 | <tt>builtin-fsck.c</tt>, |
| 738 | <tt>builtin-rm.c</tt> |
Junio C Hamano | 4604fe5 | 2008-09-19 06:37:10 | [diff] [blame] | 739 | for real-world examples.</p></div> |
Junio C Hamano | 4224f99 | 2008-06-23 07:14:08 | [diff] [blame] | 740 | </div> |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 741 | <div id="footer"> |
| 742 | <div id="footer-text"> |
Junio C Hamano | 3d14151 | 2009-06-01 01:22:40 | [diff] [blame^] | 743 | Last updated 2009-06-01 01:22:22 UTC |
Junio C Hamano | 3dac504 | 2007-12-15 08:40:54 | [diff] [blame] | 744 | </div> |
| 745 | </div> |
| 746 | </body> |
| 747 | </html> |