/// <summary>
        /// Create configuration object from command line arguments
        /// and type with fields decorated Energy.Attribute.Command.Option attributes
        /// using specified shell option style.
        /// </summary>
        /// <param name="args">Command line options</param>
        /// <param name="type">Configuration class type</param>
        /// <param name="style">Command line option style settings</param>
        /// <returns>Configuration object</returns>
        public static object Create(string[] args, System.Type type, Energy.Core.Shell.OptionStyle style)
        {
            System.Collections.Generic.List <string> optList = new System.Collections.Generic.List <string>();
            System.Collections.Generic.List <string> argList = new System.Collections.Generic.List <string>();

            foreach (string arg in args)
            {
                if (style.IsOption(arg))
                {
                    optList.Add(arg);
                }
                else
                {
                    argList.Add(arg);
                }
            }

            System.Collections.Generic.Dictionary <string, List <string> > optionDictionary
                = new System.Collections.Generic.Dictionary <string, List <string> >();
            System.Collections.Generic.Dictionary <string, Energy.Attribute.Command.OptionAttribute> attributeDictionary
                = new System.Collections.Generic.Dictionary <string, Energy.Attribute.Command.OptionAttribute>();

            string[] a = Energy.Base.Class.GetFieldsAndProperties(type, true);
            foreach (string f in a)
            {
                Energy.Attribute.Command.OptionAttribute[] optionAttributes = (Energy.Attribute.Command.OptionAttribute[])
                                                                              Energy.Base.Class.GetFieldOrPropertyAttributes <Energy.Attribute.Command.OptionAttribute>(type, f, true, false);
                if (optionAttributes != null && optionAttributes.Length > 0)
                {
                    foreach (Energy.Attribute.Command.OptionAttribute optionAttribute in optionAttributes)
                    {
                        System.Collections.Generic.List <string> synonym = new System.Collections.Generic.List <string>();
                        synonym.Add(optionAttribute.Name);
                        synonym.Add(optionAttribute.Short);
                        synonym.Add(optionAttribute.Long);
                        if (optionAttribute.Alternatives != null)
                        {
                            synonym.AddRange(optionAttribute.Alternatives);
                        }
                        foreach (string name in synonym)
                        {
                            if (string.IsNullOrEmpty(name))
                            {
                                continue;
                            }
                            if (!optionDictionary.ContainsKey(name))
                            {
                                optionDictionary[name] = new List <string>();
                            }
                            optionDictionary[name].Add(f);
                        }
                    }
                }
                Energy.Attribute.Command.OptionAttribute[] parameterAttributes = (Energy.Attribute.Command.OptionAttribute[])
                                                                                 Energy.Base.Class.GetFieldOrPropertyAttributes(type, f, typeof(Energy.Attribute.Command.OptionAttribute), true, false);
            }
            return(null);
        }
Beispiel #2
0
            /// <summary>
            /// Create argument list from string array with specified command options style.
            /// </summary>
            /// <param name="args"></param>
            /// <param name="style"></param>
            /// <returns></returns>
            public static ArgumentList Create(string[] args, Energy.Core.Shell.OptionStyle style)
            {
                Energy.Base.Anonymous.Function <string, string> testDelA = delegate(string s) { return(s); };
                Energy.Base.Anonymous.Action <string>           testDelB = delegate(string s) { Console.WriteLine(s); };

                ArgumentList  list = new Core.Shell.ArgumentList();
                List <string> o    = new List <string>();

                for (int i = 0; i < args.Length; i++)
                {
                    string s = args[i];
                    if (false)
                    {
                    }
                    // Double dash
                    else if (style.Double && s.StartsWith("--"))
                    {
                        if (s.Length == 2)
                        {
                            list.Arguments.Add(s);
                            continue;
                        }
                        s = s.Substring(2);
                    }
                    // Slash
                    else if (style.Slash && s.StartsWith("/"))
                    {
                        if (s.Length == 1)
                        {
                            list.Arguments.Add(s);
                            continue;
                        }
                        s = s.Substring(1);
                        if (style.Multiple && Energy.Base.Text.Contains(s, "/"))
                        {
                            string[] a = s.Split('/');
                            o.AddRange(a);
                        }
                        else
                        {
                            o.Add(s);
                        }
                    }
                    // Single dash
                    else if (style.Single && s.StartsWith("-"))
                    {
                        if (s.Length == 1)
                        {
                            list.Arguments.Add(s);
                            continue;
                        }
                        s = s.Substring(1);
                        if (s.Length == 1)
                        {
                            o.Add(s);
                        }
                        else if (!style.Multiple)
                        {
                            o.Add(s);
                        }
                        else if (style.Short)
                        {
                            foreach (char c in s.ToCharArray())
                            {
                                if (c == '-')
                                {
                                    continue;
                                }
                                o.Add(c.ToString());
                            }
                        }
                        else
                        {
                            string[] a = s.Split('-');
                            for (int n = 0; n < a.Length; n++)
                            {
                                o.Add(a[n]);
                            }
                        }
                    }
                    else
                    {
                        list.Arguments.Add(s);
                    }
                    for (int n = 0; n < o.Count; n++)
                    {
                        s = o[n];
                        if (false)
                        {
                        }
                        else if (style.Colon && Energy.Base.Text.Contains(s, ":"))
                        {
                            int    x = s.IndexOf(":");
                            string v = s.Substring(x + 1);
                            s = s.Substring(0, x - 1);
                            list.Values[s] = v;
                        }
                        else if (style.Equal && Energy.Base.Text.Contains(s, "="))
                        {
                            int    x = s.IndexOf("=");
                            string v = s.Substring(x + 1);
                            s = s.Substring(0, x - 1);
                            list.Values[s] = v;
                        }
                        list.Options.Add(s);
                    }
                    if (o.Count > 0)
                    {
                        o.Clear();
                    }
                }
                return(list);
            }