Changeset 75
- Timestamp:
- 01/09/07 20:41:38 (2 years ago)
- Files:
-
- misc/optparse.d (modified) (12 diffs)
- misc/opttest.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
misc/optparse.d
r74 r75 28 28 import std.string : split, find; 29 29 import std.c.stdlib : exit, EXIT_FAILURE; 30 import std.conv : toInt, ConvError; 30 31 31 32 /* … … 67 68 } 68 69 70 int toOptInt(char[] s) { 71 int i; 72 try { 73 i = toInt(s); 74 } catch (ConvError e) { 75 error("Could not convert '"~s~"' to an integer."); 76 } 77 return i; 78 } 79 80 // Thrown if client code tries to set up an improper option. 69 81 class OptionError : Exception { 82 this(char[] msg) { super(msg); } 83 } 84 // Thrown if client code tries to extract the wrong type from an option. 85 class OptionTypeError : Exception { 70 86 this(char[] msg) { super(msg); } 71 87 } … … 84 100 } 85 101 } 102 int value(char[] opt) { 103 char[][]* o = opt in opts; 104 if (o) { 105 return toInt((*o)[0]); 106 } else { 107 return 0; 108 } 109 } 86 110 char[][] list(char[] opt) { 87 111 char[][]* o = opt in opts; … … 92 116 } 93 117 } 118 int[] value_list(char[] opt) { 119 char[][]* o = opt in opts; 120 int[] l; 121 if (o) { 122 l.length = (*o).length; 123 foreach (i, s; *o) { 124 l[i] = toInt(s); 125 } 126 } 127 return l; 128 } 94 129 int count(char[] opt) { 95 130 int* c = opt in counted_opts; … … 103 138 104 139 alias void delegate(char[]) OptionCallbackArg; 140 alias void delegate(int) OptionCallbackInt; 105 141 alias void delegate() OptionCallback; 106 142 … … 115 151 * CallbackVoid: dg 116 152 */ 117 enum Action { Store, StoreConst, Append, AppendConst, Count, CallbackVoid, CallbackArg, Help } 153 enum Action { Store, StoreConst, Append, AppendConst, Count, Callback, Help } 154 enum ArgType { None, String, Integer } 155 156 ArgType defaultType(Action action) { 157 switch (action) { 158 case Action.Store, Action.Append, Action.Callback: 159 return ArgType.String; 160 break; 161 default: 162 return ArgType.None; 163 break; 164 } 165 } 118 166 119 167 class Option { 120 168 char[][] shortopts, longopts; 121 169 Action action; 170 ArgType type; 122 171 char[] name; 123 172 char[] const_value; 124 173 OptionCallbackArg callback; 174 OptionCallbackInt int_callback; 125 175 OptionCallback void_callback; 126 176 this( 127 char[][] shorts, char[][] longs, 177 char[][] shorts, char[][] longs, ArgType type, 128 178 Action act, char[] name, char[] const_value, 129 OptionCallbackArg dga, OptionCallback dg 179 OptionCallbackArg dga, OptionCallback dg, 180 OptionCallbackInt dgi 130 181 ) { 131 182 this.shortopts = shorts; 132 183 this.longopts = longs; 133 184 this.action = act; 185 this.type = type; 134 186 this.name = name; 135 187 … … 138 190 switch (act) { 139 191 case Action.Store, Action.Append: 192 assert(type != ArgType.None); 140 193 break; 141 194 case Action.StoreConst, Action.AppendConst: 195 assert(type == ArgType.None); 142 196 assert(const_value !is null); 143 197 break; 144 case Action.CallbackArg: 145 assert(dga !is null); 146 break; 147 case Action.CallbackVoid: 148 assert(dg !is null); 149 break; 150 default: break; 198 case Action.Callback: 199 //assert(type != ArgType.None); 200 switch (type) { 201 case ArgType.String: 202 assert(dga !is null); 203 break; 204 case ArgType.Integer: 205 assert(dgi !is null); 206 break; 207 case ArgType.None: 208 assert(dg !is null); 209 } 210 break; 211 default: 212 break; 151 213 } 152 214 this.const_value = const_value; 153 215 this.callback = dga; 216 this.int_callback = dgi; 154 217 this.void_callback = dg; 155 218 } 156 219 // Does whatever this option is supposed to do. 157 220 void perform_action(Options results, char[] arg) { 158 char[][]* these_args; 221 int i; 222 if (this.type == ArgType.Integer) { 223 // Verify that it's an int. 224 i = toOptInt(arg); 225 } 159 226 switch (this.action) { 160 227 case Action.Store: … … 166 233 case Action.StoreConst: 167 234 assert(arg is null, "Got unexpected argument for '"~name~"' option."); 168 these_args = name in results.opts; 169 if (these_args.length == 0) { 170 (*these_args) ~= const_value; 171 } 235 results.opts[name] = [const_value]; 172 236 break; 173 237 case Action.AppendConst: … … 179 243 ++results.counted_opts[name]; 180 244 break; 181 case Action.CallbackArg: 182 callback(arg); 183 break; 184 case Action.CallbackVoid: 185 void_callback(); 245 case Action.Callback: 246 switch (type) { 247 case ArgType.String: 248 callback(arg); 249 break; 250 case ArgType.Integer: 251 int_callback(i); 252 break; 253 case ArgType.None: 254 void_callback(); 255 break; 256 } 186 257 break; 187 258 case Action.Help: … … 191 262 } 192 263 bool hasArg() { 193 switch (this.action) { 194 case Action.Store, Action.Append, Action.CallbackArg: 195 return true; 196 default: 197 return false; 198 } 264 return this.type != ArgType.None; 199 265 } 200 266 // Returns true if the passed option string matches this option. … … 333 399 } 334 400 401 // options action name type const_value dga dgv dgi 335 402 void add_option(char[][] options ...) { 336 add_option(options, Action.Store, null,null, null, null);403 add_option(options, Action.Store, null, defaultType(Action.Store), null, null, null, null); 337 404 } 338 405 void add_option(char[][] options, char[] name) { 339 add_option(options, Action.Store, name,null, null, null);406 add_option(options, Action.Store, name, defaultType(Action.Store), null, null, null, null); 340 407 } 341 408 void add_option(char[][] options, Action action) { 342 add_option(options, action, null, null, null, null); 409 add_option(options, action, null, defaultType(action), null, null, null, null); 410 } 411 void add_option(char[][] options, ArgType type) { 412 add_option(options, Action.Store, null, type, null, null, null, null); 413 } 414 void add_option(char[][] options, Action action, ArgType type) { 415 add_option(options, action, null, type, null, null, null, null); 343 416 } 344 417 void add_option(char[][] options, char[] name, Action action) { 345 add_option(options, action, name, null, null, null); 418 add_option(options, action, name, defaultType(action), null, null, null, null); 419 } 420 void add_option(char[][] options, char[] name, Action action, ArgType type) { 421 add_option(options, action, name, type, null, null, null, null); 422 } 423 void add_option(char[][] options, Action action, char[] const_value) { 424 add_option(options, action, null, defaultType(action), const_value, null, null, null); 425 } 426 void add_option(char[][] options, char[] name, char[] const_value) { 427 add_option(options, Action.StoreConst, name, defaultType(Action.Store), const_value, null, null, null); 428 } 429 void add_option(char[][] options, char[] name, Action action, char[] const_value) { 430 add_option(options, action, name, defaultType(action), const_value, null, null, null); 346 431 } 347 432 void add_option(char[][] options, OptionCallbackArg dg) { 348 add_option(options, Action.Callback Arg, null, null, dg, null);433 add_option(options, Action.Callback, null, defaultType(Action.Callback), null, dg, null, null); 349 434 } 350 435 void add_option(char[][] options, OptionCallback dg) { 351 add_option(options, Action.CallbackVoid, null, null, null, dg); 352 } 436 add_option(options, Action.Callback, null, ArgType.None, null, null, dg, null); 437 } 438 void add_option(char[][] options, OptionCallbackInt dg) { 439 add_option(options, Action.Callback, null, ArgType.Integer, null, null, null, dg); 440 } 441 // Although users certainly /can/ call this, all those overloads are there 442 // for a reason. 353 443 void add_option( 354 444 char[][] options, 355 Action action, char[] name, char[] const_value, 356 OptionCallbackArg callback, OptionCallback vcall 445 Action action, char[] name, ArgType type, char[] const_value, 446 OptionCallbackArg callback, OptionCallback vcall, 447 OptionCallbackInt icall 357 448 ) { 358 449 char[][] shortopts; … … 383 474 name = shortopts[0][1 .. 2]; 384 475 } 385 this.options ~= new Option(shortopts, longopts, action, name, const_value, callback, vcall);386 } 387 } 388 476 this.options ~= new Option(shortopts, longopts, type, action, name, const_value, callback, vcall, icall); 477 } 478 } 479 misc/opttest.d
r74 r75 4 4 5 5 void print_opts(Options options) { 6 writefln("file: ", options["file"]); 7 writefln("imports: ", options.list("importPath")); 8 writefln("verbosity: ", options.count("verbose")); 6 writefln("file: ", options["file"]); 7 writefln("imports: ", options.list("importPath")); 8 writefln("verbosity: ", options.count("verbose")); 9 writefln("value: ", options.value("value")); 10 writefln("number list: ", options.value_list("list")); 9 11 foreach (a; options.args) { 10 writefln("arg: ", a);12 writefln("arg: ", a); 11 13 } 12 14 } … … 19 21 writefln("callback encountered"); 20 22 }); 23 parser.add_option(["-n", "--number"], (int i) { 24 writefln("number callback: %d * 2 = %d", i, i*2); 25 }); 26 parser.add_option(["-V", "--value"], ArgType.Integer); 27 parser.add_option(["-l", "--list"], Action.Append, ArgType.Integer); 21 28 parser.add_option(["-v", "--verbose"], Action.Count); 22 29 auto options = parser.parse_args(args);
