Exemplo n.º 1
0
        private void ParseLongOption(int i)
        {
            string            exmsg;
            int               optID;
            string            optLong;
            Nullable <char>   optShort;
            ARGUMENT          hasArg;
            bool              isRepeated;
            Nullable <Option> optInput;
            OptionPrivate     optInternal;
            string            tmp;
            string            arg = null;
            int               id;

            tmp = ExtractOption(m_args [i]);

            if ((optInput = FindInOptionInput(tmp)) == null)
            {
                exmsg = "invalid option '--" + tmp + "'";
                throw new GetOptionsException(exmsg);
            }

            optID      = optInput.Value.optID;
            optShort   = optInput.Value.optShort;
            optLong    = optInput.Value.optLong;
            hasArg     = optInput.Value.hasArg;
            isRepeated = optInput.Value.isRepeated;

            if (hasArg == ARGUMENT.RequiredArgument || hasArg == ARGUMENT.OptionalArgument)
            {
                arg = ExtractOptionArgument(m_args[i], false);

                if ((arg == null) && (m_args.Length > (i + 1)))
                {
                    i++;
                    arg = m_args [i];
                }
                else
                {
                    if ((hasArg == ARGUMENT.RequiredArgument) && (arg == null))
                    {
                        exmsg = "option '" + m_args[i].Remove(m_args[i].IndexOf('='),
                                                              (m_args[i].Length - m_args[i].IndexOf('='))) + "' requires an argument";
                        throw new GetOptionsException(exmsg);
                    }
                }
            }

            optInternal = FindInOptionInternal(optLong);

            if (optInternal == null)
            {
                optInternal       = new OptionPrivate(optID, optShort, optLong, hasArg, isRepeated);
                optInternal.valid = true;

                if (hasArg == ARGUMENT.RequiredArgument || hasArg == ARGUMENT.OptionalArgument)
                {
                    if (arg != null)
                    {
                        optInternal.AddArgument(arg);
                    }
                }

                m_optsInternal.Add(optInternal);
            }
            else
            {
                if ((id = FindOptionInternalIndex(optLong)) != -1)
                {
                    if (id >= m_optsInternal.Count)
                    {
                        throw new GetOptionsException("Oops, internal error (index out of range)");
                    }

                    if (optInternal.isRebeated)
                    {
                        if (arg != null)
                        {
                            m_optsInternal [id].AddArgument(arg);
                        }
                        else
                        {
                            exmsg = "option '--" + optLong + "' cannot occur several times";
                            throw new GetOptionsException(exmsg);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        private void ParseShortOption(int i)
        {
            string            exmsg;
            int               optID;
            string            optLong;
            Nullable <char>   optShort;
            ARGUMENT          hasArg;
            bool              isRepeated;
            Nullable <Option> optInput;
            OptionPrivate     optInternal;
            string            tmp;
            List <string>     sopts;

            tmp   = ExtractOption(m_args [i]);
            sopts = CombinedShortOptionsToStringList(tmp);

            foreach (string s in sopts)
            {
                string arg = null;
                int    id;

                if ((optInput = FindInOptionInput(s.ToCharArray()[0])) == null)
                {
                    exmsg = "invalid option '-" + s.ToCharArray()[0] + "'";
                    throw new GetOptionsException(exmsg);
                }

                optID      = optInput.Value.optID;
                optShort   = optInput.Value.optShort;
                optLong    = optInput.Value.optLong;
                hasArg     = optInput.Value.hasArg;
                isRepeated = optInput.Value.isRepeated;

                if (hasArg == ARGUMENT.RequiredArgument || hasArg == ARGUMENT.OptionalArgument)
                {
                    arg = ExtractOptionArgument(s, true);

                    if ((arg == null) && (m_args.Length > (i + 1)))
                    {
                        i++;
                        arg = m_args [i];
                    }
                    else
                    {
                        if ((hasArg == ARGUMENT.RequiredArgument) && (arg == null))
                        {
                            exmsg = "option \"-" + s.ToCharArray()[0] + "\" requires an argument";
                            throw new GetOptionsException(exmsg);
                        }
                    }
                }

                optInternal = FindInOptionInternal(optShort);

                if (optInternal == null)
                {
                    optInternal       = new OptionPrivate(optID, optShort, optLong, hasArg, isRepeated);
                    optInternal.valid = true;

                    if (hasArg == ARGUMENT.RequiredArgument || hasArg == ARGUMENT.OptionalArgument)
                    {
                        if (arg != null)
                        {
                            optInternal.AddArgument(arg);
                        }
                    }

                    m_optsInternal.Add(optInternal);
                }
                else
                {
                    if ((id = FindOptionInternalIndex(optShort)) != -1)
                    {
                        if (id >= m_optsInternal.Count)
                        {
                            throw new GetOptionsException("Oops, internal error (index out of range)");
                        }

                        if (optInternal.isRebeated)
                        {
                            if (arg != null)
                            {
                                m_optsInternal [id].AddArgument(arg);
                            }
                            else
                            {
                                exmsg = "option '-" + optShort + "' cannot occur several times";
                                throw new GetOptionsException(exmsg);
                            }
                        }
                    }
                }
            }
        }