/// <summary>
            /// Initializes a new instance of the <see cref="ObjectConverterRegistration"/> class.
            /// </summary>
            /// <param name="targetType">The type of conversion supported by the registered converter.</param>
            /// <param name="objectCreator">A function used to create an instance of the registered
            /// <see cref="IObjectConverter"/> implementation.</param>
            /// <exception cref="ArgumentNullException">Thrown when the given target type or object
            /// creation function is null.</exception>
            internal ObjectConverterRegistration(Type targetType, Func <IObjectConverter> objectCreator)
            {
                VerificationProvider.VerifyNotNull(objectCreator, "objectCreator");

                TargetType     = targetType;
                mObjectCreator = objectCreator;
            }
        /// <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);
        }
Exemple #3
0
        /// <summary>
        /// Allows the application to be configured by derived classes.
        /// </summary>
        /// <param name="configuration">An action used to configure the rule set being used by the application for
        /// defining the context parameters for the command-line argument parsing operation.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given configuration action is null.</exception>
        protected ConsoleApplication <TContext> Configure(Action <CommandLineRuleSet> configuration)
        {
            VerificationProvider.VerifyNotNull(configuration, "configuration");

            configuration.Invoke(mParser.RuleSet);

            return(this);
        }
Exemple #4
0
        /// <summary>
        /// Mimics the <see cref="Console.WriteLine(string, object[])"/> method while changing the color of the output message
        /// based on the specified message type.
        /// </summary>
        /// <param name="messageType">The type of message that is being displayed, which dictates the color of the output message.</param>
        /// <param name="format">The message to be displayed that may optionally contain markers for string formatting.</param>
        /// <param name="parameters">An optional collection of parameters to be formatted into the given message format.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given message format is a null reference.</exception>
        public static void WriteLine(ConsoleMessageType messageType, string format, params object[] parameters)
        {
            VerificationProvider.VerifyNotNull(format, "format");

            ChangeConsoleColor(messageType);

            Console.WriteLine(format, parameters);
            Console.ResetColor();
        }
Exemple #5
0
        /// <summary>
        /// Copies all configuration details from the given rule set into the current rule set.
        /// </summary>
        /// <param name="ruleSet">The rule set to copy the configuration details from.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given rule set is null.</exception>
        public void Copy(CommandLineRuleSet ruleSet)
        {
            VerificationProvider.VerifyNotNull(ruleSet, "ruleSet");

            // Copy the configuration details
            Prefixes            = ruleSet.Prefixes;
            Separators          = ruleSet.Separators;
            AllowMultipleValues = ruleSet.AllowMultipleValues;
            AllowSwitches       = ruleSet.AllowSwitches;
        }
Exemple #6
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;
        }
Exemple #7
0
        /// <summary>
        /// Converts the given object value to an instance of the supported type.
        /// </summary>
        /// <param name="value">The object value to be converted.</param>
        /// <returns>A strongly-typed representation of the given value converted to the supported type.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the given value is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the given value cannot be converted to the target type.</exception>
        public TTarget Convert(object value)
        {
            VerificationProvider.VerifyNotNull(value, "value");

            // Attempt a direct cast to the target type
            if (value is TTarget)
            {
                return((TTarget)value);
            }

            return(ConvertCore(value));
        }
Exemple #8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ConsoleApplication" /> class.
        /// </summary>
        /// <param name="applicationContext">An instance of the strongly-typed application context.</param>
        /// <param name="arguments">A collection of all arguments passed through to the application
        /// from the command line.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given application context or collection of arguments is null.</exception>
        protected ConsoleApplication(TContext applicationContext, string[] arguments)
        {
            VerificationProvider.VerifyNotNull(applicationContext, "applicationContext");
            VerificationProvider.VerifyNotNull(arguments, "arguments");

            mRegistrar = new ExecutableCommandRegistrar(mObjectConverterFactory);

            mArguments = arguments;

            // Initialize and load the application context
            mApplicationContext = applicationContext;
            mApplicationContext.Load();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandResultSet"/> class.
        /// </summary>
        /// <param name="objectConverterFactory">A factory used to create requested object converters.</param>
        /// <param name="commands">A collection of commands to add to the set.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given object converter factory or collection of commands is null.</exception>
        internal CommandResultSet(ObjectConverterFactory objectConverterFactory, IEnumerable <CommandResult> commands)
        {
            VerificationProvider.VerifyNotNull(objectConverterFactory, "objectConverterFactory");
            VerificationProvider.VerifyNotNull(commands, "commands");

            mObjectConverterFactory = objectConverterFactory;
            mCommands = commands;

            // Extract all commands that have a key value
            foreach (var command in commands)
            {
                mKeyedCommands[command.Key] = command;
            }
        }
        /// <summary>
        /// Parses the given collection of arguments based on the current parser rule set.
        /// </summary>
        /// <param name="arguments">A collection of all arguments passed through to the application
        /// from the command line.</param>
        /// <returns>A collection of commands resulting from the parse operation.</returns>
        /// <exception cref="ArgumentNullException">Thrown when the given collection of arguments is null.</exception>
        public IEnumerable <CommandResult> Parse(string[] arguments)
        {
            VerificationProvider.VerifyNotNull(arguments, "arguments");

            var context = new CommandParsingContext(RuleSet);

            foreach (string argument in arguments)
            {
                var command = Parse(context, argument);
                if (command != null)
                {
                    yield return(command);
                }
            }
        }
Exemple #11
0
        /// <summary>
        /// Executes the registered command by invoking the contained action.
        /// </summary>
        /// <param name="result">The result of the command parsing process pertinent to the registered command.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given command result is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when invocation of the contained action fails.</exception>
        public void Execute(CommandResult result)
        {
            VerificationProvider.VerifyNotNull(result, "result");

            // Get hold of the relevant converter for the command value type
            var    converter = mObjectConverterFactory.Create <TValue>();
            TValue value     = converter.Convert(result.Value);

            try
            {
                mExecutionAction.Invoke(value);
            }
            catch (Exception ex)
            {
                // Wrap the exception within an InvalidOperationException and re-throw
                throw new InvalidOperationException(
                          "An error occurred while executing the command. See inner exception for details.", ex);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CommandParsingContext"/> class.
        /// </summary>
        /// <param name="ruleSet">The rule set in use by the command parsing operations.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given rule set is null.</exception>
        internal CommandParsingContext(CommandLineRuleSet ruleSet)
        {
            VerificationProvider.VerifyNotNull(ruleSet, "ruleSet");

            mRuleSet = ruleSet;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ExecutableCommandRegistrar"/> class.
        /// </summary>
        /// <param name="objectConverterFactory">A factory used to create requested object converters.</param>
        /// <exception cref="ArgumentNullException">Thrown when the given object converter factory is null.</exception>
        internal ExecutableCommandRegistrar(ObjectConverterFactory objectConverterFactory)
        {
            VerificationProvider.VerifyNotNull(objectConverterFactory, "objectConverterFactory");

            mObjectConverterFactory = objectConverterFactory;
        }