Example #1
0
        private void ParseShortOptionSet(string name)
        {
            char[] itName = name.ToCharArray();

            for (int i = 0; i < itName.Length; ++i)
            {
                string currentOption = itName[i].ToString();
                if (_definition.HasShortcut(currentOption) == false)
                {
                    throw new RuntimeException(String.Format("The option \"{0}\" does not exist", currentOption));
                }

                InputOption option = _definition.GetOptionForShortcut(currentOption);

                if (option.AcceptsValue())
                {
                    AddLongOption(option.Name, i == itName.Length - 1 ? null : name.Substring(i + 1));
                    break;
                }
                else
                {
                    AddLongOption(option.Name);
                }
            }
        }
        public void AddOption(InputOption option)
        {
            if (_options.ContainsKey(option.Name))
            {
                throw new LogicException(String.Format("An option with \"{0}\" name already exists", option.Name));
            }

            if (_shortcuts.ContainsKey(option.Shortcut))
            {
                throw new LogicException(String.Format("There already is a \"{0}\" shortcut", option.Shortcut));
            }

            _shortcuts.Add(option.Shortcut, option.Name);
            _options.Add(option.Name, option);
        }
Example #3
0
        private void AddLongOption(string name, string val = null)
        {
            if (_definition.HasOption(name) == false)
            {
                throw new RuntimeException(String.Format("The option \"{0}\" does not exist", name));
            }

            InputOption option = _definition.GetOption(name);


            if (option.AcceptsValue() == false && val != null)
            {
                throw new RuntimeException(String.Format("The option \"{0}\" does not accept a value", option.Name));
            }

            if (val == null && option.AcceptsValue() && _remainingTokens.Count > 0)
            {
                string next = _remainingTokens[0];
                _remainingTokens.RemoveAt(0);
                if (next.Length > 0 && next.StartsWith("-") == false)
                {
                    val = next;
                }
                else if (String.IsNullOrEmpty(next))
                {
                    val = null;
                }
                else
                {
                    next.Insert(0, next);
                }
            }

            if (val == null)
            {
                if (option.Mode == InputOptionValueMode.Required)
                {
                    throw new RuntimeException(String.Format("The option {0} requires a value", name));
                }
                val = option.Mode == InputOptionValueMode.Optional ? option.DefaultValue : "";
            }

            _options.Add(name, val);
        }