dotnet / command-line-api

Command line parsing, invocation, and rendering of terminal output.
https://github.com/dotnet/command-line-api/wiki
MIT License
3.38k stars 380 forks source link

Initial thoughts on generation #1829

Open KathleenDollard opened 2 years ago

KathleenDollard commented 2 years ago

@davidfowl

Here is the shape we are considering - very early thoughts.

An entry point in the System.CommandLine namespace or package namespace that is currently named ParserBuilder because an preliminary user study identified that some folks that are do not plan subcommands associate "Command" with that and think something named "Command" is the wrong choice. Also, argparse in Python makes folks with that background think parser.

To create the parser:

The resulting parser is a wrapper for System.CommandLine.Command and offers strongly typed access to the tree. IOW, this is valid for a StarTrek parser:

var janewayOption = pb.NextGeneration.Voyager.Janeway;

We are still looking at the relationship between parsing and invocation. System.CommandLine supplies a bunch of features via this pipeline (most things except validation). The simple case is very simple:

var pb = ParserBuilder.CreateFromMethod(Greet);   // Greet is a method with descriptions in XML comments
return pb.Invoke();  // If args are not specified Environment.CommandLineArgs is used. 

Note that this and the Greet method is the entire application.

Finding args in Top level statement apps not obvious, thus the default to Environment... When folks are using old-style apps, they will want to know where to put the args, so that overload also exists.

If you just want to get the results, and then switch on them, strongly typed results are provided via GetResults.

If you are interested in customizing the pipeline (including removing things from it) I would like to know the scenario to plan how much we balance making this easy vs exposing the metal.

What do you want to make sure you can easily do?

KalleOlaviNiemitalo commented 2 years ago

Will the generated code work in a C# project that has <LangVersion>7.3</LangVersion>, which is the last version supported on .NET Framework?

KathleenDollard commented 2 years ago

I hope the answer to that is Yes. If System.CommandLine supports it, I hope it will, although the current prototypes do not. Thank for the reminder on the importance of this.

KathleenDollard commented 2 years ago

I want to work with folks that have more options/arguments than are comfortable as method parameters to design the CreateFromType approach and understand what level of generation you want in those scenarios.

KalleOlaviNiemitalo commented 2 years ago

Custom completions that depend on parse results. I implemented a Command with Argument\<FileInfo[]> and Option\<string[]>. The completion callback of the option gets the value of the argument and generates completions depending on the contents of those files.

Those completions are CompletionItem instances rather than strings, so the completion code is already specific to System.CommandLine and annotations would not be a problem.

KalleOlaviNiemitalo commented 2 years ago

How is localisation going to work with XML comments?

KathleenDollard commented 2 years ago

@KalleOlaviNiemitalo it depends on the approach to localization. If it is based on the default language's string, then it would be quite easy to generate

Description = Localized(<xml comment description>);

The challenge would be hooking up to the localization approach the user wanted. I am quite out of date on my understanding of common practices here, and would tend to mimic ASP.NET if possible.