Comments (4)
I was in a wrong way. You are right!
I was doing like this
Cli.Wrap("./example.exe")
.WithStandardOutputPipe(PipeTarget.ToDelegate(Console.WriteLine))
.Observe(Encoding.UTF8, Encoding.UTF8, forciblyCloseCTS.Token, gracefullyCloseCTS.Token)
.Subscribe();
But I didn't notice that encoding parameters on Observe() has an effect only on the observable, which will be created by Observe() and it doesn't have an effect on Pipe which is merged by WithStandardOutputPipe().
Providing Encoding.UTF8 in WithStandardOutputPie solved my issue.
For your reference, I was trying to run a process made of Go and is using log package for logging. log package doesn't have a feature to set the encoding and seems to depend on the console.
Also, I tested with another batch file, which outputs the current code page by chcp
and run it on CliWrapImpl and DotNetImpl. Both of it outputs the same encoding and looks like both of it doesn't have an effect on the console's encoding. So to change the console's encoding, I have to use something like your WithChcpWrapper
.
Thank you.
from cliwrap.
Hey @mtaku3.
I was able to reproduce the discrepancy with your help, thank you. I'm now trying to figure out whether this is a bug and how to address it.
Furthermore, I tried to replicate your scenario in CliWrap tests (which are running against a .NET executable instead of a batch file) and it didn't work. It seems that ProcessStartInfo.StandardOutputEncoding
does not have any effect on certain type of programs – or at least .NET console applications.
I also tried to dig through the documentation to see whether this is an edge case or an OS-specific behavior but I was not able to find any official information regarding this scenario. The docs you linked (https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.standardoutputencoding?view=net-7.0) don't provide a lot of useful information beyond this remark:
Setting this property does not guarantee that the process will use the specified encoding. The application should be tested to determine which encodings the process supports.
Question: does your original use case also involve a batch file? If not, what kind of program is it?
from cliwrap.
As an immediate workaround, you can use this extension method:
public static Command WithChcpWrapper(this Command command, Encoding encoding)
{
return Cli.Wrap("cmd")
.WithArguments(a => a
.Add("/c")
.Add(
new ArgumentsBuilder()
.Add("chcp")
.Add(encoding.CodePage)
.Add(">nul")
.Add("&&")
.Add(command.TargetFilePath)
.Add(command.Arguments, false)
.Build(),
false
)
)
.WithWorkingDirectory(command.WorkingDirPath)
.WithEnvironmentVariables(command.EnvironmentVariables)
.WithCredentials(command.Credentials)
.WithStandardInputPipe(command.StandardInputPipe)
.WithStandardOutputPipe(command.StandardOutputPipe)
.WithStandardErrorPipe(command.StandardErrorPipe)
.WithValidation(command.Validation);
}
It wraps your existing command in cmd
and sets the encoding within that session. You can use it like so:
private static async Task CliWrapImpl()
{
await Cli.Wrap("echo.bat")
.WithStandardOutputPipe(PipeTarget.ToDelegate(Console.WriteLine, Encoding.UTF8))
.WithChcpWrapper(Encoding.UTF8)
.ExecuteBufferedAsync();
}
Note that I removed ./
from the path because cmd
trips up on paths starting with .
unless they're quoted, and CliWrap doesn't quote .
because it's not considered a special character. You may want to tweak it a bit.
from cliwrap.
Actually, after further testing, it seems that even making this change is enough to get it working. Can you test it out @mtaku3?
private static async Task CliWrapImpl()
{
await Cli.Wrap("./echo.bat")
- .WithStandardOutputPipe(PipeTarget.ToDelegate(Console.WriteLine))
+ .WithStandardOutputPipe(PipeTarget.ToDelegate(Console.WriteLine, Encoding.UTF8))
.ExecuteAsync();
}
from cliwrap.
Related Issues (20)
- Ripgrep returns exit code 1 with no stdout or stderr, can't work out why HOT 3
- Input prompt is not captured HOT 8
- Possibility to not redirect certain process streams HOT 4
- Cannot be used in IIS? HOT 6
- Stdout and stderr order not preserved HOT 10
- Windows 11 throws exception for some arguments passed to SqlPackage HOT 6
- Support intermediate pipe handlers to transform output from one command into input for another HOT 11
- Not getting the proper response from docker command HOT 5
- Add `WithArguments` overload (or new method) that allows appending arguments to an existing command HOT 2
- Command can deadlock if the process writes large output and one of the targets inside `PipeTarget.Merge(...)` throws
- Problem with stdOut HOT 8
- When using .NET Framework instead of .NET Core, there are problems with stdin encoding HOT 10
- UnicodeEncodeError when Python program outputs unicode HOT 4
- Allow forced argument escaping HOT 5
- Allow input midway through a script's execution HOT 5
- Option to allocate a pseudo-terminal for running command-line applications HOT 1
- `telnet` command is throwing 'No such file or directory' error HOT 4
- Argument Escaping HOT 1
- Ignore waiting for output stream when main process is finished, but it has active children reusing the same output streams HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cliwrap.