Esempio n. 1
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);
 }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }