Пример #1
0
        public static ICommand GetCommand(string commandName)
        {
            var commandAttributes = new CommandAttributes
                {
                    CommandName = commandName,
                    Description = "Yada yada " + commandName,
                    AlternativeCommandNames = new string[] { },
                    Examples = new Dictionary<string, string>
                        {
                            { commandName + " 1", "Some description for " + commandName + " 1" },
                            { commandName + " 2", "Some description for " + commandName + " 2" }
                        },
                    ArgumentDescriptions = new Dictionary<string, string>
                        {
                            { "arg1", "Some description for " + commandName + " arg1" },
                            { "arg2", "Some description for " + commandName + " arg2" },
                            { "arg3", "Some description for " + commandName + " arg3" }
                        }
                };

            var command = new Mock<ICommand>();
            command.Setup(c => c.Attributes).Returns(commandAttributes);

            return command.Object;
        }
Пример #2
0
 public static void ValidateCommandAttributes(CommandAttributes commandAttributes)
 {
     Assert.IsNotNull(commandAttributes, "The command attributes cannot be null.");
     Assert.IsNotNullOrEmpty(commandAttributes.CommandName, string.Format("The {0} cannot be null or empty.", "command name"));
     Assert.IsNotNullOrEmpty(commandAttributes.Description, string.Format("The {0} cannot be null or empty.", "command description"));
     Assert.IsNotNullOrEmpty(commandAttributes.Usage, string.Format("The {0} cannot be null or empty.", "command usage description"));
     Assert.IsTrue(commandAttributes.Examples.Count > 0, "The command attributes should contain at least one example");
 }
Пример #3
0
        /// <summary>
        /// Uses reflection on the executing assembly to find all implementations of IGettingReadyCommand and
        /// creates a Dictionary for looking up a command specified by integer id.
        /// </summary>
        private void LoadAvailableCommands()
        {
            Type[] typesInAssembly = Assembly.GetExecutingAssembly().GetTypes();

            foreach (Type type in typesInAssembly)
            {
                if (type.GetInterface("IGettingReadyCommand") != null)
                {
                    CommandAttributes attributes = type.GetCustomAttribute(typeof(CommandAttributes)) as CommandAttributes;
                    CommandLookup.Add(attributes.Id, attributes);
                }
            }
        }
Пример #4
0
        private CommandAttributes RetrieveCommandAttributes()
        {
            CommandAttributes commandAttributes = new CommandAttributes();

            object[] attrs = obj.GetType().GetCustomAttributes(typeof(ICommandAttribute), false);
            if (attrs.Length > 0)
            {
                for (int i = 0; i < attrs.Length; i++)
                {
                    object attr = attrs[i];
                    if (attr is CommandAttribute)
                    {
                        CommandAttribute commandAttr = (CommandAttribute)attr;
                        commandAttributes.CommandName      = commandAttr.CommandName;
                        commandAttributes.RequiresContext  = commandAttr.RequiresContext;
                        commandAttributes.ShortDescription = commandAttr.ShortDescription;
                    }
                    else if (attr is CommandAliasAttribute)
                    {
                        CommandAliasAttribute aliasAttribute = (CommandAliasAttribute)attr;
                        commandAttributes.Aliases.Add(aliasAttribute.Alias);
                    }
                    else if (attr is CommandSynopsisAttribute)
                    {
                        CommandSynopsisAttribute synopsisAttr = (CommandSynopsisAttribute)attr;
                        if (synopsisAttr.Text != null && synopsisAttr.Text.Length > 0)
                        {
                            commandAttributes.Synopsis.Add(synopsisAttr.Text);
                        }
                    }
                    else if (attr is CommandGroupAttribute)
                    {
                        CommandGroupAttribute groupAttribute = (CommandGroupAttribute)attr;
                        commandAttributes.CommandGroup = groupAttribute.GroupName;
                    }
                    else if (attr is CommandCompletionAttribute)
                    {
                        CommandCompletionAttribute completionAttribute = (CommandCompletionAttribute)attr;
                        attributes.CommandCompletion = completionAttribute.CompleteCommand;
                    }
                    else if (attr is CommandDesctiprionAttribute)
                    {
                        CommandDesctiprionAttribute desctiprionAttribute = (CommandDesctiprionAttribute)attr;
                        attributes.Description = ReadDescription(desctiprionAttribute.Value, desctiprionAttribute.Source);
                    }
                }
            }

            return(commandAttributes);
        }
Пример #5
0
 /// <summary>
 /// Add action to execution queue
 /// </summary>
 /// <typeparam name="TArgs1">Type of actions 1st parameter</typeparam>
 /// <typeparam name="TArgs2">Type of actions 2nd parameter</typeparam>
 /// <typeparam name="TArgs3">Type of actions 3rd parameter</typeparam>
 /// <param name="action">Action to queue</param>
 /// <param name="args1">1st parameter for action</param>
 /// <param name="args2">2nd parameter for action</param>
 /// <param name="args3">3rd parameter for action</param>
 /// <param name="attributes"></param>
 /// <returns>This chain</returns>
 public AsyncChain AddAction <TArgs1, TArgs2, TArgs3>(Action <TArgs1, TArgs2, TArgs3> action, TArgs1 args1, TArgs2 args2, TArgs3 args3, CommandAttributes attributes = CommandAttributes.None)
 {
     return(AddCommand(new AsyncCommandActionArgs3 <TArgs1, TArgs2, TArgs3>(action, args1, args2, args3, attributes)));
 }
Пример #6
0
 /// <summary>
 /// Add action to execution queue
 /// </summary>
 /// <typeparam name="TArgs">Type of actions parameter</typeparam>
 /// <param name="action">Action to queue</param>
 /// <param name="args">Parameter for action</param>
 /// <param name="attributes">Attributes for queued command</param>
 /// <returns>This chain</returns>
 public AsyncChain AddAction <TArgs>(Action <TArgs> action, TArgs args, CommandAttributes attributes = CommandAttributes.None)
 {
     return(AddCommand(new AsyncCommandActionArgs <TArgs>(action, args, attributes)));
 }
Пример #7
0
 /// <summary>
 /// Add action to execution queue
 /// </summary>
 /// <param name="action">Action to queue</param>
 /// <param name="attributes">Attributes for queued command</param>
 /// <returns>This chain</returns>
 public AsyncChain AddAction(Action action, CommandAttributes attributes = CommandAttributes.None)
 {
     return(AddCommand(new AsyncCommandAction(action, attributes)));
 }
Пример #8
0
        private CommandAttributes RetrieveCommandAttributes()
        {
            CommandAttributes commandAttributes = new CommandAttributes();

            object[] attrs = obj.GetType().GetCustomAttributes(typeof(ICommandAttribute), false);
            if (attrs.Length > 0) {
                for (int i = 0; i < attrs.Length; i++) {
                    object attr = attrs[i];
                    if (attr is CommandAttribute) {
                        CommandAttribute commandAttr = (CommandAttribute)attr;
                        commandAttributes.CommandName = commandAttr.CommandName;
                        commandAttributes.RequiresContext = commandAttr.RequiresContext;
                        commandAttributes.ShortDescription = commandAttr.ShortDescription;
                    } else if (attr is CommandAliasAttribute) {
                        CommandAliasAttribute aliasAttribute = (CommandAliasAttribute)attr;
                        commandAttributes.Aliases.Add(aliasAttribute.Alias);
                    } else if (attr is CommandSynopsisAttribute) {
                        CommandSynopsisAttribute synopsisAttr = (CommandSynopsisAttribute)attr;
                        if (synopsisAttr.Text != null && synopsisAttr.Text.Length > 0)
                            commandAttributes.Synopsis.Add(synopsisAttr.Text);
                    } else if (attr is CommandGroupAttribute) {
                        CommandGroupAttribute groupAttribute = (CommandGroupAttribute)attr;
                        commandAttributes.CommandGroup = groupAttribute.GroupName;
                    } else if (attr is CommandCompletionAttribute) {
                        CommandCompletionAttribute completionAttribute = (CommandCompletionAttribute)attr;
                        attributes.CommandCompletion = completionAttribute.CompleteCommand;
                    } else if (attr is CommandDesctiprionAttribute) {
                        CommandDesctiprionAttribute desctiprionAttribute = (CommandDesctiprionAttribute)attr;
                        attributes.Description = ReadDescription(desctiprionAttribute.Value, desctiprionAttribute.Source);
                    }
                }
            }

            return commandAttributes;
        }
Пример #9
0
 public AsyncCommandActionArgs3(Action <TArgs1, TArgs2, TArgs3> command, TArgs1 args1, TArgs2 args2, TArgs3 args3, CommandAttributes attributes)
 {
     _command        = command;
     _args1          = args1;
     _args2          = args2;
     _args3          = args3;
     this.attributes = attributes;
 }
 public AsyncCommandAction(Action command, CommandAttributes attributes)
 {
     _command        = command;
     this.attributes = attributes;
 }
 public AsyncCommandActionArgs(Action <TArgs> command, TArgs args, CommandAttributes attributes)
 {
     _command        = command;
     _args           = args;
     this.attributes = attributes;
 }
Пример #12
0
 public void Display(CommandAttributes commandAttributes)
 {
     foreach (ICommand command in CommandManager.Commands.Where(command => command.Supports(commandAttributes)))
     {
         command.Execute();
     }
 }