protected override String[] Flatten(Options options, string[] arguments, bool stopAtNonOption) { Init(); int argc = arguments.Length; for (int i = 0; i < argc; i++) { // get the next command line token string token = arguments[i]; // handle long option --foo or --foo=bar if (token.StartsWith("--")) { int pos = token.IndexOf('='); String opt = pos == -1 ? token : token.Substring(0, pos); // --foo if (!options.HasOption(opt)) { ProcessNonOptionToken(token, stopAtNonOption); } else { currentOption = options.GetOption(opt); tokens.Add(opt); if (pos != -1) { tokens.Add(token.Substring(pos + 1)); } } } // single hyphen else if ("-".Equals(token)) { tokens.Add(token); } else if (token.StartsWith("-")) { if (token.Length == 2 || options.HasOption(token)) { ProcessOptionToken(options, token, stopAtNonOption); } // requires bursting else { BurstToken(options, token, stopAtNonOption); } } else { ProcessNonOptionToken(token, stopAtNonOption); } Gobble(arguments, ref i); } return((String[])tokens.ToArray(typeof(String))); }
private void ProcessOptionToken(Options options, string token, bool stopAtNonOption) { if (stopAtNonOption && !options.HasOption(token)) { eatTheRest = true; } if (options.HasOption(token)) { currentOption = options.GetOption(token); } tokens.Add(token); }
protected void BurstToken(Options options, string token, bool stopAtNonOption) { for (int i = 1; i < token.Length; i++) { String ch = token[i].ToString(); if (options.HasOption(ch)) { tokens.Add("-" + ch); currentOption = options.GetOption(ch); if (currentOption.HasArgument() && (token.Length != (i + 1))) { tokens.Add(token.Substring(i + 1)); break; } } else if (stopAtNonOption) { ProcessNonOptionToken(token.Substring(i), true); break; } else { tokens.Add(token); break; } } }
protected override String[] Flatten(Options options, string[] arguments, bool stopAtNonOption) { Init(); int argc = arguments.Length; for (int i = 0; i < argc; i++) { // get the next command line token string token = arguments[i]; // handle long option --foo or --foo=bar if (token.StartsWith("--")) { int pos = token.IndexOf('='); String opt = pos == -1 ? token : token.Substring(0, pos); // --foo if (!options.HasOption(opt)) { ProcessNonOptionToken(token, stopAtNonOption); } else { currentOption = options.GetOption(opt); tokens.Add(opt); if (pos != -1) { tokens.Add(token.Substring(pos + 1)); } } } // single hyphen else if ("-".Equals(token)) { tokens.Add(token); } else if (token.StartsWith("-")) { if (token.Length == 2 || options.HasOption(token)) { ProcessOptionToken(options, token, stopAtNonOption); } // requires bursting else { BurstToken(options, token, stopAtNonOption); } } else { ProcessNonOptionToken(token, stopAtNonOption); } Gobble(arguments, ref i); } return (String[])tokens.ToArray(typeof(String)); }
protected override String[] Flatten(Options options, string[] arguments, bool stopAtNonOption) { ArrayList tokens = new ArrayList(); bool eatTheRest = false; for (int i = 0; i < arguments.Length; i++) { String arg = arguments[i]; if ("--".Equals(arg)) { eatTheRest = true; tokens.Add("--"); } else if ("-".Equals(arg)) { tokens.Add("-"); } else if (arg.StartsWith("-")) { String opt = Util.StripLeadingHyphens(arg); if (options.HasOption(opt)) { tokens.Add(arg); } else { if (opt.IndexOf('=') != -1 && options.HasOption(opt.Substring(0, opt.IndexOf('=')))) { // the format is --foo=value or -foo=value tokens.Add(arg.Substring(0, arg.IndexOf('='))); // --foo tokens.Add(arg.Substring(arg.IndexOf('=') + 1)); // value } else if (options.HasOption(arg.Substring(0, 2))) { // the format is a special properties option (-Dproperty=value) tokens.Add(arg.Substring(0, 2)); // -D tokens.Add(arg.Substring(2)); // property=value } else { eatTheRest = stopAtNonOption; tokens.Add(arg); } } } else { tokens.Add(arg); } if (eatTheRest) { for (i++; i < arguments.Length; i++) { tokens.Add(arguments[i]); } } } return (String[]) tokens.ToArray(typeof (string)); }
public ICommandLine Parse(Options options, string[] arguments, IEnumerable<KeyValuePair<string, string>> properties, bool stopAtNonOption) { if (options == null) return new CommandLine(false); if (values != null) { // clear out the data in options in case it's been used before foreach (OptionValue option in values.Values) { option.ClearValues(); } } cmd = new CommandLine(true); bool eatTheRest = false; if (arguments == null) arguments = new string[0]; string[] tokenList = Flatten(options, arguments, stopAtNonOption); int tokenCount = tokenList.Length; for (int i = 0; i < tokenCount; i++) { string t = tokenList[i]; // the value is the double-dash if ("--".Equals(t)) { eatTheRest = true; } // the value is a single dash else if ("-".Equals(t)) { if (stopAtNonOption) { eatTheRest = true; } else { cmd.AddArgument(t); } } // the value is an option else if (t.StartsWith("-")) { if (stopAtNonOption && !options.HasOption(t)) { eatTheRest = true; cmd.AddArgument(t); } else { ProcessOption(options, t, tokenList, ref i); } } // the value is an argument else { cmd.AddArgument(t); if (stopAtNonOption) { eatTheRest = true; } } // eat the remaining tokens if (eatTheRest) { while (++i < tokenCount) { String str = tokenList[i]; // ensure only one double-dash is added if (!"--".Equals(str)) { cmd.AddArgument(str); } } } } ProcessProperties(options, properties); CheckRequiredOptions(options); return cmd; }
protected override String[] Flatten(Options options, string[] arguments, bool stopAtNonOption) { ArrayList tokens = new ArrayList(); bool eatTheRest = false; for (int i = 0; i < arguments.Length; i++) { String arg = arguments[i]; if ("--".Equals(arg)) { eatTheRest = true; tokens.Add("--"); } else if ("-".Equals(arg)) { tokens.Add("-"); } else if (arg.StartsWith("-")) { String opt = Util.StripLeadingHyphens(arg); if (options.HasOption(opt)) { tokens.Add(arg); } else { if (opt.IndexOf('=') != -1 && options.HasOption(opt.Substring(0, opt.IndexOf('=')))) { // the format is --foo=value or -foo=value tokens.Add(arg.Substring(0, arg.IndexOf('='))); // --foo tokens.Add(arg.Substring(arg.IndexOf('=') + 1)); // value } else if (options.HasOption(arg.Substring(0, 2))) { // the format is a special properties option (-Dproperty=value) tokens.Add(arg.Substring(0, 2)); // -D tokens.Add(arg.Substring(2)); // property=value } else { eatTheRest = stopAtNonOption; tokens.Add(arg); } } } else { tokens.Add(arg); } if (eatTheRest) { for (i++; i < arguments.Length; i++) { tokens.Add(arguments[i]); } } } return((String[])tokens.ToArray(typeof(string))); }
public ICommandLine Parse(Options options, string[] arguments, IEnumerable <KeyValuePair <string, string> > properties, bool stopAtNonOption) { if (options == null) { return(new CommandLine(false)); } if (values != null) { // clear out the data in options in case it's been used before foreach (OptionValue option in values.Values) { option.ClearValues(); } } cmd = new CommandLine(true); bool eatTheRest = false; if (arguments == null) { arguments = new string[0]; } string[] tokenList = Flatten(options, arguments, stopAtNonOption); int tokenCount = tokenList.Length; for (int i = 0; i < tokenCount; i++) { string t = tokenList[i]; // the value is the double-dash if ("--".Equals(t)) { eatTheRest = true; } // the value is a single dash else if ("-".Equals(t)) { if (stopAtNonOption) { eatTheRest = true; } else { cmd.AddArgument(t); } } // the value is an option else if (t.StartsWith("-")) { if (stopAtNonOption && !options.HasOption(t)) { eatTheRest = true; cmd.AddArgument(t); } else { ProcessOption(options, t, tokenList, ref i); } } // the value is an argument else { cmd.AddArgument(t); if (stopAtNonOption) { eatTheRest = true; } } // eat the remaining tokens if (eatTheRest) { while (++i < tokenCount) { String str = tokenList[i]; // ensure only one double-dash is added if (!"--".Equals(str)) { cmd.AddArgument(str); } } } } ProcessProperties(options, properties); CheckRequiredOptions(options); return(cmd); }