-
Notifications
You must be signed in to change notification settings - Fork 395
Feature request: Support POSIX standard of using single hyphen "-" to mean standard input. #2432
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
Comments
How exactly would you imagine this working / looking like in the source code? Considering that it is the application logic and not the System.CommandLine library code that is doing the reading/writing from/to some (named) file (or stream), how would you see System.CommandLine being able to exert influence on the application logic and make it read from stdin / write to stdout instead of a file without you needing to implement this stuff in the application logic anyway...?
That's not what Posix stipulates. Posix establishes the hyphen operand as an (optional) convention to command reading from stdin / writing to stdout instead from/to a file, Posix does not define it to be a request for prompting for a file name. |
The section 12.1 Utility Argument Syntax
Note the term 'should be used' i.e it is not optional. So, the single '-' operand is used to read a VALUE from STDIN (in my example of file name, it is a value from stdin that represent filename. it can be represent any value as i show in the examples below). When using the command In case of STDIN is redirected like: In both cases the system is using STDIN and interpret the single dash as READ STDIN.
The parser is aware of the option and its value. As POC, the parser is aware that single dash is a VALUE like:
Currently I implement this feature from within Incremental generator and hook the code to read from stdin in Suggested Solution: |
I have a few applications that read or write binary data (e.g. compressed formats) rather than text, and I want them to support
Other notes:
In the System.CommandLine API then, I think this would minimally be a validator that can be put in CliArgument<string> and CliOption<string>. One could also think about some metadata for shell integration but I haven't been keeping track of how that works on the Powderhouse branch. |
I find that public class SinglDashTest
{
[Fact]
public void Argument_with_single_dash_can_read_from_stdin()
{
var argument = new CliArgument<string>("arg")
{
CustomParser = x => x.Tokens[0].Value == "-" ? readStdin() : x.Tokens[0].Value,
};
var result = new CliRootCommand { argument }.Parse("-");
result.GetValue(argument)
.Should()
.Be("abc");
}
[Fact]
public void Option_with_single_dash_can_read_from_stdin()
{
var argument = new CliOption<string>("--token")
{
CustomParser = x => x.Tokens[0].Value == "-" ? readStdin() : x.Tokens[0].Value,
};
var result = new CliRootCommand { argument }.Parse("--token -");
result.GetValue(argument)
.Should()
.Be("abc");
}
string readStdin()
{
//show user a meaning prompt
Console.WriteLine("Enter Token");
Console.SetIn(new StringReader("abc"));
return Console.In.ReadToEnd();
}
} No need for new feature :) |
Pay attention to the quoted guideline ending with "or a file named -." Posix does not require the single hyphen to mean a request to use stdin/stdout, it is left for the application developer to decide whether their app wants to treat the hyphen as a file name or a stdin/stdout redirect.
That is an extremely simple use case. However, stdin/stdout redirects are not limited to simple short text values, data piped through stdin or stdout can encompass very large amounts of data that is not necessarily text. That was one of the motivations of my question of how you would imagine S:CL being involved/helpful in what is essentially the application's task to deal with. Anyway, since you found a solution for your use case, i consider my question answered :-) |
I was looking for something similar while working on an application where I want the user to be able to output to either a file or stdout, like when they want to pipe it to another application. I could imagine if you have the following option or argument
The |
Argument<Stream> has these problems:
I think this should be handled as Argument<string> or Argument<some new type>, rather than Argument<Stream> or Argument<FileInfo>. System.CommandLine can provide a validation function ExistingOrDashOnly. (LegalFilePathsOnly and LegalFileNamesOnly already allow "-".) |
Related to #1782. |
Posix support of using single hyphen "-" to mean standard input.
ref: https://pubs.opengroup.org/onlinepubs/9699919799/
example
The
-
argument passed to tar after xzf means to read from standard input instead of from a named file.The
-
argument passed to wget after -O means to write to standard output instead of to a named file.Here OS will prompt the user to enter fileName from keyboard
It is nice if this feature is supported and system prompt the user to enter filename.
The text was updated successfully, but these errors were encountered: