// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 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); } }
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 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); } }
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; } } } } }
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); }
public MethodDefinition(MethodBase method, CmdLineMethodAttribute attribute, CmdLineConfig config) { Method = method; Attribute = attribute; Config = config; }
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); }
static public int Execute(string[] args, CmdLineConfig config = null) { return(Execute(args, null, config ?? new CmdLineConfig())); }
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= 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); } }