question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Design: builder API for parsing args into .NET types

See original GitHub issue

Problem 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:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
natemcmastercommented, Mar 23, 2018

Just added support for this. Should be available soon in nightly builds and will be in 2.2.0

0reactions
peterwurzingercommented, Feb 1, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found