Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Env.useShell #23

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -273,7 +276,17 @@ var (stdout, stderror) = process2($"");
var (stdout, stderror) = processl2($"");
```

`await string` does not escape argument so recommend to use `run($"string")` when use with argument.
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.

Expand Down
3 changes: 2 additions & 1 deletion sandbox/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 5 additions & 1 deletion src/ProcessX/Zx/Env.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public static string shell
}
}

public static bool useShell { get; set; } = true;

static readonly Lazy<CancellationTokenSource> _terminateTokenSource = new Lazy<CancellationTokenSource>(() =>
{
var source = new CancellationTokenSource();
Expand Down Expand Up @@ -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();

Expand Down
Loading