Changeset 74

Show
Ignore:
Timestamp:
01/09/07 18:11:47 (2 years ago)
Author:
KirkMcDonald
Message:

Rollback to revision 71. (Slash options were a stupid idea.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • misc/optparse.d

    r73 r74  
    3030 
    3131/* 
    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. 
     32Options may be in two forms: long and short. Short options start with a single 
     33dash and are one letter long. Long options start with two dashes and may 
     34consist of any number of characters (so long as they don't start with a dash, 
     35though they may contain dashes). Options are case-sensitive. 
    3836 
    3937Short options may be combined. The following are equivalent: 
     
    4341$ myapp -ab -c 
    4442 
    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: 
     43If -f and --file are aliases of the same option, which accepts an argument, 
     44the following are equivalent: 
    5145 
    5246$ myapp -f somefile.txt 
     
    5448$ myapp --file somefile.txt 
    5549$ myapp --file=somefile.txt 
    56 $ myapp /F somefile.txt 
    57 $ myapp /F=somefile.txt 
    5850 
    5951The following are also valid: 
     
    6557If an option occurs multiple times, the last one is the one recorded: 
    6658 
    67 $ myapp -f somefile.txt /F otherfile.txt --file anotherfile.txt 
    68  
    69 Matches 'anotherfile.txt'. 
     59$ myapp -f somefile.txt --file otherfile.txt 
     60 
     61Matches 'otherfile.txt'. 
    7062*/ 
    7163 
     
    126118 
    127119class Option { 
    128     char[][] shortopts, longopts, slashopts
     120    char[][] shortopts, longopts
    129121    Action action; 
    130122    char[] name; 
     
    133125    OptionCallback void_callback; 
    134126    this( 
    135         char[][] shorts, char[][] longs, char[][] slashes, 
     127        char[][] shorts, char[][] longs, 
    136128        Action act, char[] name, char[] const_value, 
    137129        OptionCallbackArg dga, OptionCallback dg 
     
    139131        this.shortopts = shorts; 
    140132        this.longopts = longs; 
    141         this.slashopts = slashes; 
    142133        this.action = act; 
    143134        this.name = name; 
     
    211202        if ( 
    212203            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] == '-') 
    217206        ) { 
    218207            return false; 
    219208        } 
    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) { 
    227210            foreach (opt; shortopts) { 
    228211                if (arg == opt) { 
     
    342325                        match.perform_action(options, arg); 
    343326                    } 
    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 is 
    370                         // the last element in args, we don't have a valid 
    371                         // 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); 
    380327                } 
    381328            } else { 
     
    411358        char[][] shortopts; 
    412359        char[][] longopts; 
    413         char[][] slashopts; 
    414360        foreach (opt; options) { 
    415361            if (opt.length < 2) { 
     
    417363                    "invalid option string '" ~ opt ~ "': must be at least two characters long" 
    418364                ); 
    419             } else if (opt[0] == '/') { 
    420                 slashopts ~= opt; 
    421365            } else if (opt.length > 2) { 
    422366                if (opt[0 .. 2] != "--" || opt[2] == '-') 
     
    436380            if (longopts.length > 0) 
    437381                name = longopts[0][2 .. $]; 
    438             else if (slashopts.length > 0) 
    439                 name = slashopts[0][1 .. $]; 
    440382            else 
    441383                name = shortopts[0][1 .. 2]; 
    442384        } 
    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  
    1414int main(char[][] args) { 
    1515    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"], { 
    1919        writefln("callback encountered"); 
    2020    }); 
    21     parser.add_option(["-v", "--verbose", "/V"], Action.Count); 
     21    parser.add_option(["-v", "--verbose"], Action.Count); 
    2222    auto options = parser.parse_args(args); 
    2323