Changeset 74
- Timestamp:
- 01/09/07 18:11:47 (2 years ago)
- Files:
-
- misc/optparse.d (modified) (12 diffs)
- misc/opttest.d (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
misc/optparse.d
r73 r74 30 30 31 31 /* 32 Options may be in three forms: long and short and slash. Short options start 33 with a single dash and are one letter long. Long options start with two dashes 34 and may consist of any number of characters (so long as they don't start with a 35 dash, though they may contain dashes). Slash options start with a single 36 forward slash, and may also consist of any number of characters. All options 37 are case-sensitive. 32 Options may be in two forms: long and short. Short options start with a single 33 dash and are one letter long. Long options start with two dashes and may 34 consist of any number of characters (so long as they don't start with a dash, 35 though they may contain dashes). Options are case-sensitive. 38 36 39 37 Short options may be combined. The following are equivalent: … … 43 41 $ myapp -ab -c 44 42 45 Slash options may occur in groups in a different way: 46 47 $ myapp /d/e/f 48 49 If -f, --file, and /F are aliases of the same option, which accepts an 50 argument, the following are equivalent: 43 If -f and --file are aliases of the same option, which accepts an argument, 44 the following are equivalent: 51 45 52 46 $ myapp -f somefile.txt … … 54 48 $ myapp --file somefile.txt 55 49 $ myapp --file=somefile.txt 56 $ myapp /F somefile.txt57 $ myapp /F=somefile.txt58 50 59 51 The following are also valid: … … 65 57 If an option occurs multiple times, the last one is the one recorded: 66 58 67 $ myapp -f somefile.txt /F otherfile.txt --file anotherfile.txt68 69 Matches ' anotherfile.txt'.59 $ myapp -f somefile.txt --file otherfile.txt 60 61 Matches 'otherfile.txt'. 70 62 */ 71 63 … … 126 118 127 119 class Option { 128 char[][] shortopts, longopts , slashopts;120 char[][] shortopts, longopts; 129 121 Action action; 130 122 char[] name; … … 133 125 OptionCallback void_callback; 134 126 this( 135 char[][] shorts, char[][] longs, char[][] slashes,127 char[][] shorts, char[][] longs, 136 128 Action act, char[] name, char[] const_value, 137 129 OptionCallbackArg dga, OptionCallback dg … … 139 131 this.shortopts = shorts; 140 132 this.longopts = longs; 141 this.slashopts = slashes;142 133 this.action = act; 143 134 this.name = name; … … 211 202 if ( 212 203 arg.length < 2 || 213 ( 214 arg.length == 2 && (arg[0] != '-' || arg[1] == '-') || 215 arg.length > 2 && (arg[0 .. 2] != "--" || arg[2] == '-') 216 ) && arg[0] != '/' 204 arg.length == 2 && (arg[0] != '-' || arg[1] == '-') || 205 arg.length > 2 && (arg[0 .. 2] != "--" || arg[2] == '-') 217 206 ) { 218 207 return false; 219 208 } 220 if (arg[0] == '/') { 221 foreach (opt; slashopts) { 222 if (arg == opt) { 223 return true; 224 } 225 } 226 } else if (arg.length == 2) { 209 if (arg.length == 2) { 227 210 foreach (opt; shortopts) { 228 211 if (arg == opt) { … … 342 325 match.perform_action(options, arg); 343 326 } 344 } else {345 unknown_opt_error("-");346 }347 } else if (opt.startswith("/")) {348 idx = find(opt, '=');349 if (idx != -1) {350 newopt = opt[0 .. idx];351 // Stitch out the old arg, stitch in the newopt, arg pair.352 args = args[0 .. i] ~ [newopt, opt[idx+1 .. $]] ~ args[i+1 .. $];353 opt = newopt;354 }355 for (int j=0; j<opt.length;) {356 idx = j;357 j = find(opt[j+1 .. $], '/');358 if (j == -1) {359 j = opt.length;360 } else {361 j += idx+1; // We searched for j in a slice of opt, remember.362 }363 newopt = opt[idx .. j];364 match = matches(newopt);365 if (match is null) {366 unknown_opt_error(newopt);367 }368 if (match.hasArg) {369 // If this isn't the last opt in a group, or if it is370 // the last element in args, we don't have a valid371 // argument.372 if (j < opt.length || i == args.length-1)373 expected_arg_error(match.name);374 arg = args[i+1];375 ++i;376 } else {377 arg = null;378 }379 match.perform_action(options, arg);380 327 } 381 328 } else { … … 411 358 char[][] shortopts; 412 359 char[][] longopts; 413 char[][] slashopts;414 360 foreach (opt; options) { 415 361 if (opt.length < 2) { … … 417 363 "invalid option string '" ~ opt ~ "': must be at least two characters long" 418 364 ); 419 } else if (opt[0] == '/') {420 slashopts ~= opt;421 365 } else if (opt.length > 2) { 422 366 if (opt[0 .. 2] != "--" || opt[2] == '-') … … 436 380 if (longopts.length > 0) 437 381 name = longopts[0][2 .. $]; 438 else if (slashopts.length > 0)439 name = slashopts[0][1 .. $];440 382 else 441 383 name = shortopts[0][1 .. 2]; 442 384 } 443 this.options ~= new Option(shortopts, longopts, slashopts,action, name, const_value, callback, vcall);444 } 445 } 446 385 this.options ~= new Option(shortopts, longopts, action, name, const_value, callback, vcall); 386 } 387 } 388 misc/opttest.d
r72 r74 14 14 int main(char[][] args) { 15 15 auto parser = new OptionParser; 16 parser.add_option("-f", "--file" , "/f");17 parser.add_option(["-I", "--import" , "/I"], "importPath", Action.Append);18 parser.add_option(["-c", "--callback" , "/C"], {16 parser.add_option("-f", "--file"); 17 parser.add_option(["-I", "--import"], "importPath", Action.Append); 18 parser.add_option(["-c", "--callback"], { 19 19 writefln("callback encountered"); 20 20 }); 21 parser.add_option(["-v", "--verbose" , "/V"], Action.Count);21 parser.add_option(["-v", "--verbose"], Action.Count); 22 22 auto options = parser.parse_args(args); 23 23
