ArgumentEscaper

We found 10 examples in language CSharp for this search. You will see 39 fragments of code.

        private void AddCLIRunSettingsArgs(List<string> allArgs)
        {
            if (this.VSTestCLIRunSettings != null && this.VSTestCLIRunSettings.Length > 0)
            {
                allArgs.Add("--");
                foreach (var arg in this.VSTestCLIRunSettings)
                {
                    allArgs.Add(ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(arg));
                }
            }
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpace()
        {
            string stringWithSpace = "Some string";

            string expected = "\"Some string\"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpace);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpaceAtEnd()
        {
            string stringWithSpaceAtEnd = "Some string  ";

            string expected = "\"Some string  \"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpaceAtEnd);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldHandleForwardSlash()
        {
            string stringWithForwardSlash = "Some/string";

            string expected = "Some/string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithForwardSlash);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveDoubleQuote()
        {
            string stringWithDoubleQuote = "Some\"string";

            string expected = "Some\\\"string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithDoubleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveSingleQuote()
        {
            string stringWithSingleQuote = "Some'string";

            string expected = "Some'string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSingleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveBackSlash()
        {
            string stringWithBackSlash = @"Some\\string";

            string expected = "Some\\\\string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithBackSlash);

            Assert.AreEqual(expected, result);
        }


        private void AddCLIRunSettingsArgs(List<string> allArgs)
        {
            if (this.VSTestCLIRunSettings != null && this.VSTestCLIRunSettings.Length > 0)
            {
                allArgs.Add("--");
                foreach (var arg in this.VSTestCLIRunSettings)
                {
                    allArgs.Add(ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(arg));
                }
            }
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpace()
        {
            string stringWithSpace = "Some string";

            string expected = "\"Some string\"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpace);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpaceAtEnd()
        {
            string stringWithSpaceAtEnd = "Some string  ";

            string expected = "\"Some string  \"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpaceAtEnd);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldHandleForwardSlash()
        {
            string stringWithForwardSlash = "Some/string";

            string expected = "Some/string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithForwardSlash);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveDoubleQuote()
        {
            string stringWithDoubleQuote = "Some\"string";

            string expected = "Some\\\"string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithDoubleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveSingleQuote()
        {
            string stringWithSingleQuote = "Some'string";

            string expected = "Some'string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSingleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveBackSlash()
        {
            string stringWithBackSlash = @"Some\\string";

            string expected = "Some\\\\string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithBackSlash);

            Assert.AreEqual(expected, result);
        }


        private void AddCLIRunSettingsArgs(List<string> allArgs)
        {
            if (this.VSTestCLIRunSettings != null && this.VSTestCLIRunSettings.Length > 0)
            {
                allArgs.Add("--");
                foreach (var arg in this.VSTestCLIRunSettings)
                {
                    allArgs.Add(ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(arg));
                }
            }
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpace()
        {
            string stringWithSpace = "Some string";

            string expected = "\"Some string\"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpace);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldAddDoubleQuoteIfThereIsSpaceAtEnd()
        {
            string stringWithSpaceAtEnd = "Some string  ";

            string expected = "\"Some string  \"";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSpaceAtEnd);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldHandleForwardSlash()
        {
            string stringWithForwardSlash = "Some/string";

            string expected = "Some/string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithForwardSlash);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveDoubleQuote()
        {
            string stringWithDoubleQuote = "Some\"string";

            string expected = "Some\\\"string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithDoubleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveSingleQuote()
        {
            string stringWithSingleQuote = "Some'string";

            string expected = "Some'string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithSingleQuote);

            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void EscapeArgForProcessStartShouldPreserveBackSlash()
        {
            string stringWithBackSlash = @"Some\\string";

            string expected = "Some\\\\string";
            string result = ArgumentEscaper.HandleEscapeSequenceInArgForProcessStart(stringWithBackSlash);

            Assert.AreEqual(expected, result);
        }

	/// <summary>
	/// Runs the specified command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="args">The arguments to send to the command-line app.</param>
	public static void RunApp(string path, params string?[] args) =>
		RunApp(path, (args ?? throw new ArgumentNullException(nameof(args))).AsEnumerable());

	/// <summary>
	/// Runs the specified command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="args">The arguments to send to the command-line app.</param>
	public static void RunApp(string path, IEnumerable<string?> args) =>
		RunApp(path, new AppRunnerSettings { Arguments = args ?? throw new ArgumentNullException(nameof(args)) });

	/// <summary>
	/// Runs the specified command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="settings">The settings to use when running the app.</param>
	public static int RunApp(string path, AppRunnerSettings settings) =>
		DoRunApp(path, settings);

	/// <summary>
	/// Runs the specified .NET Framework command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="args">The arguments to send to the command-line app.</param>
	/// <remarks>On Linux and macOS, Mono is used to run the app.</remarks>
	[Obsolete("Use AppRunnerSettings.IsFrameworkApp.")]
	public static void RunDotNetFrameworkApp(string path, params string?[] args) =>
		RunDotNetFrameworkApp(path, (args ?? throw new ArgumentNullException(nameof(args))).AsEnumerable());

	/// <summary>
	/// Runs the specified .NET Framework command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="args">The arguments to send to the command-line app.</param>
	/// <remarks>On Linux and macOS, Mono is used to run the app.</remarks>
	[Obsolete("Use AppRunnerSettings.IsFrameworkApp.")]
	public static void RunDotNetFrameworkApp(string path, IEnumerable<string?> args) =>
		RunDotNetFrameworkApp(path, new AppRunnerSettings { Arguments = args ?? throw new ArgumentNullException(nameof(args)), IsFrameworkApp = true });

	/// <summary>
	/// Runs the specified .NET Framework command-line app.
	/// </summary>
	/// <param name="path">The path of the command-line app.</param>
	/// <param name="settings">The settings to use when running the app.</param>
	/// <remarks>On Linux and macOS, Mono is used to run the app.</remarks>
	[Obsolete("Use AppRunnerSettings.IsFrameworkApp.")]
	public static int RunDotNetFrameworkApp(string path, AppRunnerSettings settings)
	{
		var clone = (settings ?? throw new ArgumentNullException(nameof(settings))).Clone();
		clone.IsFrameworkApp = true;
		return DoRunApp(path, clone);
	}


        internal static Dotnet Restore(params string[] args)
        {
            return new Dotnet
            {
                _info = new ProcessStartInfo("dotnet", ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(new[] { "restore" }.Concat(args)))
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                }
            };
        }

        internal static Dotnet AddProjectToProjectReference(string projectFile, params string[] args)
        {
            return new Dotnet
            {
                _info = new ProcessStartInfo("dotnet", ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(new[] { "add", projectFile, "reference" }.Concat(args)))
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                }
            };
        }

        internal static Dotnet AddPackageReference(string projectFile, string packageName, string version = null)
        {
            string argString;
            if (version == null)
            {
                argString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(new[] { "add", projectFile, "package", packageName });
            }
            else
            {
                argString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(new[] { "add", projectFile, "package", packageName, "--version", version });
            }

            return new Dotnet
            {
                _info = new ProcessStartInfo("dotnet", argString)
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                }
            };
        }

        internal static Dotnet AddProjectsToSolution(string solutionFile, IReadOnlyList<string> projects, string solutionFolder = "")
        {
            List<string> allArgs = new List<string>()
            {
                "sln",
                solutionFile,
                "add"
            };

            if (!string.IsNullOrWhiteSpace(solutionFolder))
            {
                allArgs.Add("--solution-folder");
                allArgs.Add(solutionFolder);
            }

            allArgs.AddRange(projects);
            string argString = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(allArgs);

            return new Dotnet
            {
                _info = new ProcessStartInfo("dotnet", argString)
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                }
            };
        }

        internal static Dotnet Version()
        {
            return new Dotnet
            {
                _info = new ProcessStartInfo("dotnet", "--version")
                {
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true
                }
            };
        }

        internal Dotnet ForwardStdErr()
        {
            _errorDataReceived = ForwardStreamStdErr;
            return this;
        }

        public CommandSpec CreateCommandSpec(
           string commandName,
           IEnumerable<string> args,
           string commandPath,
           CommandResolutionStrategy resolutionStrategy,
           IEnvironmentProvider environment)
        {
            var useCmdWrapper = false;

            if (Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = environment.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath == null)
                {
                    useCmdWrapper = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            return useCmdWrapper
                ? CreateCommandSpecWrappedWithCmd(commandPath, args, resolutionStrategy)
                : CreateCommandSpecFromExecutable(commandPath, args, resolutionStrategy);
        }

        private CommandSpec CreateCommandSpecFromExecutable(
            string command,
            IEnumerable<string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
            return new CommandSpec(command, escapedArgs, resolutionStrategy);
        }

        private CommandSpec CreateCommandSpecWrappedWithCmd(
            string command,
            IEnumerable<string> args,
            CommandResolutionStrategy resolutionStrategy)
        {
            var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";

            // Handle the case where ComSpec is already the command
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args = args.Skip(1);
            }

            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (!ArgumentEscaper.IsSurroundedWithQuotes(command) // Don't quote already quoted strings
                && ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return new CommandSpec(comSpec, escapedArgString, resolutionStrategy);
        }

        public CommandSpec CreateCommandSpec(
           string commandName,
           IEnumerable<string> args,
           string commandPath,
           IEnvironmentProvider environment)
        {
            var useCmdWrapper = false;

            if (Path.GetExtension(commandPath).Equals(".cmd", StringComparison.OrdinalIgnoreCase))
            {
                var preferredCommandPath = environment.GetCommandPath(commandName, ".exe");

                if (preferredCommandPath == null)
                {
                    useCmdWrapper = true;
                }
                else
                {
                    commandPath = preferredCommandPath;
                }
            }

            return useCmdWrapper
                ? CreateCommandSpecWrappedWithCmd(commandPath, args)
                : CreateCommandSpecFromExecutable(commandPath, args);
        }

        private CommandSpec CreateCommandSpecFromExecutable(
            string command,
            IEnumerable<string> args)
        {
            var escapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args);
            return new CommandSpec(command, escapedArgs);
        }

        private CommandSpec CreateCommandSpecWrappedWithCmd(
            string command,
            IEnumerable<string> args)
        {
            var comSpec = Environment.GetEnvironmentVariable("ComSpec") ?? "cmd.exe";

            // Handle the case where ComSpec is already the command
            if (command.Equals(comSpec, StringComparison.OrdinalIgnoreCase))
            {
                command = args.FirstOrDefault();
                args = args.Skip(1);
            }

            var cmdEscapedArgs = ArgumentEscaper.EscapeAndConcatenateArgArrayForCmdProcessStart(args);

            if (!ArgumentEscaper.IsSurroundedWithQuotes(command) // Don't quote already quoted strings
                && ArgumentEscaper.ShouldSurroundWithQuotes(command))
            {
                command = $"\"{command}\"";
            }

            var escapedArgString = $"/s /c \"{command} {cmdEscapedArgs}\"";

            return new CommandSpec(comSpec, escapedArgString);
        }

Microsoft.DotNet.Cli.Utils.ArgumentEscaper : Object

Methods :

public static String EscapeAndConcatenateArgArrayForProcessStart(IEnumerable<String> args = )
public static String EscapeAndConcatenateArgArrayForCmdProcessStart(IEnumerable<String> args = )
public static String EscapeSingleArg(String arg = )
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()