Changeset 73

Show
Ignore:
Timestamp:
01/09/07 07:13:10 (2 years ago)
Author:
KirkMcDonald
Message:

'/' arg improvements.

Files:

Legend:

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

    r72 r73  
    3030 
    3131/* 
    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. 
     32Options may be in three forms: long and short and slash. Short options start 
     33with a single dash and are one letter long. Long options start with two dashes 
     34and may consist of any number of characters (so long as they don't start with a 
     35dash, though they may contain dashes). Slash options start with a single 
     36forward slash, and may also consist of any number of characters. All options 
     37are case-sensitive. 
    3638 
    3739Short options may be combined. The following are equivalent: 
     
    4143$ myapp -ab -c 
    4244 
    43 If -f and --file are aliases of the same option, which accepts an argument, 
    44 the following are equivalent: 
     45Slash options may occur in groups in a different way: 
     46 
     47$ myapp /d/e/f 
     48 
     49If -f, --file, and /F are aliases of the same option, which accepts an 
     50argument, the following are equivalent: 
    4551 
    4652$ myapp -f somefile.txt 
     
    4854$ myapp --file somefile.txt 
    4955$ myapp --file=somefile.txt 
     56$ myapp /F somefile.txt 
     57$ myapp /F=somefile.txt 
    5058 
    5159The following are also valid: 
     
    5765If an option occurs multiple times, the last one is the one recorded: 
    5866 
    59 $ myapp -f somefile.txt --file otherfile.txt 
    60  
    61 Matches 'otherfile.txt'. 
     67$ myapp -f somefile.txt /F otherfile.txt --file anotherfile.txt 
     68 
     69Matches 'anotherfile.txt'. 
    6270*/ 
    6371 
     
    286294        for (int i=0; i<args.length; ++i) { 
    287295            opt = args[i]; 
    288             if (opt.startswith("--") || opt.startswith("/")) { 
     296            if (opt.startswith("--")) { 
    289297                idx = find(opt, '='); 
    290298                if (idx != -1) { 
     
    337345                    unknown_opt_error("-"); 
    338346                } 
     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); 
     380                } 
    339381            } else { 
    340382                options.args ~= opt;