/// <summary>
        /// Registers the given information to make the command available for later execution.
        /// </summary>
        /// <typeparam name="TValue">The type of value expected to be handled by the execution action.</typeparam>
        /// <param name="name">The name of the command to be registered.</param>
        /// <param name="executionAction">The action to be executed when the command is triggered.</param>
        /// <exception cref="ArgumentException">Thrown when the given command name is not a valid string.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the given execution action is null.</exception>
        /// <remarks>
        /// <para>
        /// If a command registration already exists under the given name, that registration will be overwritten
        /// by the given information.
        /// </para>
        /// </remarks>
        public void Register <TValue>(string name, Action <TValue> executionAction)
        {
            VerificationProvider.VerifyValidString(name, "name");
            VerificationProvider.VerifyNotNull(executionAction, "executionAction");

            mCommandRegistrations[name] = new ExecutableCommandRegistration <TValue>(name, executionAction, mObjectConverterFactory);
        }
        private void HandleRegistration(string[] registrationParameters)
        {
            if (registrationParameters.Length != 2)
            {
                ConsoleHelper.WriteLine(ConsoleMessageType.Error,
                                        "Invalid number of values specified for command registration. Please specify the name and program path.");

                return;
            }

            // Extract the registration name and program path from the registration parameters
            string registrationName = registrationParameters[0];
            string programPath      = registrationParameters[1];

            VerificationProvider.VerifyValidString(registrationName, "registrationName");
            VerificationProvider.VerifyValidString(programPath, "programPath");

            // Check we haven't already made a registration with this name
            if (ApplicationContext.ProgramRegistrations.ContainsKey(registrationName))
            {
                ConsoleHelper.WriteLine(ConsoleMessageType.Warning,
                                        "A program has already been registered under the name '{0}' and will be overridden.",
                                        registrationName);
            }

            // Store the registration, overriding any existing registrations
            ApplicationContext.ProgramRegistrations[registrationName] = programPath;

            ConsoleHelper.WriteLine(ConsoleMessageType.Information,
                                    "A registration has been successfully made under '{0}' for the path '{1}'.",
                                    registrationName, programPath);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RunCommandApplicationContext"/> class.
        /// </summary>
        /// <param name="filePath">The path to which the context information should be written to and read from.</param>
        /// <exception cref="ArgumentException">Thrown when the given file path is not a valid string.</exception>
        public RunCommandApplicationContext(string filePath)
        {
            VerificationProvider.VerifyValidString(filePath, "filePath");

            mFilePath = filePath;

            // Make sure the directory exists
            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ExecutableCommandRegistration{TValue}"/> class.
        /// </summary>
        /// <typeparam name="TValue">The type of value expected to be handled by the execution action.</typeparam>
        /// <param name="name">The name of the command to be registered.</param>
        /// <param name="executionAction">The action to be invoked when the command is triggered.</param>
        /// <param name="objectConverterFactory">A factory used to create requested object converters.</param>
        /// <exception cref="ArgumentException">Thrown when the given name is an invalid string.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the given or execution action object converter factory is null.</exception>
        internal ExecutableCommandRegistration(string name, Action <TValue> executionAction, ObjectConverterFactory objectConverterFactory)
        {
            VerificationProvider.VerifyValidString(name, "name");
            VerificationProvider.VerifyNotNull(executionAction, "executionAction");
            VerificationProvider.VerifyNotNull(objectConverterFactory, "objectConverterFactory");

            mName                   = name;
            mExecutionAction        = executionAction;
            mObjectConverterFactory = objectConverterFactory;
        }
        /// <summary>
        /// Attempts to retrieve an executable command registered under the given name.
        /// </summary>
        /// <param name="name">The name of the command registration to find.</param>
        /// <returns>The executable command registered under the given name if it exists; otherwise null.</returns>
        /// <exception cref="ArgumentException">Thrown when the given command name is not a valid string.</exception>
        public IExecutableCommandRegistration GetCommandRegistration(string name)
        {
            VerificationProvider.VerifyValidString(name, "name");

            IExecutableCommandRegistration registration;

            if (!mCommandRegistrations.TryGetValue(name, out registration))
            {
                return(null);
            }

            return(registration);
        }
Пример #6
0
        /// <summary>
        /// Attempts to retrieve a value from the set of commands that is stored under the given key.
        /// </summary>
        /// <typeparam name="TValue">The type of the value to be retrieved.</typeparam>
        /// <param name="key">The key of the command to retrieve.</param>
        /// <param name="value">An output parameter that will be populated with the requested value.</param>
        /// <returns>True if the given key matches a stored command; false otherwise.</returns>
        /// <exception cref="ArgumentException">Thrown when the given key value is not a valid string.</exception>
        public bool TryGetValue <TValue>(string key, out TValue value)
        {
            VerificationProvider.VerifyValidString(key, "key");

            // Initialize the output parameter with a default value
            value = default(TValue);

            CommandResult command;

            if (!mKeyedCommands.TryGetValue(key, out command))
            {
                // The command does not exist under the given key
                return(false);
            }

            // We have the command, so get hold of the relevant converter for its type
            var converter = mObjectConverterFactory.Create <TValue>();

            value = converter.Convert(command.Value);

            return(true);
        }