コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="CommandEntry"></param>
        private void BindMethod(CommandEntry CommandEntry)
        {
            var Method = CommandEntry.MethodInfo;

            Getopt.AddRule(CommandEntry.Aliases, () =>
            {
                var Parameters     = Method.GetParameters();
                var ParametersData = new List <object>();

                foreach (var Parameter in Parameters)
                {
                    if (Getopt.HasMore)
                    {
                        var ParameterData = Getopt.DequeueNext();
                        if (Parameter.ParameterType == typeof(string))
                        {
                            ParametersData.Add((string)ParameterData);
                        }
                        else
                        {
                            throw (new NotImplementedException("Not supported parameter type " + Parameter.ParameterType));
                        }
                    }
                    else
                    {
                        ParametersData.Add(null);
                    }
                }

                Method.Invoke(this, ParametersData.ToArray());
            });
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="CommandEntry"></param>
        private void BindField(CommandEntry CommandEntry)
        {
            var Field = CommandEntry.FieldInfo;

            Getopt.AddRule(CommandEntry.Aliases, () =>
            {
                if (Field.FieldType == typeof(bool))
                {
                    Field.SetValue(this, true);
                }
                else if (Field.FieldType == typeof(int))
                {
                    int Value = int.Parse(Getopt.DequeueNext());
                    if (!CommandEntry.Values.Contains(Value))
                    {
                        throw (new Exception(String.Format("Expecting one of [{0}] but found {1}", String.Join(", ", CommandEntry.Values), Value)));
                    }
                    Field.SetValue(this, Value);
                }
                else
                {
                    throw (new NotImplementedException("Not supported field type " + Field.FieldType));
                }
            });
        }
コード例 #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="SwitchDefaultAttribute"></param>
 /// <param name="Method"></param>
 private void BindDefaultMethod(CommandDefaultAttribute SwitchDefaultAttribute, MethodInfo Method)
 {
     Getopt.AddDefaultRule(() =>
     {
         Method.Invoke(this, new object[] { });
     });
 }
コード例 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="args"></param>
        public void Run(string[] args)
        {
            Getopt = new Getopt(args);

            CommandEntries = new List <CommandEntry>();

            foreach (var Member in this.GetType().GetMembers(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var DescriptionAttribute = GetSingleAttribute <DescriptionAttribute>(Member);
                var ValuesAttribute      = GetSingleAttribute <ValuesAttribute>(Member);

                var CommandDefaultAttribute = GetSingleAttribute <CommandDefaultAttribute>(Member);
                if (CommandDefaultAttribute != null)
                {
                    if (Member is MethodInfo)
                    {
                        BindDefaultMethod(CommandDefaultAttribute, Member as MethodInfo);
                    }
                }

                var CommandAttribute = GetSingleAttribute <CommandAttribute>(Member);
                if (CommandAttribute != null)
                {
                    var CommandEntry = new CommandEntry()
                    {
                        Aliases     = CommandAttribute.Aliases,
                        MemberInfo  = Member,
                        Examples    = Member.GetCustomAttributes(typeof(ExampleAttribute), true).Cast <ExampleAttribute>().Select(Item => Item.Example).ToArray(),
                        Description = (DescriptionAttribute != null) ? DescriptionAttribute.Description : "",
                        Values      = (ValuesAttribute != null) ? ValuesAttribute.Values : new object[0],
                    };

                    CommandEntries.Add(CommandEntry);

                    if (Member is MethodInfo)
                    {
                        BindMethod(CommandEntry);
                    }
                    else if (Member is FieldInfo)
                    {
                        BindField(CommandEntry);
                    }
                    else
                    {
                        throw (new NotImplementedException("Don't know how to handle type " + Member.GetType()));
                    }
                }
            }

            try
            {
                Getopt.Process();
            }
            catch (TargetInvocationException TargetInvocationException)
            {
                Console.Error.WriteLine(TargetInvocationException.InnerException);
                Environment.Exit(-1);
            }
            catch (Exception Exception)
            {
                //Console.Error.WriteLine(Exception.Message);
                Console.Error.WriteLine(Exception);
                Environment.Exit(-2);
            }

            if (Debugger.IsAttached)
            {
                Console.ReadKey();
            }
        }