Changeset 79
- Timestamp:
- 01/13/07 02:21:43 (2 years ago)
- Files:
-
- misc/optparse.d (modified) (16 diffs)
- misc/opttest.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
misc/optparse.d
r78 r79 141 141 } 142 142 } 143 } 143 bool flag(char[] opt) { 144 char[][]* o = opt in opts; 145 if (o) { 146 return (*o)[0] == "1"; 147 } else { 148 return false; 149 } 150 } 151 } 152 153 // Options, args, this opt's index in args, name[, arg] 154 alias void delegate(Options, inout char[][], inout int, char[], char[]) OptionCallbackFancyArg; 155 alias void delegate(Options, inout char[][], inout int, char[], int) OptionCallbackFancyInt; 156 alias void delegate(Options, inout char[][], inout int, char[]) OptionCallbackFancy; 144 157 145 158 alias void delegate(char[]) OptionCallbackArg; 146 159 alias void delegate(int) OptionCallbackInt; 147 alias void delegate() OptionCallback;160 alias void delegate() OptionCallback; 148 161 149 162 /* … … 157 170 * CallbackVoid: dg 158 171 */ 159 enum Action { Store, StoreConst, Append, AppendConst, Count, Callback, Help }172 enum Action { Store, StoreConst, Append, AppendConst, Count, SetTrue, SetFalse, Callback, CallbackFancy, Help } 160 173 enum ArgType { None, String, Integer } 161 174 162 175 ArgType defaultType(Action action) { 163 176 switch (action) { 164 case Action.Store, Action.Append, Action.Callback :177 case Action.Store, Action.Append, Action.Callback, Action.CallbackFancy: 165 178 return ArgType.String; 166 179 break; … … 180 193 OptionCallbackInt int_callback; 181 194 OptionCallback void_callback; 195 196 OptionCallbackFancyArg fancy_callback; 197 OptionCallbackFancyInt fancy_int_callback; 198 OptionCallbackFancy fancy_void_callback; 182 199 char[] helptext; 183 200 this( … … 186 203 OptionCallbackArg dga, OptionCallback dg, 187 204 OptionCallbackInt dgi, 188 char[] help 205 OptionCallbackFancyArg fdga, 206 OptionCallbackFancyInt fdgi, 207 OptionCallbackFancy fdg 189 208 ) { 190 209 this.shortopts = shorts; … … 194 213 this.name = name; 195 214 this.argname = toupper(name); 196 this.helptext = help;197 215 198 216 // Perform sanity checks. … … 217 235 case ArgType.None: 218 236 assert(dg !is null); 219 } 220 break; 237 break; 238 } 239 break; 240 case Action.CallbackFancy: 241 switch (type) { 242 case ArgType.String: 243 assert(fdga !is null); 244 break; 245 case ArgType.Integer: 246 assert(fdgi !is null); 247 break; 248 case ArgType.None: 249 assert(fdg !is null); 250 break; 251 } 221 252 default: 222 253 break; … … 226 257 this.int_callback = dgi; 227 258 this.void_callback = dg; 259 this.fancy_callback = fdga; 260 this.fancy_int_callback = fdgi; 261 this.fancy_void_callback = fdg; 228 262 } 229 263 char[] toString() { … … 242 276 } 243 277 // Does whatever this option is supposed to do. 244 void perform_action(OptionParser parser, Options results, char[] arg) {278 void perform_action(OptionParser parser, Options results, inout char[][] args, inout int idx, char[] arg) { 245 279 int i; 246 280 if (this.type == ArgType.Integer) { … … 267 301 ++results.counted_opts[name]; 268 302 break; 303 case Action.SetTrue: 304 results.opts[name] = ["1"]; 305 break; 306 case Action.SetFalse: 307 results.opts[name] = ["0"]; 308 break; 269 309 case Action.Callback: 270 310 switch (type) { … … 277 317 case ArgType.None: 278 318 void_callback(); 319 break; 320 } 321 break; 322 case Action.CallbackFancy: 323 switch (type) { 324 case ArgType.String: 325 fancy_callback(results, args, idx, name, arg); 326 break; 327 case ArgType.Integer: 328 fancy_int_callback(results, args, idx, name, i); 329 break; 330 case ArgType.None: 331 fancy_void_callback(results, args, idx, name); 279 332 break; 280 333 } … … 462 515 arg = null; 463 516 } 464 match.perform_action(this, options, arg );517 match.perform_action(this, options, args, i, arg); 465 518 } else if (opt.startswith("-")) { 466 519 if (opt.length >= 2) { … … 483 536 } else { 484 537 arg = toUTF8(opt32[j+1 .. $]); 485 match.perform_action(this, options, arg );538 match.perform_action(this, options, args, i, arg); 486 539 break; 487 540 } … … 489 542 arg = null; 490 543 } 491 match.perform_action(this, options, arg );544 match.perform_action(this, options, args, i, arg); 492 545 } 493 546 } else { … … 512 565 return option; 513 566 } 514 // options action name type const_value dga dgv dgi help567 // options action name type const_value dga dgv dgi fdga fdgi fdg 515 568 Option add_option(char[][] options ...) { 516 return add_option(options, Action.Store, null, defaultType(Action.Store), null,null, null, null, null);569 return add_option(options, Action.Store, null, defaultType(Action.Store), null, null, null, null, null, null, null); 517 570 } 518 571 Option add_option(char[][] options, char[] name) { 519 return add_option(options, Action.Store, name, defaultType(Action.Store), null,null, null, null, null);572 return add_option(options, Action.Store, name, defaultType(Action.Store), null, null, null, null, null, null, null); 520 573 } 521 574 Option add_option(char[][] options, Action action) { 522 return add_option(options, action, null, defaultType(action), null,null, null, null, null);575 return add_option(options, action, null, defaultType(action), null, null, null, null, null, null, null); 523 576 } 524 577 Option add_option(char[][] options, ArgType type) { 525 return add_option(options, Action.Store, null, type, null,null, null, null, null);578 return add_option(options, Action.Store, null, type, null, null, null, null, null, null, null); 526 579 } 527 580 Option add_option(char[][] options, Action action, ArgType type) { 528 return add_option(options, action, null, type, null,null, null, null, null);581 return add_option(options, action, null, type, null, null, null, null, null, null, null); 529 582 } 530 583 Option add_option(char[][] options, char[] name, Action action) { 531 return add_option(options, action, name, defaultType(action), null,null, null, null, null);584 return add_option(options, action, name, defaultType(action), null, null, null, null, null, null, null); 532 585 } 533 586 Option add_option(char[][] options, char[] name, Action action, ArgType type) { 534 return add_option(options, action, name, type, null,null, null, null, null);587 return add_option(options, action, name, type, null, null, null, null, null, null, null); 535 588 } 536 589 Option add_option(char[][] options, Action action, char[] const_value) { 537 return add_option(options, action, null, defaultType(action), const_value, null, null, null, null);590 return add_option(options, action, null, defaultType(action), const_value, null, null, null, null, null, null); 538 591 } 539 592 Option add_option(char[][] options, char[] name, char[] const_value) { 540 return add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null);593 return add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null, null, null, null); 541 594 } 542 595 Option add_option(char[][] options, char[] name, Action action, char[] const_value) { 543 return add_option(options, action, name, defaultType(action), const_value, null, null, null, null);596 return add_option(options, action, name, defaultType(action), const_value, null, null, null, null, null, null); 544 597 } 545 598 Option add_option(char[][] options, OptionCallbackArg dg) { 546 return add_option(options, Action.Callback, null, defaultType(Action.Callback), null, dg,null, null, null);599 return add_option(options, Action.Callback, null, ArgType.String, null, dg, null, null, null, null, null); 547 600 } 548 601 Option add_option(char[][] options, OptionCallback dg) { 549 return add_option(options, Action.Callback, null, ArgType.None, null, null, dg,null, null);602 return add_option(options, Action.Callback, null, ArgType.None, null, null, dg, null, null, null, null); 550 603 } 551 604 Option add_option(char[][] options, OptionCallbackInt dg) { 552 return add_option(options, Action.Callback, null, ArgType.Integer, null, null, null, dg, null); 605 return add_option(options, Action.Callback, null, ArgType.Integer, null, null, null, dg, null, null, null); 606 } 607 Option add_option(char[][] options, OptionCallbackFancyArg dg) { 608 return add_option(options, Action.CallbackFancy, null, ArgType.String, null, null, null, null, dg, null, null); 609 } 610 Option add_option(char[][] options, OptionCallbackFancy dg) { 611 return add_option(options, Action.CallbackFancy, null, ArgType.None, null, null, null, null, null, null, dg); 612 } 613 Option add_option(char[][] options, OptionCallbackFancyInt dg) { 614 return add_option(options, Action.CallbackFancy, null, ArgType.Integer, null, null, null, null, null, dg, null); 615 } 616 Option add_option(char[][] options, char[] name, OptionCallbackFancyArg dg) { 617 return add_option(options, Action.CallbackFancy, name, ArgType.String, null, null, null, null, dg, null, null); 618 } 619 Option add_option(char[][] options, char[] name, OptionCallbackFancy dg) { 620 return add_option(options, Action.CallbackFancy, name, ArgType.None, null, null, null, null, null, null, dg); 621 } 622 Option add_option(char[][] options, char[] name, OptionCallbackFancyInt dg) { 623 return add_option(options, Action.CallbackFancy, name, ArgType.Integer, null, null, null, null, null, dg, null); 553 624 } 554 625 // Although users certainly /can/ call this, all those overloads are there … … 559 630 OptionCallbackArg callback, OptionCallback vcall, 560 631 OptionCallbackInt icall, 561 char[] help 632 OptionCallbackFancyArg fdga, 633 OptionCallbackFancyInt fdgi, 634 OptionCallbackFancy fdg 562 635 ) { 563 636 char[][] shortopts; … … 599 672 ); 600 673 } 601 option = new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall, help);674 option = new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall, fdga, fdgi, fdg); 602 675 this.options ~= option; 603 676 return option; misc/opttest.d
r76 r79 9 9 writefln("value: ", options.value("value")); 10 10 writefln("number list: ", options.value_list("list")); 11 writefln("switch: ", options.flag("switch")); 11 12 writefln("args: ", options.args); 12 13 } … … 22 23 writefln("number callback: %d * 2 = %d", i, i*2); 23 24 }).help("Calls a callback with a number."); 25 // A fancy callback 26 parser.add_option(["--fancy"], "a callback", (Options opts, inout char[][] a, inout int i, char[] name) { 27 writefln("Current file: %s", opts["file"]); 28 writefln("Remaining args: %s", a[i+1 .. $]); 29 // Skip the next arg 30 ++i; 31 writefln("This arg's name: %s", name); 32 }).help("Tests optparse's fancy callbacks."); 33 parser.add_option(["--enable"], "switch", Action.SetTrue).help("Enables the switch."); 34 parser.add_option(["--disable"], "switch", Action.SetFalse).help("Disables the switch."); 24 35 parser.add_option(["-V", "--value"], ArgType.Integer).help("Stores a single number").argName("NUMBER"); 25 36 parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer).help("Adds numbers to a list.").argName("NUMBER");
