Skip to content

ArgumentExtensions.ExistingOnly, but allow "-" for reading standard input #1782

Open
@KalleOlaviNiemitalo

Description

@KalleOlaviNiemitalo

In a command-line app, I'd like to define a mandatory argument whose value is either a file path to read that file, or "-" to read standard input. What is the best practice for defining such an argument so that it works properly with completion and validation?

ArgumentExtensions.ExistingOnly can be used to add validation that an Argument<FileInfo> names an existing file, but this of course does not support "-". In 2.0.0-beta4.22272.1, neither ExistingOnly nor the FileInfo type affects completion, but I imagine they could do so in the future.

If I use Argument<FileInfo>, then the problem is how to recognize that the value is "-", rather than e.g. "./-". FileInfo.Name and FileSystemInfo.FullName are not suitable. FileInfo.ToString() could be suitable but the documentation advises against using it, and in Reference Source, there seems to be some logic that might cause ToString() not to return the entire string that was passed to the constructor.

After my app has recognized "-", it can use either Console.In or Console.OpenStandardInput(), depending on whether it needs to read text or binary data. It can also check IStandardIn.IsInputRedirected and refuse to read binary data from standard input unless redirected. #275 does not seem useful to me because it does not cover binary input. I don't care about cancellation #1073.

So then, is the best practice like this:

  • Define the argument as Argument<string>, to avoid any shenanigans from FileInfo.ToString().
  • Add validation similar to ArgumentExtensions.ExistingOnly, but using string rather than FileInfo, and allowing "-".
  • Don't do anything about completion for now. Reconsider after ICompletionSource for file paths? #1697 has been implemented.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions