Esempio n. 1
0
        public OptionToken GetToken(bool expectValue)
        {
            if (m_Arguments == null)
            {
                return(null);
            }

            while (m_ArgumentPosition < m_Arguments.Length && m_Tokens.Count == 0)
            {
                ParseNextArgument(expectValue);
            }

            if (m_Tokens.Count > 0)
            {
                if (expectValue)
                {
                    OptionTokenKind tokenKind = m_Tokens.Peek().Token;
                    if (tokenKind == OptionTokenKind.Argument || tokenKind == OptionTokenKind.Value)
                    {
                        OptionToken token = m_Tokens.Dequeue();
                        token.Token = OptionTokenKind.Value;
                        return(token);
                    }
                    // Didn't find a value/argument
                    return(null);
                }
                return(m_Tokens.Dequeue());
            }
            return(null);
        }
Esempio n. 2
0
        private void ParseShortOption(IOptionParser parser, OptionToken token)
        {
            if (!m_ShortOptionList.TryGetValue(token.Value[0], out OptionData optionData))
            {
                throw new OptionUnknownException(token.ToString(parser));
            }

            ParseOptionParameter(parser, optionData, token);
        }
Esempio n. 3
0
        private void ParseLongOption(IOptionParser parser, OptionToken token)
        {
            string option = parser.LongOptionCaseInsensitive ? token.Value.ToLowerInvariant() : token.Value;

            if (!m_LongOptionList.TryGetValue(option, out OptionData optionData))
            {
                throw new OptionUnknownException(token.ToString(parser));
            }

            ParseOptionParameter(parser, optionData, token);
        }
Esempio n. 4
0
        private void ParseOption(IOptionParser parser, OptionToken token)
        {
            switch (token.Token)
            {
            case OptionTokenKind.ShortOption:
                ParseShortOption(parser, token);
                break;

            case OptionTokenKind.LongOption:
                ParseLongOption(parser, token);
                break;
            }
        }
Esempio n. 5
0
        private void ParseOptionParameter(IOptionParser parser, OptionData optionData, OptionToken token)
        {
            string argument = null;

            try {
                if (optionData.ExpectsValue)
                {
                    OptionToken argumentToken = parser.GetToken(true);
                    if (argumentToken == null)
                    {
                        OptionDefaultAttribute defaultAttribute = null;
                        if (optionData.Member != null)
                        {
                            defaultAttribute = GetAttribute <OptionDefaultAttribute>(optionData.Member);
                        }
                        if (defaultAttribute == null)
                        {
                            throw new OptionMissingArgumentException(token.ToString(parser));
                        }
                        argument = defaultAttribute.DefaultValue;
                    }
                    else
                    {
                        argument = argumentToken.Value;
                    }
                    SetOption(parser, optionData, argument);
                }
                else
                {
                    // This is a boolean type. We can only set it to true.
                    SetBoolean(optionData, true);
                }

                optionData.Set = true;
            } catch (OptionException) {
                throw;
            } catch (Exception e) {
                string message;
                if (argument == null)
                {
                    message = string.Format("Error parsing option '{0}'", token.ToString(parser));
                    throw new OptionException(message);
                }
                message = string.Format("Wrong format '{0}' given to option {1}", argument, token.ToString(parser));
                throw new OptionFormatException(token.Value, message, e);
            }
        }
Esempio n. 6
0
 private void ParseArgument(OptionToken token)
 {
     m_Arguments.Add(token.Value);
 }
Esempio n. 7
0
        /// <summary>
        /// Parses the command line arguments.
        /// </summary>
        /// <param name="arguments">The command line arguments to parse.</param>
        /// <remarks>
        /// Parses the command line arguments provided, setting the fields in the object provided when
        /// this object was instantiated.
        /// </remarks>
        private void ParseCommandLine(string[] arguments)
        {
            OptionToken   token;
            OptionToken   lastToken       = null;
            OptionToken   lastOptionToken = null;
            IOptionParser parser;

            switch (m_OptionsStyle)
            {
            case OptionsStyle.Unix:
                parser = new UnixOptionParser(arguments);
                break;

            case OptionsStyle.Windows:
                parser = new WindowsOptionEnumerator(arguments);
                break;

            default:
                throw new InternalApplicationException("Unknown command style parser");
            }

            BuildOptionList(parser.LongOptionCaseInsensitive);
            IOptions options = m_Options as IOptions;

            if (m_Arguments == null)
            {
                m_Arguments = new List <string>();
            }

            try {
                do
                {
                    string message;
                    token = parser.GetToken(false);
                    if (token == null)
                    {
                        continue;
                    }
                    switch (token.Token)
                    {
                    case OptionTokenKind.ShortOption:
                    case OptionTokenKind.LongOption:
                        if (m_Arguments.Count > 0)
                        {
                            if (lastOptionToken != null)
                            {
                                message = string.Format("Unexpected option '{0}', perhaps too many arguments after '{1}'",
                                                        token.ToString(parser), lastOptionToken.ToString(parser));
                            }
                            else
                            {
                                message = string.Format("Unexpected option '{0}'", token.ToString(parser));
                            }
                            throw new OptionException(message);
                        }
                        ParseOption(parser, token);
                        lastOptionToken = token;
                        break;

                    case OptionTokenKind.Argument:
                        ParseArgument(token);
                        break;

                    case OptionTokenKind.Value:
                        if (lastToken != null)
                        {
                            message = string.Format("Unexpected value for option {0} (argument {1})",
                                                    lastToken.ToString(parser), token);
                            throw new OptionException(message);
                        }
                        message = string.Format("Unexpected value {0}", token);
                        throw new OptionException(message);
                    }
                    lastToken = token;
                } while (token != null);
            } catch (OptionUnknownException e) {
                if (options != null)
                {
                    options.InvalidOption(e.Option);
                }
                throw;
            } catch (OptionMissingArgumentException e) {
                if (options != null)
                {
                    options.InvalidOption(e.Option);
                }
                throw;
            } catch (OptionFormatException e) {
                if (options != null)
                {
                    options.InvalidOption(e.Option);
                }
                throw;
            } catch (OptionException) {
                if (options != null)
                {
                    options.Usage();
                }
                throw;
            }

            // Check that all mandatory options were provided
            StringBuilder sb         = new StringBuilder();
            List <string> optionList = new List <string>();

            foreach (OptionData optionData in m_OptionList)
            {
                if (optionData.Attribute.Required && !optionData.Set)
                {
                    MissingOption(parser,
                                  optionData.Attribute.ShortOption,
                                  optionData.Attribute.LongOption,
                                  sb, optionList);
                }
            }

            if (sb.Length > 0)
            {
                if (options != null)
                {
                    options.Missing(optionList);
                }
                throw new OptionMissingException(sb.ToString());
            }

            if (options != null)
            {
                options.Check();
            }
        }