Builds a syntactical representation of a Command object.
        public void CommandWithTwoParams_NoArgumentProvided_BuildsCorrectly()
        {
            CommandWithTwoParams command = new CommandWithTwoParams();

            string expected = "command";
            SyntaxBuilder target = new SyntaxBuilder(command);
            string actual = target.ToString();

            Assert.AreEqual(expected, actual);
        }
        public void CommandWithRequiredParam_ArgumentNotProvided_ExceptionThrown()
        {
            const int argument1 = 0;
            CommandWithRequiredParam command = new CommandWithRequiredParam();

            string expected = "command --param1 " + argument1.ToString();
            SyntaxBuilder target = new SyntaxBuilder(command);
            string actual = target.ToString();

            Assert.AreEqual(expected, actual);
        }
        public void CommandWithTwoParams_OneArgumentProvided_BuildsCorrectly()
        {
            const int argument1 = 0;
            CommandWithTwoParams command = new CommandWithTwoParams
            {
                Parameter1 = argument1
            };

            string expected = "command --param1 " + argument1.ToString();
            SyntaxBuilder target = new SyntaxBuilder(command);
            string actual = target.ToString();

            Assert.AreEqual(expected, actual);
        }
Exemple #4
0
        /// <summary>
        /// Gets the command prompt <see cref="String"/> representation for this <see cref="Command"/>.
        /// </summary>
        /// <returns>A <see cref="String"/> that contains the command prompt representation of this
        /// <see cref="Command"/>.</returns>
        public string GetSyntax()
        {
            SyntaxBuilder syntaxBuilder = new SyntaxBuilder(this);

            return(syntaxBuilder.ToString());
        }
        /// <summary>
        /// Starts the process with customized starting information
        /// </summary>
        /// <param name="startInfo">CommandStartInfo object that defines how this command should start</param>
        private void StartProcess(CommandStartInfo startInfo)
        {
            if (this.IsExecuting)
            {
                throw new CommandException("Cannot execute this command because it is already running.");
            }

            var commandStartingEventArgs = new CommandStartingEventArgs(startInfo);
            this.OnCommandStarting(commandStartingEventArgs);
            if (commandStartingEventArgs.Cancel)
            {
                return;
            }

            this.IsExecuting = true;

            if (this.HasExecuted)
            {
                throw new CommandException("This command has already been executed.", null, this);
            }

            if (startInfo == null)
            {
                throw new ArgumentNullException("startInfo");
            }

            var commandSyntaxAttribute = this.GetCommandSyntaxAttribute();
            SyntaxBuilder syntaxBuilder = new SyntaxBuilder(this);
            var arguments = syntaxBuilder.Arguments;

            ProcessStartInfo processStartInfo = startInfo.GetProcessStartInfo(syntaxBuilder.FileName);
            processStartInfo.Arguments = arguments;
            processStartInfo.RedirectStandardError = true;
            processStartInfo.RedirectStandardOutput = true;
            processStartInfo.UseShellExecute = false;

            this.process.StartInfo = processStartInfo;
            this.process.ErrorDataReceived += (s, e) =>
                                                  {
                                                      this.errorOutputBuilder.AppendLine(e.Data);
                                                      this.OnErrorOutputWritten(e.Data);
                                                  };

            this.process.OutputDataReceived += (s, e) =>
                                                   {
                                                       this.standardOutputBuilder.AppendLine(e.Data);
                                                       this.OnStandardOutputWritten(e.Data);
                                                   };

            this.process.Start();
            this.process.BeginErrorReadLine();
            this.process.BeginOutputReadLine();
            this.process.EnableRaisingEvents = true;
        }
        /// <summary>
        /// Gets the command prompt <see cref="String"/> representation for this <see cref="Command"/>.
        /// </summary>
        /// <returns>A <see cref="String"/> that contains the command prompt representation of this 
        /// <see cref="Command"/>.</returns>
        public string GetSyntax()
        {
            SyntaxBuilder syntaxBuilder = new SyntaxBuilder(this);

            return syntaxBuilder.ToString();
        }