<commit_before>﻿namespace Crabwise.CommandWrap
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.Remoting.Messaging;
    using System.Text;
    using System.Threading;

    /// <summary>
    /// Provides an abstract representation of a command prompt command.
    /// </summary>
    public abstract class Command
    {
        /// <summary>
        /// Builds a string from the error output messages sent by the command.
        /// </summary>
        private readonly StringBuilder errorOutputBuilder = new StringBuilder();

        /// <summary>
        /// Builds a string from the standard output messages sent by the command.
        /// </summary>
        private readonly StringBuilder standardOutputBuilder = new StringBuilder();

        /// <summary>
        /// The process in which this command runs.
        /// </summary>
        private Process process = new Process();

        /// <summary>
        /// Event fired after the command is executed.
        /// </summary>
        public event EventHandler<ExecuteCompletedEventArgs> ExecuteCompleted;

        public event EventHandler<CommandStartingEventArgs> CommandStarting;

        /// <summary>
        /// Gets the output that was printed to standard error after the command was executed.
        /// </summary>
        public string ErrorOutput { get; private set; }

        /// <summary>
        /// Gets a value indicating whether or not this command has ben executed.
        /// </summary>
        /// <remarks>
        /// After a command has been executed it can not be executed again.
        /// </remarks>
        public bool HasExecuted { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the command is executing or not.
        /// </summary>
        public bool IsExecuting { get; private set; }

        /// <summary>
        /// Gets the output that was printed to standard output after the command was executed.
        /// </summary>
        public string StandardOutput { get; private set; }

        /// <summary>
        /// Cancels the running command. Throws an exception if called on a command that is not executing.
        /// </summary>
        public void CancelAsync()
        {
            if (this.IsExecuting)
            {
                this.process.CloseMainWindow();
                this.process.Close();
            }
            else
            {
                throw new CommandException("Cannot cancel this command because it isn't executing.");
            }
        }

        /// <summary>
        /// Executes the command with default starting parameters.
        /// </summary>
        public void ExecuteAsync()
        {
            this.ExecuteAsync(new CommandStartInfo());
        }

        /// <summary>
        /// Executes the command with custom starting parameters.
        /// </summary>
        /// <param name="startInfo">The CommandStartInfo object that defines how the command should start</param>
        public void ExecuteAsync(CommandStartInfo startInfo)
        {
            this.process.Exited += this.ProcessExited;
            this.StartProcess(startInfo);
        }

        /// <summary>
        /// Closes the standard input to this command.
        /// </summary>
        public void CloseStandardInput()
        {
            try
            {
                this.process.StandardInput.Close();
            }
            catch (InvalidOperationException)
            {
            }
        }

        /// <summary>
        /// Executes this command with the default <see cref="CommandStartInfo"/> for this <see cref="Command"/>.
        /// </summary>
        /// <returns>The exit code of the process.</returns>
        /// <remarks>
        /// The default <see cref="CommandStartInfo"/> options can be set by deriving from this class and then 
        /// overriding the <see cref="Command.DefaultCommandStartInfo"/> property.
        /// </remarks>
        public int Execute()
        {
            return this.Execute(new CommandStartInfo());
        }

        /// <summary>
        /// Uses a <see cref="CommandStartInfo"/> object to execute this command.
        /// </summary>
        /// <param name="startInfo">Specifies how to execute this command.</param>
        /// <returns>The exit code of the process.</returns>
        public int Execute(CommandStartInfo startInfo)
        {
            this.StartProcess(startInfo);

            this.process.WaitForExit();

            return this.CleanupProcess();
        }
<commit_msg>Fixes CloseStandardInput so an exception isn't thrown<commit_after>﻿namespace Crabwise.CommandWrap
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.Remoting.Messaging;
    using System.Text;
    using System.Threading;

    /// <summary>
    /// Provides an abstract representation of a command prompt command.
    /// </summary>
    public abstract class Command
    {
        /// <summary>
        /// Builds a string from the error output messages sent by the command.
        /// </summary>
        private readonly StringBuilder errorOutputBuilder = new StringBuilder();

        /// <summary>
        /// Builds a string from the standard output messages sent by the command.
        /// </summary>
        private readonly StringBuilder standardOutputBuilder = new StringBuilder();

        /// <summary>
        /// The process in which this command runs.
        /// </summary>
        private Process process = new Process();

        /// <summary>
        /// Event fired after the command is executed.
        /// </summary>
        public event EventHandler<ExecuteCompletedEventArgs> ExecuteCompleted;

        public event EventHandler<CommandStartingEventArgs> CommandStarting;

        /// <summary>
        /// Gets the output that was printed to standard error after the command was executed.
        /// </summary>
        public string ErrorOutput { get; private set; }

        /// <summary>
        /// Gets a value indicating whether or not this command has ben executed.
        /// </summary>
        /// <remarks>
        /// After a command has been executed it can not be executed again.
        /// </remarks>
        public bool HasExecuted { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the command is executing or not.
        /// </summary>
        public bool IsExecuting { get; private set; }

        /// <summary>
        /// Gets the output that was printed to standard output after the command was executed.
        /// </summary>
        public string StandardOutput { get; private set; }

        /// <summary>
        /// Cancels the running command. Throws an exception if called on a command that is not executing.
        /// </summary>
        public void CancelAsync()
        {
            if (this.IsExecuting)
            {
                this.process.CloseMainWindow();
                this.process.Close();
            }
            else
            {
                throw new CommandException("Cannot cancel this command because it isn't executing.");
            }
        }

        /// <summary>
        /// Executes the command with default starting parameters.
        /// </summary>
        public void ExecuteAsync()
        {
            this.ExecuteAsync(new CommandStartInfo());
        }

        /// <summary>
        /// Executes the command with custom starting parameters.
        /// </summary>
        /// <param name="startInfo">The CommandStartInfo object that defines how the command should start</param>
        public void ExecuteAsync(CommandStartInfo startInfo)
        {
            this.process.Exited += this.ProcessExited;
            this.StartProcess(startInfo);
        }

        /// <summary>
        /// Closes the standard input to this command.
        /// </summary>
        public void CloseStandardInput()
        {
            if (this.process.StartInfo.RedirectStandardInput)
            {
                this.process.StandardInput.Close();
            }
        }

        /// <summary>
        /// Executes this command with the default <see cref="CommandStartInfo"/> for this <see cref="Command"/>.
        /// </summary>
        /// <returns>The exit code of the process.</returns>
        /// <remarks>
        /// The default <see cref="CommandStartInfo"/> options can be set by deriving from this class and then 
        /// overriding the <see cref="Command.DefaultCommandStartInfo"/> property.
        /// </remarks>
        public int Execute()
        {
            return this.Execute(new CommandStartInfo());
        }

        /// <summary>
        /// Uses a <see cref="CommandStartInfo"/> object to execute this command.
        /// </summary>
        /// <param name="startInfo">Specifies how to execute this command.</param>
        /// <returns>The exit code of the process.</returns>
        public int Execute(CommandStartInfo startInfo)
        {
            this.StartProcess(startInfo);

            this.process.WaitForExit();

            return this.CleanupProcess();
        }

{'fraction_non_alphanumeric': 0.1046712802768166, 'fraction_numerical': 0.0, 'mean_word_length': 1.735581188997338, 'pattern_counts': {'":': 0, '<': 101, '<?xml version=': 0, '>': 101, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7296251', 'n_tokens_mistral': 2166, 'n_tokens_neox': 2112, 'n_words': 749}