Design: builder API for parsing args into .NET types
See original GitHub issueProblem
Users who prefer the builder API can’t take advantage of the automatic type parsing available in the attribute api for int
, List<T>
, etc. Part of the difficulty is that CommandLineApplication
, CommandArgument
, and CommandOption
only provide string-based properties.
Proposal
Add CommandOption<T>
and CommandArgument<T>
.
There is acts as an implicit requirement that values can be parsed to typeof(T)
.
For custom types, users can provide a custom converter to parse string
into typeof(T)
. CLU will provide parsers for most commonly used types like int
, double
, bool
, etc.
Usage
public static int Main(string[] args)
{
var app = new CommandLineApplication();
CommandArgument<int> arg = app.Argument<int>("arg", "argument");
CommandOption<int> option = app.Option<int>("--arg", CommandOptionType.SingleValue)
.Accepts().Range(0, 100);
CommandOption<IPAddress> option2 = app.Option<IPAddress>("--addres", CommandOptionType.SingleValue)
.UseConverter(val => IPAddress.Parse(val));
app.OnExecute(() =>
{
int[] values = option.Values;
});
return app.Execute(args);
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Parse the Command Line with System.CommandLine
Enables parsing of command-line generic arguments (tokens) into distinct ... For example, the dotnet build command includes the --no-restore ...
Read more >Parse command line arguments/options in C# - ...
The problem is that when adding the reference to my .NET Framework 3.5 project I get a warning icon. From the above page...
Read more >optparse — Parser for command line options
args , the list of positional arguments leftover after parsing options. This tutorial section only covers the four most important option attributes: action...
Read more >picocli - a mighty tiny command line interface
How it works: annotate your class and picocli initializes it from the command line arguments, converting the input to strongly typed values in...
Read more >Tutorial: Build a REST API with HTTP non-proxy integration
Learn how to create an API Gateway API with the HTTP custom integration using the API Gateway console.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just added support for this. Should be available soon in nightly builds and will be in 2.2.0
Ah, I didn’t think of compositing types - my bad.
Your proposal looks quite good to me tbh, since one is then able to distinguish between a “normal” Argument, where you don’t care about parsing into a .NET-Type, and “special” Arguments which have this abillity by explicitly accessing an additional property.
I just tried to come up with a solution to make it accessible through a common
<whatevertype> Values { get; }
- Property, but didn’t do too well without bringing the big reflection-hammer.