| 90 | | <li><tt>Option add_option(char[][] options, char[], void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> |
|---|
| 91 | | <li><tt>Option add_option(char[][] options, char[], void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> |
|---|
| 92 | | <li><tt>Option add_option(char[][] options, char[], void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> |
|---|
| | 90 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> |
|---|
| | 91 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> |
|---|
| | 92 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> |
|---|
| 263 | | <h4>Methods</h4> |
|---|
| 264 | | <h4>Results</h4> |
|---|
| 265 | | <h4>Example</h4> |
|---|
| | 263 | <p>Flag options may be implemented with these actions. The <tt>SetTrue</tt> action will set a flag to true, and <tt>SetFalse</tt> will set it to false.</p> |
|---|
| | 264 | |
|---|
| | 265 | <h4>Methods</h4> |
|---|
| | 266 | <ul> |
|---|
| | 267 | <li><tt>Option add_option(char[][] options, Action action)</tt></li> |
|---|
| | 268 | <li><tt>Option add_option(char[][] options, char[] name, Action action)</tt></li> |
|---|
| | 269 | </ul> |
|---|
| | 270 | |
|---|
| | 271 | <h4>Results</h4> |
|---|
| | 272 | <p>The flag is retrieved using the <tt>flag</tt> method of the <tt>Options</tt> class.</p> |
|---|
| | 273 | |
|---|
| | 274 | <blockquote><tt>bool flag = options.flag("flag");</tt></blockquote> |
|---|
| | 275 | |
|---|
| | 276 | <p>If the flag was not specified, it defaults to false.</p> |
|---|
| | 277 | |
|---|
| | 278 | <h4>Example</h4> |
|---|
| | 279 | <blockquote><pre>void main(char[][] args) { |
|---|
| | 280 | auto parser = new OptionParser(); |
|---|
| | 281 | parser.add_option(["--enable"], "flag", Action.SetTrue); |
|---|
| | 282 | parser.add_option(["--disable"], "flag", Action.SetFalse); |
|---|
| | 283 | auto options = parser.parse_args(args); |
|---|
| | 284 | writefln("flag: %s", options.flag("flag")); |
|---|
| | 285 | }</pre></blockquote> |
|---|
| | 286 | |
|---|
| | 287 | <p>This produces the following on the command-line:</p> |
|---|
| | 288 | |
|---|
| | 289 | <blockquote><pre>$ myapp --enable |
|---|
| | 290 | flag: true |
|---|
| | 291 | $ myapp --enable --disable |
|---|
| | 292 | flag: false |
|---|
| | 293 | $ myapp |
|---|
| | 294 | flag: false</pre></blockquote> |
|---|
| 268 | | <h4>Methods</h4> |
|---|
| 269 | | <h4>Results</h4> |
|---|
| 270 | | <h4>Example</h4> |
|---|
| | 297 | <p>The <tt>Count</tt> action simply counts the number of times the option occurs on the command-line.</p> |
|---|
| | 298 | |
|---|
| | 299 | <h4>Methods</h4> |
|---|
| | 300 | <ul> |
|---|
| | 301 | <li><tt>Option add_option(char[][] options, Action action)</tt></li> |
|---|
| | 302 | <li><tt>Option add_option(char[][] options, char[] name, Action action)</tt></li> |
|---|
| | 303 | </ul> |
|---|
| | 304 | |
|---|
| | 305 | <h4>Results</h4> |
|---|
| | 306 | <p>The count may be retrieved from the <tt>count</tt> method of <tt>Options</tt>.</p> |
|---|
| | 307 | |
|---|
| | 308 | <blockquote><tt>int c = options.count("verbosity");</tt></blockquote> |
|---|
| | 309 | |
|---|
| | 310 | <h4>Example</h4> |
|---|
| | 311 | <blockquote><pre>void main(char[][] args) { |
|---|
| | 312 | auto parser = new OptionParser(); |
|---|
| | 313 | parser.add_option(["-v", "--verbose"], "verbosity", Action.Count); |
|---|
| | 314 | auto options = parser.parse_args(args); |
|---|
| | 315 | writefln("verbosity: %s", options.count("verbosity")); |
|---|
| | 316 | }</pre></blockquote> |
|---|
| | 317 | |
|---|
| | 318 | <p>This produces the following on the command-line:</p> |
|---|
| | 319 | |
|---|
| | 320 | <blockquote><pre>$ myapp -vvv |
|---|
| | 321 | verbosity: 3 |
|---|
| | 322 | $ myapp -v --verbose |
|---|
| | 323 | verbosity: 2 |
|---|
| | 324 | $ myapp |
|---|
| | 325 | verbosity: 0</pre></blockquote> |
|---|
| 273 | | <h4>Methods</h4> |
|---|
| 274 | | <h4>Results</h4> |
|---|
| 275 | | <h4>Example</h4> |
|---|
| | 328 | <p>Sometimes, the above actions aren't enough. To extend <tt>optparse</tt>, users may provide a callback as an option's action. A callback option may accept an argument. Whether it accepts an argument is determined by the type of the callback.</p> |
|---|
| | 329 | |
|---|
| | 330 | <p><strong>Important note:</strong> A callback is called as soon as it is encountered in the argument list. This means that <tt>optparse</tt> may still exit with an error if it finds malformed or invalid options on the command line, after the callback is run. For this reason, users should not place actual program logic inside of callbacks, and should only use them to store option data for later use.</p> |
|---|
| | 331 | |
|---|
| | 332 | <h4>Methods</h4> |
|---|
| | 333 | <ul> |
|---|
| | 334 | <li><tt>Option add_option(char[][] options, void delegate() dg)</tt></li> |
|---|
| | 335 | <li><tt>Option add_option(char[][] options, void delegate(char[]) dg)</tt></li> |
|---|
| | 336 | <li><tt>Option add_option(char[][] options, void delegate(int) dg)</tt></li> |
|---|
| | 337 | </ul> |
|---|
| | 338 | <h4>Results</h4> |
|---|
| | 339 | <p>The results of a callback option are not stored anywhere by <tt>optparse</tt>. It is assumed the user will keep any information about the callback themselves.</p> |
|---|
| | 340 | |
|---|
| | 341 | <h4>Example</h4> |
|---|
| | 342 | <blockquote><pre>void main(char[][] args) { |
|---|
| | 343 | auto parser = new OptionParser(); |
|---|
| | 344 | parser.add_option(["--callback"], { |
|---|
| | 345 | writefln("Callback encountered!"); |
|---|
| | 346 | }); |
|---|
| | 347 | auto options = parser.parse_args(args); |
|---|
| | 348 | writefln("Done."); |
|---|
| | 349 | }</pre></blockquote> |
|---|
| | 350 | |
|---|
| | 351 | <p>This produces the following on the command-line:</p> |
|---|
| | 352 | |
|---|
| | 353 | <blockquote><pre>$ myapp --callback |
|---|
| | 354 | Callback encountered! |
|---|
| | 355 | Done. |
|---|
| | 356 | $ myapp |
|---|
| | 357 | Done.</pre></blockquote> |
|---|
| 278 | | <h4>Methods</h4> |
|---|
| 279 | | <h4>Results</h4> |
|---|
| 280 | | <h4>Example</h4> |
|---|
| | 360 | <p>If client code wants to handle the parsing process at a lower level, <tt>optparse</tt> provides these advanced callbacks.</p> |
|---|
| | 361 | |
|---|
| | 362 | <p>The first four arguments to these callbacks have the following types and meanings:</p> |
|---|
| | 363 | |
|---|
| | 364 | <dl> |
|---|
| | 365 | <dt><tt>Options</tt></dt> <dd>The current results of parsing the command-line, up to when the callback was encountered.</dd> |
|---|
| | 366 | |
|---|
| | 367 | <dt><tt>inout char[][]</tt></dt> <dd>The args array. This is mostly identical to the array originally passed to <tt>parse_args</tt>, with the following differences: |
|---|
| | 368 | <ol> |
|---|
| | 369 | <li>The first element, with the executable name, has been removed.</li> |
|---|
| | 370 | <li>Options of the form "<tt>--option=argument</tt>" preceeding (or including) this option have been parsed apart into two elements, as in <tt>[--option,argument]</tt>. Elements of the array after the current option have not been touched, yet.</li> |
|---|
| | 371 | </ol> |
|---|
| | 372 | It is useful to note that this argument is declared <tt>inout</tt>, meaning callbacks are free to modify the array.</dd> |
|---|
| | 373 | |
|---|
| | 374 | <dt><tt>inout int</tt></dt> <dd>The index in the args array of this option; or, if this option accepts an argument, of the option's argument. Note that this is declared <tt>inout</tt>. When the callback returns, parsing will resume with the element of args following this <tt>int</tt>. If this argument is advanced beyond the end of the args array, parsing will quietly terminate.</dd> |
|---|
| | 375 | |
|---|
| | 376 | <dt><tt>char[]</tt></dt> <dd>The name of this option.</dd> |
|---|
| | 377 | </dl> |
|---|
| | 378 | |
|---|
| | 379 | <p>The fifth argument to the callback, if it exists, controls the type of the argument to the option.</p> |
|---|
| | 380 | |
|---|
| | 381 | <h4>Methods</h4> |
|---|
| | 382 | <ul> |
|---|
| | 383 | <li><tt>Option add_option(char[][] options, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> |
|---|
| | 384 | <li><tt>Option add_option(char[][] options, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> |
|---|
| | 385 | <li><tt>Option add_option(char[][] options, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> |
|---|
| | 386 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[]) dg)</tt></li> |
|---|
| | 387 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], char[]) dg)</tt></li> |
|---|
| | 388 | <li><tt>Option add_option(char[][] options, char[] name, void delegate(Options, inout char[][], inout int, char[], int) dg)</tt></li> |
|---|
| | 389 | </ul> |
|---|
| | 390 | <h4>Results</h4> |
|---|
| | 391 | <p>As with regular callbacks, <tt>optparse</tt> does not store the results of advanced callbacks.</p> |
|---|
| | 392 | |
|---|
| | 393 | <h4>Example</h4> |
|---|
| | 394 | <blockquote><pre>void main(char[][] args) { |
|---|
| | 395 | auto parser = new OptionParser(); |
|---|
| | 396 | parser.add_option( |
|---|
| | 397 | ["--adv-callback"], |
|---|
| | 398 | (Options o, inout char[][] a, inout int i, char[] name) { |
|---|
| | 399 | // Skip the next argument, whatever it is. |
|---|
| | 400 | ++i; |
|---|
| | 401 | } |
|---|
| | 402 | ); |
|---|
| | 403 | parser.add_option(["-c"], Action.Count); |
|---|
| | 404 | auto options = parser.parse_args(args); |
|---|
| | 405 | writefln("count: %s", options.count("c")); |
|---|
| | 406 | }</pre></blockquote> |
|---|
| | 407 | |
|---|
| | 408 | <p>This produces the following on the command-line:</p> |
|---|
| | 409 | |
|---|
| | 410 | <blockquote><pre>$ myapp -c --adv-callback -cc |
|---|
| | 411 | count: 1</pre></blockquote> |
|---|
| 283 | | <h4>Methods</h4> |
|---|
| 284 | | <h4>Results</h4> |
|---|
| 285 | | <h4>Example</h4> |
|---|
| | 414 | <p>The <tt>Help</tt> action prints out a listing of the options in the parser.</p> |
|---|
| | 415 | |
|---|
| | 416 | <p>The listing begins with a header like the following:</p> |
|---|
| | 417 | |
|---|
| | 418 | <blockquote><pre>Usage: myapp [options] args... |
|---|
| | 419 | A short description of myapp. |
|---|
| | 420 | |
|---|
| | 421 | Options:</pre></blockquote> |
|---|
| | 422 | |
|---|
| | 423 | <p>This header is customizable in two ways:</p> |
|---|
| | 424 | <ul> |
|---|
| | 425 | <li>The "<tt>[options] args...</tt>" portion is contained in the <tt>argdesc</tt> member variable of the <tt>OptionParser</tt> class. Users may change this member to whatever they like in their instance.</li> |
|---|
| | 426 | <li>The short description should be provided to the <tt>OptionParser</tt> constructor.</li> |
|---|
| | 427 | </ul> |
|---|
| | 428 | |
|---|
| | 429 | <p>Additionally, the name of the application is derived directly from the first element of the args array.</p> |
|---|
| | 430 | |
|---|
| | 431 | <p>Options are printed in the following form. Given these options:</p> |
|---|
| | 432 | |
|---|
| | 433 | <blockquote><pre>parser.add_option("-f", "--file").help("The file to operate on.") |
|---|
| | 434 | parser.add_option(["--enable"], Action.SetTrue).help("Enables the option.")</pre></blockquote> |
|---|
| | 435 | |
|---|
| | 436 | <p><tt>Help</tt> produces the following:</p> |
|---|
| | 437 | |
|---|
| | 438 | <blockquote><pre> -f, --file=FILE The file to operate on. |
|---|
| | 439 | --enable Enables the option.</pre></blockquote> |
|---|
| | 440 | |
|---|
| | 441 | <p>The <tt>help</tt> method of the <tt>Option</tt> class provides the description used in the listing. Notice also the "<tt>=FILE</tt>" in the first option above. This indicates that the option accepts an argument. If you wanted to have this say "<tt>=FILENAME</tt>" instead, you would instead define the <tt>--file</tt> option as:</p> |
|---|
| | 442 | |
|---|
| | 443 | <blockquote<tt>parser.add_option("-f", "--file").help("The file to operate on.").argName("filename");</tt></blockquote> |
|---|
| | 444 | |
|---|
| | 445 | <p>The <tt>argName</tt> method of <tt>Option</tt> class defines this string, which otherwise defaults to the option's name. Note that it always converts the argument's name to all-caps.</p> |
|---|
| | 446 | |
|---|
| | 447 | <h4>Methods</h4> |
|---|
| | 448 | <ul> |
|---|
| | 449 | <li><tt>Option add_option(char[][] options, Action action)</tt></li> |
|---|
| | 450 | </ul> |
|---|
| | 451 | |
|---|
| | 452 | <h4>Results</h4> |
|---|
| | 453 | <p><tt>Help</tt> terminates the program when found.</p> |
|---|
| | 454 | |
|---|
| | 455 | <h4>Example</h4> |
|---|
| | 456 | <p>Bringing it all together, we get this:</p> |
|---|
| | 457 | |
|---|
| | 458 | <blockquote><pre>void main(char[][] args) { |
|---|
| | 459 | auto parser = new OptionParser("Testing the Help action."); |
|---|
| | 460 | parser.argdesc = "[options]"; |
|---|
| | 461 | parser.add_option("-f", "--file").help("Defines the file to use.").argName("filename"); |
|---|
| | 462 | parser.add_option(["-n", "--number"], ArgType.Integer).help("Specifies the number."); |
|---|
| | 463 | parser.add_option(["--blue"], "color", Action.AppendConst, "blue").help("Adds the color blue."); |
|---|
| | 464 | parser.add_option(["--green"], "color", Action.AppendConst, "green").help("Adds the color green."); |
|---|
| | 465 | parser.add_option(["--help"], Action.Help).help("Displays this help message."); |
|---|
| | 466 | auto options = parser.parse_args(args); |
|---|
| | 467 | writefln("Done."); |
|---|
| | 468 | }</pre></blockquote> |
|---|
| | 469 | |
|---|
| | 470 | <p>This produces the following on the command-line:</p> |
|---|
| | 471 | |
|---|
| | 472 | <blockquote><pre>$ myapp --help |
|---|
| | 473 | Usage: myapp [options] |
|---|
| | 474 | Testing the Help action. |
|---|
| | 475 | |
|---|
| | 476 | Options: |
|---|
| | 477 | -f, --file=FILENAME Defines the file to use. |
|---|
| | 478 | -n, --number=NUMBER Specifies the number. |
|---|
| | 479 | --blue Adds the color blue. |
|---|
| | 480 | --green Adds the color green. |
|---|
| | 481 | --help Displays this help message. |
|---|
| | 482 | $ myapp |
|---|
| | 483 | Done.</pre></blockquote> |
|---|
| 337 | | Option add_option(char[][] options, char[], OptionCallbackFancy dg); |
|---|
| 338 | | Option add_option(char[][] options, char[], OptionCallbackFancyArg dg); |
|---|
| 339 | | Option add_option(char[][] options, char[], OptionCallbackFancyInt dg); |
|---|
| | 536 | Option add_option(char[][] options, char[] name, OptionCallbackFancy dg); |
|---|
| | 537 | Option add_option(char[][] options, char[] name, OptionCallbackFancyArg dg); |
|---|
| | 538 | Option add_option(char[][] options, char[] name, OptionCallbackFancyInt dg); |
|---|