Beispiel #1
0
        /// \brief Append the argument onto the given array as strings.
        public void Render(ArgumentList args,
                           List <string> output)
        {
            var renderStyleKind = GetOption().GetRenderStyle();

            if (renderStyleKind == RenderStyleKind.RenderValuesStyle)
            {
                output.AddRange(_values);
                return;
            }

            if (renderStyleKind == RenderStyleKind.RenderCommaJoinedStyle)
            {
                var os = new StringBuilder(256);
                os.Append(GetSpelling());
                for (int i = 0, e = GetNumValues(); i != e; ++i)
                {
                    if (i != 0)
                    {
                        os.Append(',');
                    }

                    os.Append(GetValue(i));
                }

                output.Add(args.MakeArgString(os.ToString()));
                return;
            }

            if (renderStyleKind == RenderStyleKind.RenderJoinedStyle)
            {
                output.Add(args.GetOrMakeJoinedArgString(GetIndex(),
                                                         GetSpelling(),
                                                         GetValue()));
                output.AddRange(_values.Skip(1));
                return;
            }

            if (renderStyleKind == RenderStyleKind.RenderSeparateStyle)
            {
                output.Add(args.MakeArgString(GetSpelling()));
                output.AddRange(_values);
            }
        }
Beispiel #2
0
        /// accept - Potentially accept the current argument, returning a
        /// new Argument instance, or 0 if the option does not accept this
        /// argument (or the argument is missing values).
        ///
        /// If the option accepts the current argument, accept() sets
        /// Index to the position where argument parsing should resume
        /// (even if the argument is missing values).
        ///
        /// \param ArgSize The number of bytes taken up by the matched Option prefix
        /// and name. This is used to determine where joined values
        /// start.
        public Argument Accept(ArgumentList args,
                               ref int index,
                               int argSize)
        {
            var    unaliasedOption = GetUnaliasedOption();
            string spelling;

            // If the option was an alias, get the spelling from the unaliased one.
            if (GetId() == unaliasedOption.GetId())
            {
                spelling = args.GetArgString(index).Substring(0,
                                                              argSize);
            }
            else
            {
                spelling = args.MakeArgString(unaliasedOption.GetPrefix() + unaliasedOption.GetName());
            }

            var kind = GetKind();

            if (kind == OptionKind.FlagClass)
            {
                if (argSize != args.GetArgString(index).Length)
                {
                    return(null);
                }

                var a = new Argument(unaliasedOption,
                                     spelling,
                                     index++);

                if (!string.IsNullOrEmpty(GetAliasArgs()))
                {
                    var vals = GetAliasArgs().Split(new[] { " " },
                                                    StringSplitOptions.RemoveEmptyEntries);

                    a.GetValues().AddRange(vals.Select(item => item));

                    //while (*Val != '\0')
                    //{
                    //    A.getValues().AddRange(Val);

                    //    // Move past the '\0' to the next argument.
                    //    Val += Val.Length + 1;
                    //}
                }

                if (unaliasedOption.GetKind() == OptionKind.JoinedClass &&
                    string.IsNullOrEmpty(GetAliasArgs()))
                {
                    a.GetValues().Add(string.Empty);
                }

                return(a);
            }
            else if (kind == OptionKind.JoinedClass)
            {
                string value = args.GetArgString(index).Substring(argSize);
                return(new Argument(unaliasedOption,
                                    spelling,
                                    index++,
                                    value));
            }
            else if (kind == OptionKind.CommaJoinedClass)
            {
                // Always matches.
                var str = args.GetArgString(index).Substring(argSize).
                          Split(new[] { ',' },
                                StringSplitOptions.RemoveEmptyEntries);      // + ArgSize;

                var a = new Argument(unaliasedOption,
                                     spelling,
                                     index++);

                a.GetValues().AddRange(str.Select(item => item));

                a.SetOwnsValues(true);

                return(a);
            }
            else if (kind == OptionKind.SeparateClass)
            {
                // Matches iff this is an exact match.
                // FIXME: Avoid strlen.
                if (argSize != args.GetArgString(index).Length)
                {
                    return(null);
                }

                index += 2;
                if (index > args.GetNumInputArgStrings() ||
                    args.GetArgString(index - 1) == null)
                {
                    return(null);
                }

                return(new Argument(unaliasedOption,
                                    spelling,
                                    index - 2,
                                    args.GetArgString(index - 1)));
            }
            else if (kind == OptionKind.MultiArgClass)
            {
                // Matches iff this is an exact match.
                // FIXME: Avoid strlen.
                if (argSize != args.GetArgString(index).Length)
                {
                    return(null);
                }

                index += 1 + GetNumArgs();
                if (index > args.GetNumInputArgStrings())
                {
                    return(null);
                }

                var a = new Argument(unaliasedOption,
                                     spelling,
                                     index - 1 - GetNumArgs(),
                                     args.GetArgString(index - GetNumArgs()));
                for (var i = 1; i != GetNumArgs(); ++i)
                {
                    a.GetValues().Add(args.GetArgString(index - GetNumArgs() + i));
                }

                return(a);
            }
            else if (kind == OptionKind.JoinedOrSeparateClass)
            {
                // If this is not an exact match, it is a joined arg.
                // FIXME: Avoid strlen.
                if (argSize != args.GetArgString(index).Length)
                {
                    var value = args.GetArgString(index).Substring(argSize);
                    return(new Argument(this,
                                        spelling,
                                        index++,
                                        value));
                }

                // Otherwise it must be separate.
                index += 2;
                if (index > args.GetNumInputArgStrings() ||
                    args.GetArgString(index - 1) == null)
                {
                    return(null);
                }

                return(new Argument(unaliasedOption,
                                    spelling,
                                    index - 2,
                                    args.GetArgString(index - 1)));
            }
            else if (kind == OptionKind.JoinedAndSeparateClass)
            {
                // Always matches.
                index += 2;
                if (index > args.GetNumInputArgStrings() ||
                    args.GetArgString(index - 1) == null)
                {
                    return(null);
                }

                return(new Argument(unaliasedOption,
                                    spelling,
                                    index - 2,
                                    args.GetArgString(index - 2).Substring(argSize),
                                    args.GetArgString(index - 1)));
            }
            else if (kind == OptionKind.RemainingArgsClass)
            {
                // Matches iff this is an exact match.
                // FIXME: Avoid strlen.
                if (argSize != args.GetArgString(index).Length)
                {
                    return(null);
                }

                var a = new Argument(unaliasedOption,
                                     spelling,
                                     index++);
                while (index < args.GetNumInputArgStrings() &&
                       args.GetArgString(index) != null)
                {
                    a.GetValues().Add(args.GetArgString(index++));
                }

                return(a);
            }
            else if (kind == OptionKind.RemainingArgsJoinedClass)
            {
                var a = new Argument(unaliasedOption,
                                     spelling,
                                     index);
                if (argSize != args.GetArgString(index).Length)
                {
                    a.GetValues().Add(args.GetArgString(index).Substring(argSize));
                }

                index++;
                while (index < args.GetNumInputArgStrings() &&
                       args.GetArgString(index) != null)
                {
                    a.GetValues().Add(args.GetArgString(index++));
                }

                return(a);
            }

            throw new Exception("Invalid option kind!");
        }