Example #1
0
        public static IList<Option> ToOptions(this string[] args)
        {
            var options = new List<Option>();

            var parser = new Parser
                             {
                                 OnShortOption = name => options.Add(new Option {ShortName = name}),
                                 OnLongOption = name => options.Add(new Option {LongName = name}),
                                 OnValue = value =>
                                               {
                                                   if (options.Count > 0)
                                                   {
                                                       var last = options[options.Count - 1];
                                                       last.AddValue(value);
                                                   }
                                                   else
                                                   {
                                                       var option = new Option();
                                                       option.AddValue(value);
                                                       options.Add(option);
                                                   }
                                               }
                             };
            parser.Parse(args);

            return options;
        }
Example #2
0
        public static IList <Option> ToOptions(this string[] args)
        {
            var options = new List <Option>();

            var parser = new Parser
            {
                OnShortOption = name => options.Add(new Option {
                    ShortName = name
                }),
                OnLongOption = name => options.Add(new Option {
                    LongName = name
                }),
                OnValue = value =>
                {
                    if (options.Count > 0)
                    {
                        var last = options[options.Count - 1];
                        last.AddValue(value);
                    }
                    else
                    {
                        var option = new Option();
                        option.AddValue(value);
                        options.Add(option);
                    }
                }
            };

            parser.Parse(args);

            return(options);
        }
Example #3
0
        // Throws an exception for unrecogized options, ambiguous options,
        // and malformed arguments. Note that either '=' or ':' can be used
        // for the argument separator.
        public void Parse(params string[] args)
        {
            DoSanityTest();

            m_operands.Clear();
            foreach (Option option in m_options.Values)
            {
                option.Reset();
            }

            bool processOptions = true;

            foreach (string arg in args)
            {
                if (arg == "--")
                {
                    processOptions = false;
                }
                else if (processOptions && arg.StartsWith("-"))
                {
                    string arg2 = arg;
                    if (arg.StartsWith("--"))                           // this seems kind of cheesy
                    {
                        arg2 = arg.Remove(0, 1);
                    }

                    string key   = arg2;                        // TODO: should support grouped short options, eg -lrwx
                    string value = null;                        // TODO: add an enum to control the option style: linux (-xml), windows (/xml), both, or gnu (--xml), or just use aliaii?

                    int i = arg2.IndexOfAny(new char[] { '=', ':' });
                    if (i >= 0)
                    {
                        key   = arg2.Substring(0, i);
                        value = arg2.Substring(i + 1);
                    }

                    Option option = DoFind(key);
                    option.Found = true;
                    option.AddValue(value);
                }
                else
                {
                    m_operands.Add(arg);
                }
            }
        }
Example #4
0
        public T Parse <T>(string[] args, T obj)
        {
            var    availableOptions = Options.Create(obj);
            var    setOptions       = new List <Option>();
            Option current          = null;

//            var options = new List<Option>();
//
//            var parser = new Parser
//                             {
//                                 OnShortOption = name => options.Add(new Option {ShortName = name}),
//                                 OnLongOption = name => options.Add(new Option {LongName = name}),
//                                 OnValue = value =>
//                                               {
//                                                   if (options.Count > 0)
//                                                   {
//                                                       var last = options[options.Count - 1];
//                                                       last.AddValue(value);
//                                                   }
//                                                   else
//                                                   {
//                                                       var option = new Option();
//                                                       option.AddValue(value);
//                                                       options.Add(option);
//                                                   }
//                                               }
//                             };
//            parser.Parse(args);

            for (var i = 0; i < args.Length; i++)
            {
                var arg = args[i];
                if (ShortOption.IsMatch(arg))
                {
                    var name = arg.Substring(1);

                    var option = (from o in setOptions
                                  where o.ShortName == name
                                  select o).FirstOrDefault();
                    if (option != null)
                    {
                        OnDuplicateOption(option);
                    }

                    option = (from o in availableOptions
                              where o.ShortName == name
                              select o).FirstOrDefault();
                    if (option == null)
                    {
                        OnInvalidOption(name);
                    }

                    // TODO: duplicate code
                    if (option != null && option.Type == typeof(bool))
                    {
                        option.Property.SetValue(obj, true, null);
                    }

                    current = option;
                    setOptions.Add(current);
                    availableOptions.Remove(current);
                }
                else if (LongOption.IsMatch(arg))
                {
                    var name = arg.Substring(2);

                    var option = (from o in setOptions
                                  where o.LongName == name
                                  select o).FirstOrDefault();
                    if (option != null)
                    {
                        OnDuplicateOption(option);
                    }

                    option = (from o in availableOptions
                              where o.LongName == name
                              select o).FirstOrDefault();
                    if (option == null)
                    {
                        OnInvalidOption(name);
                    }

                    // TODO: duplicate code
                    if (option != null && option.Type == typeof(bool))
                    {
                        option.Property.SetValue(obj, true, null);
                    }

                    current = option;
                    setOptions.Add(current);
                    availableOptions.Remove(current);
                }
                else
                {
                    if (current == null)
                    {
                        OnMissingOption(arg);
                    }
                    else
                    {
                        current.AddValue(arg);
                        current.Property.SetValue(obj, arg, null);
                        current = null;
                    }
                }
            }

            foreach (var option in availableOptions)
            {
                var value = option.Property.GetValue(obj, null);
                if (option.Required && (value == null || String.IsNullOrEmpty(value.ToString())))
                {
                    throw new RequirementException(option);
                }
            }

            foreach (var option in setOptions)
            {
                if (option.Required && String.IsNullOrEmpty(option.Value))
                {
                    throw new RequirementException(option);
                }
            }

            return(obj);
        }