HasOption() public method

public HasOption ( String opt ) : bool
opt String
return bool
示例#1
0
        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)));
        }
示例#2
0
        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);
        }
示例#3
0
        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;
                }
            }
        }
示例#4
0
        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));
        }
示例#5
0
        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));
        }
示例#6
0
        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;
                }
            }
        }
示例#7
0
        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;
        }
示例#8
0
        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)));
        }
示例#9
0
        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);
        }
示例#10
0
        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);
        }