Exemplo n.º 1
0
        private static void printOptions(string passName, ArgumentCollection options, List <IMemberData> unnamed)
        {
            Console.WriteLine("Usage: {2} {0} {1}",
                              string.Join(" ", options.Values.Where(x => x.IsVisible).Select(x =>
            {
                var str = x.ShortName != '\0' ? string.Format("-{0}", x.ShortName) : "--" + x.LongName;

                if (x.NeedsArgument)
                {
                    str += " <arg>";
                }

                return('[' + str + ']');
            })),
                              string.Join(" ", unnamed.Select(x =>
            {
                var attr = x.GetAttribute <UnnamedCommandLineArgument>();
                var str  = attr.Name;

                if (attr.Required == false)
                {
                    str = "[<" + str + ">]";
                }
                else
                {
                    str = "<" + str + ">";
                }

                return(str);
            })), passName);

            Console.Write(options);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Transfers an option from one
 /// </summary>
 /// <param name="optionName"></param>
 /// <param name="from"></param>
 /// <returns></returns>
 public Argument TakeOption(string optionName, ArgumentCollection from)
 {
     if (from.Contains(optionName))
     {
         this[optionName] = from[optionName].Clone();
         return(this[optionName]);
     }
     return(null);
 }
Exemplo n.º 3
0
        public ArgumentCollection Parse(string[] rawArgs)
        {
            ArgumentCollection options  = AllOptions.CreateDefault();
            List <string>      restList = options.UnnamedArguments.ToList();

            for (int i = 0; i < rawArgs.Length; i++)
            {
                string           arg       = rawArgs[i];
                optionFindResult optResult = findOption(arg, options);
                Argument         opt       = optResult.FoundOption;
                if (opt == null)
                {
                    if (optResult.IsUnknown == false)
                    {
                        restList.Add(arg);
                    }
                    else
                    {
                        options.UnknownsOptions.Add(arg);
                    }
                    continue;
                }

                if (opt.NeedsArgument)
                {
                    if (optResult.InlineArg != null)
                    {
                        opt.Values.Add(optResult.InlineArg);
                    }
                    else if (i + 1 < rawArgs.Length)
                    {
                        opt.Values.Add(rawArgs[++i]);
                    }
                    else
                    {
                        options.MissingArguments.Add(opt);
                        continue;
                    }
                }
                else
                {
                    if (optResult.InlineArg != null)
                    {
                        // Add the value of the inline arg, even if NeedsArgument was not specified
                        opt.Values.Add(optResult.InlineArg);
                    }
                }
                options.Add(opt);
            }
            options.UnnamedArguments = restList.ToArray();
            return(options);
        }
Exemplo n.º 4
0
        optionFindResult findOption(string st, ArgumentCollection options)
        {
            optionFindResult opt = new optionFindResult {
                IsUnknown = false
            };
            int len = 0;

            if (st.StartsWith("--"))
            {
                len = 2;
            }
            else if (st.StartsWith("-")) // Removed check for starting with '\'. It breaks installing a pacakge from network drive because it starts with \\.
            {
                len = 1;
            }
            if (len == 0)
            {
                return(opt);
            }

            st = st.Substring(len);
            var idx = st.IndexOf('=');

            if (idx > 0)
            {
                opt.InlineArg = st.Substring(idx + 1);
                st            = st.Substring(0, idx);
            }
            opt.FoundOption = options
                              .Where(o => o.Value.CompareTo(st))
                              .FirstOrDefault().Value;

            if (opt.FoundOption == null)
            {
                opt.FoundOption = AllOptions
                                  .Where(o => o.Value.CompareTo(st))
                                  .FirstOrDefault().Value;
            }

            if (opt.FoundOption == null)
            {
                opt.IsUnknown = true;
            }
            else
            {
                opt.FoundOption = opt.FoundOption.Clone();
            }
            return(opt);
        }
Exemplo n.º 5
0
        public ArgumentCollection CreateDefault()
        {
            ArgumentCollection opt = new ArgumentCollection {
                UnnamedArguments = UnnamedArguments
            };

            foreach (var key in Keys)
            {
                if (this[key].Value != null)
                {
                    opt[key] = this[key];
                }
            }
            return(opt);
        }