From a7287296c2d3b69c3bac8035fb82396a334c429f Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 14 Jan 2025 17:00:06 +0900 Subject: [PATCH 1/2] add Env.useShell --- README.md | 17 ++++++++++++++++- sandbox/ConsoleApp/Program.cs | 3 ++- src/ProcessX/Zx/Env.cs | 6 +++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6a3e178..a776a80 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,9 @@ using static Zx.Env; // Env.verbose, write all stdout/stderror log to console. default is true. verbose = false; +// Env.useShell, default is true; which invoke process by `cmd/bash "command argment..."`. +useShell = true; + // Env.shell, default is Windows -> "cmd /c", Linux -> "(which bash) -c";. shell = "/bin/sh -c"; @@ -273,7 +276,19 @@ var (stdout, stderror) = process2($""); var (stdout, stderror) = processl2($""); ``` -`await string` does not escape argument so recommend to use `run($"string")` when use with argument. +Here's the English translation: + +By default (useShell == true), commands are executed through the shell. This means that `dotnet --version` is actually converted to something like `"cmd /c \"dotnet --version\""` during execution. When strings contain spaces, they need to be escaped, but please note that escape handling differs depending on the shell (cmd, bash, pwsh, etc.). If you want to avoid execution through the shell, you can set `Env.useShell = false`, which will result in more intuitive execution. + +```csharp +using Zx; +using static Zx.Env; + +useShell = false; +await "dotnet --version"; +``` + +If you want to escape the arguments, you can also use `run($"string")`. If you want to more colorize like Chalk on JavaScript, [Cysharp/Kokuban](https://github.com/Cysharp/Kokuban) styler for .NET ConsoleApp will help. diff --git a/sandbox/ConsoleApp/Program.cs b/sandbox/ConsoleApp/Program.cs index 14a893d..5e4a22f 100644 --- a/sandbox/ConsoleApp/Program.cs +++ b/sandbox/ConsoleApp/Program.cs @@ -8,7 +8,8 @@ using Zx; using static Zx.Env; -await "dotnet add --help"; +useShell = false; +await "dotnet --version"; //// `await string` execute process like shell ////await "cat package.json | grep name"; diff --git a/src/ProcessX/Zx/Env.cs b/src/ProcessX/Zx/Env.cs index 1d9803d..a5f5137 100644 --- a/src/ProcessX/Zx/Env.cs +++ b/src/ProcessX/Zx/Env.cs @@ -44,6 +44,8 @@ public static string shell } } + public static bool useShell { get; set; } = true; + static readonly Lazy _terminateTokenSource = new Lazy(() => { var source = new CancellationTokenSource(); @@ -217,7 +219,9 @@ public static IDisposable color(ConsoleColor color) static async Task<(string StdOut, string StdError)> ProcessStartAsync(string command, CancellationToken cancellationToken, bool forceSilcent = false) { - var cmd = shell + " \"" + command + "\""; + var cmd = useShell + ? shell + " \"" + command + "\"" + : command; var sbOut = new StringBuilder(); var sbError = new StringBuilder(); From ce228aeceb15bcef0da606de5472e0021c0fd354 Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 14 Jan 2025 17:00:52 +0900 Subject: [PATCH 2/2] r --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a776a80..fa42fc8 100644 --- a/README.md +++ b/README.md @@ -276,8 +276,6 @@ var (stdout, stderror) = process2($""); var (stdout, stderror) = processl2($""); ``` -Here's the English translation: - By default (useShell == true), commands are executed through the shell. This means that `dotnet --version` is actually converted to something like `"cmd /c \"dotnet --version\""` during execution. When strings contain spaces, they need to be escaped, but please note that escape handling differs depending on the shell (cmd, bash, pwsh, etc.). If you want to avoid execution through the shell, you can set `Env.useShell = false`, which will result in more intuitive execution. ```csharp