예제 #1
0
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        public CmdLineParameter(PropertyInfo info, CmdLineArgAttribute attribute, CmdLineConfig config)
        {
            Field    = null;
            Property = info;
            Config   = config ?? throw new ArgumentNullException(nameof(config));
            Name     = attribute != null && attribute.ArgName != null
                ? attribute.ArgName
                : info.Name;
            Type      = info.PropertyType;
            Attribute = attribute;
            if (attribute != null && attribute.Default != null)
            {
                HasDefaultValue = true;
                DefaultValue    = attribute.Default;
            }
            else if (info.GetGetMethod().IsStatic)
            {
                DefaultValue    = info.GetValue(null, null);
                HasDefaultValue = DefaultValue != null;
            }
            else
            {
                // pi.GetValue() for non-static requires object
                HasDefaultValue = false;
                DefaultValue    = null;
            }

            if (HasDefaultValue && DefaultValue != null && !DefaultValue.GetType().IsEquivalentTo(Type))
            {
                throw new CmdLineException("CmdLine error: invalid default value type for parameter " + Name);
            }
        }
예제 #2
0
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        public CmdLineParameter(ParameterInfo info, CmdLineArgAttribute attribute, CmdLineConfig config)
        {
            Field    = null;
            Property = null;
            Config   = config ?? throw new ArgumentNullException(nameof(config));
            Name     = attribute != null && attribute.ArgName != null
                ? attribute.ArgName
                : info.Name;
            Type      = info.ParameterType;
            Attribute = attribute;
            if (info.HasDefaultValue)
            {
                HasDefaultValue = info.HasDefaultValue;
                DefaultValue    = info.DefaultValue;
            }
            else if (attribute != null && attribute.Default != null)
            {
                HasDefaultValue = true;
                DefaultValue    = attribute.Default;
            }
            else
            {
                HasDefaultValue = false;
                DefaultValue    = null;
            }
            if (HasDefaultValue && DefaultValue != null && !DefaultValue.GetType().IsEquivalentTo(Type))
            {
                throw new CmdLineException("CmdLine error: invalid default value type for parameter " + Name);
            }
        }
예제 #3
0
        public ClassDefinition(Type type, CmdLineConfig config)
        {
            Config    = config;
            Type      = type;
            Attribute = Type.GetCustomAttributes(typeof(CmdLineClassAttribute), false)
                        .FirstOrDefault() as CmdLineClassAttribute;
            InclusionBehavior = Attribute != null
                ? Attribute.InclusionBehavior
                : InclusionBehavior.Default;

            var ąssembly = Assembly.GetEntryAssembly();

            if (ąssembly != null)
            {
                ExeName = System.IO.Path.GetFileName(ąssembly.Location);
                Version = ąssembly.GetName().Version;
                var attrs = ąssembly.GetCustomAttributes(true);
                AssemblyDescriptionAttribute vd = null;
                AssemblyTitleAttribute       vt = null;
                AssemblyCopyrightAttribute   vc = null;
                foreach (var attr in attrs)
                {
                    if (vd == null)
                    {
                        vd = attr as AssemblyDescriptionAttribute;
                        if (vd != null)
                        {
                            Description = vd.Description;
                            continue;
                        }
                    }
                    if (vt == null)
                    {
                        vt = attr as AssemblyTitleAttribute;
                        if (vt != null)
                        {
                            AppTitle = vt.Title;
                            continue;
                        }
                    }
                    if (vc == null)
                    {
                        vc = attr as AssemblyCopyrightAttribute;
                        if (vc != null)
                        {
                            Copyright = vc.Copyright;
                            continue;
                        }
                    }
                }
            }
        }
예제 #4
0
        public CmdLineParser(string[] args, CmdLineConfig config)
        {
            Config        = config ?? throw new ArgumentNullException(nameof(config));
            ArgStartsWith = Config.ArgStartsWith.ToString();
            ArgSeparator  = Config.ArgSeparator;
            string command = null;
            int    c       = 0;

            if (args.Length > 0)
            {
                IsHelpCommand = args[0] == $"{config.ArgStartsWith}?" ||
                                args[0].Equals("help", StringComparison.CurrentCultureIgnoreCase);
                if (IsHelpCommand)
                {
                    if (args.Length > 1)
                    {
                        Command = args[1];
                    }
                    return;
                }
            }
            var parsedArgs = args.Select(s => ParseArg(s, config.RequiresCommand && c++ == 0)).ToList();

            if (parsedArgs.Count > 0)
            {
                if (config.RequiresCommand)
                {
                    var first = parsedArgs.First();
                    if (first.Key != null)
                    {
                        command = null;
                    }
                    else
                    {
                        command = first.Value;
                        parsedArgs.RemoveAt(0);
                    }
                }
            }

            Command = command;
            Args    = parsedArgs.ToDictionary(p => p.Key, p => p.Value);
        }
예제 #5
0
 public MethodDefinition(MethodBase method, CmdLineMethodAttribute attribute, CmdLineConfig config)
 {
     Method    = method;
     Attribute = attribute;
     Config    = config;
 }
예제 #6
0
        static public int Execute(string[] args, T instance, CmdLineConfig config = null)
        {
            if (config == null)
            {
                config = new CmdLineConfig();
            }
            Instance = instance;
            CmdLineParser parser;
            var           cd = new ClassDefinition(typeof(T), config);

            try
            {
                parser = new CmdLineParser(args, config);
            }
            catch (CmdLineArgException ex)
            {
                con.WriteAppHeader(cd);
                con.wl(ConsoleColor.Red, "{0}", ex.Message);
                con.WriteHelp(cd);
                return(-1);
            }

            if (parser.IsHelpCommand)
            {
                con.WriteAppHeader(cd);
                if (parser.IsHelpCommand && parser.Command != null)
                {
                    con.WriteHelp(cd, parser.Command);
                }
                else
                {
                    con.WriteHelp(cd);
                }
                return(0);
            }

            var executor = new CmdLineExecutor(cd, new InstanceProvider(Instance));

            if (config.RequiresCommand)
            {
                if (parser.Command != null)
                {
                    CmdLineException exception = null;
                    try
                    {
                        if (executor.Execute(parser.Command, parser.Args, out object result))
                        {
                            return(result != null && result.GetType().IsAssignableFrom(typeof(int))
                                ? (int)result
                                : 0);
                        }
                    }
                    catch (CmdLineException ex)
                    {
                        exception = ex;
                    }

                    con.WriteAppHeader(cd);
                    if (exception != null)
                    {
                        con.wl(ConsoleColor.Red, "{0}", exception.Message);
                        con.WriteHelp(cd, parser.Command);
                    }
                    else
                    {
                        con.w("Command ").w(ConsoleColor.Red, "{0}", parser.Command).wl(" not found");
                        con.WriteHelp(cd);
                    }
                    return(-1);
                    // help on command.  parser.Args contain unknown parameters
                }
                else
                {
                    // general help
                }
            }
            else if (parser.Command == null)
            {
                if (Execute(null, cd, parser.Args, config, out object result))
                {
                    return(result != null && result.GetType().IsAssignableFrom(typeof(int))
                        ? (int)result
                        : 0);
                }
                else
                {
                    // help on command.  parser.Args contain unknown parameters
                }
            }
            else
            {
                // unknown parameter, no command expected
            }

            con.WriteAppHeader(cd);
            if (parser.IsHelpCommand && parser.Command != null)
            {
                con.WriteHelp(cd, parser.Command);
            }
            else
            {
                con.WriteHelp(cd);
            }
            return(0);
        }
예제 #7
0
 static public int Execute(string[] args, CmdLineConfig config = null)
 {
     return(Execute(args, null, config ?? new CmdLineConfig()));
 }
예제 #8
0
        // -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

        static bool Execute(string command, ClassDefinition cd, IReadOnlyDictionary <string, string> inputArgs, CmdLineConfig config, out object result)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }
            var cl = new CmdLineExecutor(cd, new InstanceProvider(Instance));

            try
            {
                return(cl.Execute(command, inputArgs, out result));
            }
            catch (CmdLineArgException ex)
            {
                con.wl(ConsoleColor.Red, "{0}", ex.Message);
                result = null;
                return(false);
            }
        }