Esempio n. 1
0
 private void WaitWhenDebuggerAttached()
 {
     if (Debugger.IsAttached)
     {
         _host.ReadValue("Press <enter> key to exit");
     }
 }
Esempio n. 2
0
        /// <summary>Gets the argument value.</summary>
        /// <param name="consoleHost">The command line host.</param>
        /// <param name="args">The arguments.</param>
        /// <param name="property">The property.</param>
        /// <returns>The value.</returns>
        /// <exception cref="System.InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
        /// <exception cref="InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
        /// <exception cref="InvalidOperationException">The parameter has no default value.</exception>
        public override object GetValue(IConsoleHost consoleHost, string[] args, PropertyInfo property)
        {
            if (!string.IsNullOrEmpty(Name) && Position > 0)
                throw new InvalidOperationException("Either the argument Name or Position can be set, but not both.");

            var value = GetPositionalArgumentValue(args);

            if (value == null)
                value = GetNamedArgumentValue(args, Name);

            if (value != null)
                return ConvertToType(value.ToString(), property.PropertyType);

            if (!IsInteractiveMode(args) && DefaultValue != null)
                return ConvertToType(DefaultValue.ToString(), property.PropertyType);
            
            var stringVal = consoleHost.ReadValue(GetFullParameterDescription(property));
            if (stringVal == "[default]")
            {
                if (DefaultValue != null)
                    return ConvertToType(DefaultValue.ToString(), property.PropertyType);

                throw new InvalidOperationException("The parameter has no default value.");
            }

            return ConvertToType(stringVal, property.PropertyType);
        }
Esempio n. 3
0
 /// <summary>
 /// Prompt user to press a key before continuing
 /// </summary>
 /// <param name="message">Message to display</param>
 private static void PromptInteractiveOnly(IConsoleHost host, string message)
 {
     if (host.InteractiveMode)
     {
         host.ReadValue(message);
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Read the command name using console host if it was not provided by call.
        /// </summary>
        /// <returns>Command name input by user</returns>
        private string ReadCommandNameInteractive()
        {
            _consoleHost.WriteMessage("Commands: \n");
            foreach (var command in Commands)
            {
                _consoleHost.WriteMessage("  " + command.Key + "\n");
            }

            return(_consoleHost.ReadValue("Command: "));
        }
Esempio n. 5
0
        /// <summary>Runs the command.</summary>
        /// <param name="processor">The processor.</param>
        /// <param name="host">The host.</param>
        /// <returns>The input object for the next command.</returns>
        public Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            foreach (var pair in processor.Commands)
            {
                if (pair.Key != "help")
                {
                    PrintCommand(host, pair);
                    host.ReadValue("Press <enter> key for next command...");
                }
            }

            return Task.FromResult<object>(null);
        }
Esempio n. 6
0
        /// <summary>Gets the argument value.</summary>
        /// <param name="consoleHost">The command line host.</param>
        /// <param name="args">The arguments.</param>
        /// <param name="property">The property.</param>
        /// <param name="command">The command.</param>
        /// <param name="input">The output from the previous command in the chain.</param>
        /// <param name="used">Indicates whether a value for the property was found in the given arguments.</param>
        /// <returns>The value.</returns>
        /// <exception cref="System.InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
        /// <exception cref="InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
        /// <exception cref="InvalidOperationException">The parameter has no default value.</exception>
        public override object GetValue(IConsoleHost consoleHost, string[] args, PropertyInfo property, IConsoleCommand command, object input, out string used)
        {
            if (!string.IsNullOrEmpty(Name) && Position > 0)
            {
                throw new InvalidOperationException("Either the argument Name or Position can be set, but not both.");
            }

            used = null;
            string value = null;

            if (TryGetPositionalArgumentValue(args, ref used, out value))
            {
                return(ConvertToType(value, property.PropertyType));
            }

            if (TryGetNamedArgumentValue(args, ref used, out value))
            {
                return(ConvertToType(value, property.PropertyType));
            }

            if (AcceptsCommandInput && input != null)
            {
                return(input);
            }

            if (!IsInteractiveMode(args) && !IsRequired)
            {
                return(property.CanRead ? property.GetValue(command) : null);
            }

            if (ShowPrompt)
            {
                value = consoleHost.ReadValue(GetFullParameterDescription(property, command));
                if (value == "[default]")
                {
                    if (!IsRequired)
                    {
                        return(property.CanRead ? property.GetValue(command) : null);
                    }

                    throw new InvalidOperationException("The parameter '" + Name + "' is required.");
                }

                return(ConvertToType(value, property.PropertyType));
            }
            else
            {
                return(property.CanRead ? property.GetValue(command) : null);
            }
        }
Esempio n. 7
0
        /// <summary>Runs the command.</summary>
        /// <param name="processor">The processor.</param>
        /// <param name="host">The host.</param>
        /// <returns>The input object for the next command.</returns>
        public Task <object> RunAsync(CommandLineProcessor processor, IConsoleHost host)
        {
            if (!string.IsNullOrEmpty(Command) && char.IsLetter(Command[0]))
            {
                if (processor.Commands.ContainsKey(Command))
                {
                    PrintCommand(host, processor.Commands.Single(c => c.Key == Command));
                }
                else
                {
                    host.WriteMessage("Command '" + Command + "' could not be found...");
                }
            }
            else
            {
                host.WriteMessage("\n");
                host.WriteMessage("Usage:\n\n");
                host.WriteMessage("  myapp.exe myCommand /myParameter:myValue /mySecondParameter:myValue\n\n");
                host.WriteMessage("Commands:\n\n");
                foreach (var command in processor.Commands.Where(c => c.Key != "help"))
                {
                    host.WriteMessage("  " + command.Key + "\n");
                }
                host.ReadValue("Press <enter> key to show commands...");

                foreach (var command in processor.Commands)
                {
                    if (command.Key != "help")
                    {
                        PrintCommand(host, command);
                        host.ReadValue("Press <enter> key for next command...");
                    }
                }
            }

            return(Task.FromResult <object>(null));
        }
Esempio n. 8
0
        /// <summary>Gets the name of the command to execute.</summary>
        /// <param name="args">The arguments.</param>
        /// <returns>The command name.</returns>
        protected string GetCommandName(string[] args)
        {
            if (args.Length == 0 || args[0].Length == 0 || !char.IsLetter(args[0][0]))
            {
                _consoleHost.WriteMessage("Commands: \n");
                foreach (var command in Commands)
                {
                    _consoleHost.WriteMessage("  " + command.Key + "\n");
                }

                return(_consoleHost.ReadValue("Command: ").ToLowerInvariant());
            }

            return(args[0].ToLowerInvariant());
        }