public object[] getArgs(MethodInfo info, CmdParam param, CmdAttribute attr) { var fparam = info.GetParameters(); object[] args = new object[fparam.Length]; if (attr == null) { Debug.Fail("Must apply a [MsgAttribute] to method"); return(args); } // 沒有参数 if (fparam.Length == 0) { return(args); } for (int i = 0; i < fparam.Length; i++) { var f = fparam[i]; var name = fparam[i].Name; // 单个参数 if (f.ParameterType == typeof(string)) { args[i] = param[name]; } // 多个参数 else if (f.ParameterType == typeof(List <string>)) { args[i] = param[name, true]; } else { System.Console.WriteLine(string.Format("Error parameter type : {0} {1}", f.Name, f.ParameterType)); } var mpa = f.GetCustomAttribute <CmdParamAttribute> (); // 设置了属性 if (mpa != null) { if (args[i] == null) { if (mpa.require) { throw new ArgumentException("Require parameter : {0}!".format(name)); } // 需要的参数 else if (mpa.defaultValue != null) { args[i] = mpa.defaultValue; } // 默认值 else if (mpa.isswitch) { args[i] = "true"; } // 开关 } } } return(args); }
public void DisplayCommandList() { WriteLine(0, "命令列表:", 0, ""); string[] keyList = new string[CommandsList.Keys.Count]; CommandsList.Keys.CopyTo(keyList, 0); foreach (string cmd in keyList) { ICommand command = CommandsList[cmd]; object[] attrs = command.GetType().GetCustomAttributes(typeof(CmdAttribute), false); if (attrs.Length == 0) { continue; } CmdAttribute ca = attrs[0] as CmdAttribute; WriteLine(4, ca.Cmd, 20, ca.Description); } Console.WriteLine(); }
/// <summary> /// The main method program.cs bootstrap method to execute command line arguments. /// </summary> public static int Run(string[] args, Type type, string rootDescription) { // Add Root Command var rootCommand = new RootCommand(rootDescription); // Load attributed methods from the specified type var cmds = CmdAttribute.GetCommands(type); foreach (var c in cmds) { rootCommand.AddCommand(c.GetCommand()); } // Invoke Command based on provided args return(rootCommand.InvokeAsync(args).Result); }
private void _Inject(System.Reflection.Assembly assembly) { //Type[] types = System.Reflection.Assembly.GetExecutingAssembly().GetTypes(); Type[] types = assembly.GetTypes(); foreach (Type t in types) { CmdAttribute attribute = Attribute.GetCustomAttribute(t, typeof(CmdAttribute)) as CmdAttribute; if (attribute != null) { ICommand command = t.Assembly.CreateInstance(t.FullName) as ICommand; if (command != null) { AddCommand(command); } } } }
public void DisplayDescription(string cmd) { if (string.IsNullOrEmpty(cmd)) { return; } cmd = cmd.ToLower(); if (CommandsList.ContainsKey(cmd) == false) { log.ErrorFormat("The command of '{0}' is not exists.", cmd); return; } ICommand command = CommandsList[cmd]; object[] attrs = command.GetType().GetCustomAttributes(typeof(CmdAttribute), false); if (attrs.Length > 0) { CmdAttribute ca = attrs[0] as CmdAttribute; if (!string.IsNullOrEmpty(ca.Description)) { WriteLine(0, ca.Cmd + ": ", ca.Cmd.Length + 2, ca.Description); } if (!string.IsNullOrEmpty(ca.Usage)) { WriteLine(0, "用法:", 0, ca.Usage + "\r\n"); } } attrs = command.GetType().GetCustomAttributes(typeof(CmdParamAttribute), false); WriteLine(4, "/?", 10, "显示帮助信息"); for (int i = 0; i < attrs.Length; i++) { CmdParamAttribute cpa = attrs[i] as CmdParamAttribute; WriteLine(4, cpa.Key, 10, cpa.Description); } Console.WriteLine(); }
internal static int Run(string[] args) { // Add Root Command var rootCommand = new RootCommand(ROOT_DESC); // Add Additional Commands rootCommand.AddCommand(GetEchoCommand()); rootCommand.AddCommand(GetCompressCommand()); //TODO: Add additional commands for license and Readme etc. in base class //TODO: General refactoring and commenting needed. var cmds = CmdAttribute.GetCommands(typeof(CommandItems)); foreach (var c in cmds) { rootCommand.AddCommand(c.GetCommand()); } // Invoke Command based on provided args return(rootCommand.InvokeAsync(args).Result); }
public CommandFilterViewModel() { void PopulateCollection <T>(ref List <string> collection, Func <string, string> selector = null, Func <string, bool> validation = null) where T : Enum { collection = new List <string>(); // default selection = no filter collection.Add(DESELECTION_DESCRIPTION); foreach (string enumVal in Enum.GetNames(typeof(T))) { if (!validation?.Invoke(enumVal) ?? false) { continue; } string selectorVal = selector?.Invoke(enumVal) ?? enumVal; if (collection.Contains(selectorVal)) { continue; } else { collection.Add(selectorVal); } } } int?SubscriptionBuilder(int?value, ref List <string> collection) => !value.HasValue ? value : value.Value == 0 || value.Value == -1 ? null : value; PopulateCollection <Transceiver>(ref _transceivers); PopulateCollection <Cmd>(ref _cmdSet, cmd => CmdAttribute.TryGetAttribute(Enum.Parse <Cmd>(cmd)).CmdSetDescription); PopulateCollection <Comms>(ref _comms); PopulateCollection <Ack>(ref _acks); this.WhenAnyValue(instance => instance.SrcModuleSelectionIndex).Subscribe(idx => SrcModuleSelectionIndex = SubscriptionBuilder(idx, ref _transceivers)); this.WhenAnyValue(instance => instance.DestModuleSelectionIndex).Subscribe(idx => DestModuleSelectionIndex = SubscriptionBuilder(idx, ref _transceivers)); this.WhenAnyValue(instance => instance.CmdSetSelectionIndex).Subscribe(idx => CmdSetSelectionIndex = SubscriptionBuilder(idx, ref _cmdSet)); this.WhenAnyValue(instance => instance.CmdSelectionIndex).Subscribe(idx => CmdSelectionIndex = SubscriptionBuilder(idx, ref _cmd)); this.WhenAnyValue(instance => instance.CommsSelectionIndex).Subscribe(idx => CommsSelectionIndex = SubscriptionBuilder(idx, ref _comms)); this.WhenAnyValue(instance => instance.AcksSelectionIndex).Subscribe(idx => AcksSelectionIndex = SubscriptionBuilder(idx, ref _acks)); this.WhenAnyValue(instance => instance.SrcModuleSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); this.WhenAnyValue(instance => instance.DestModuleSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); this.WhenAnyValue(instance => instance.CmdSetSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); this.WhenAnyValue(instance => instance.CmdSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); this.WhenAnyValue(instance => instance.CommsSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); this.WhenAnyValue(instance => instance.AcksSelectionIndex).Subscribe(idx => DjiNetworkPacketPool?.EvaluateFilterOnPackets()); // restrict the cmd based on the cmdSet this.WhenAnyValue(instance => instance.CmdSetSelectionIndex).Subscribe(idx => { if (SubscriptionBuilder(idx, ref _cmdSet) == null) { return; } CmdAttribute GetAttributeByName(string name) => CmdAttribute.TryGetAttribute(Enum.Parse <Cmd>(name)); PopulateCollection <Cmd>(ref _cmd, cmd => GetAttributeByName(cmd).CmdDescription, cmd => GetAttributeByName(cmd).CmdSetDescription == _cmdSet[idx.Value]); this.RaisePropertyChanged(nameof(Cmd)); }); }
public object[] getArgs(MethodInfo info, CmdParam param, CmdAttribute attr) { var fparam = info.GetParameters(); object[] args = new object[fparam.Length]; if (attr == null) { Debug.Fail("Must apply a [MsgAttribute] to method"); return(args); } // 沒有参数 if (fparam.Length == 0) { return(args); } // 多个参数 for (int i = 0; i < fparam.Length; i++) { var f = fparam[i]; var l = f.Name.ToLower(); // 长名字 var s = !attr.disableShort ? l[0].ToString() : null; // 短名字 var d = attr.collectDefault == null && s != null ? i : -1; // 单个参数 if (f.ParameterType == typeof(string)) { args[i] = param[l, s, d]; } // 多个参数 else if (f.ParameterType == typeof(List <string>)) { args[i] = param[l, s, d, true]; } else { System.Console.WriteLine(string.Format("Error parameter type : {0} {1}", f.Name, f.ParameterType)); } // 收集默认参数 if (attr.collectDefault == f.Name.ToLower()) { if (f.ParameterType == typeof(List <string>)) { var all = param.getAllDefault(); if (args[i] != null) { all.AddRange((List <string>)args[i]); } args[i] = all; } else { System.Console.WriteLine(string.Format("Parameter type must be List<string>")); } } var mpa = f.GetCustomAttribute <CmdParamAttribute> (); // 设置了属性 if (mpa != null) { if (args[i] == null) { if (mpa.require) { throw new ArgumentException("Require parameter : {0}!".format(l)); } // 需要的参数 else if (mpa.defaultValue != null) { args[i] = mpa.defaultValue; } // 默认值 else if (mpa.isswitch) { args[i] = "true"; } // 开关 } } } return(args); }