Skip to content

How to convert beta4 code to beta5 when parseResult.GetValue() may return a null? #2602

Answered by arielcostas
ptr727 asked this question in Q&A
Discussion options

You must be logged in to vote

GetValue returns T? by default, meaning for an Option<string> it will return string? (nullable). GetRequiredValue seems to return the type itself and use the DefaultValueFactory if it's not provided.

RootCommand command = new RootCommand("Example - Does something");
Option<string> nameOption = new Option<string>("--name")
{
    Description = "Your name",
    DefaultValueFactory = _ => "World"
};

command.Options.Add(nameOption);
command.SetAction(pr =>
{
    string name = pr.GetRequiredValue(nameOption);
    Console.WriteLine($"Hello, {name}!");
});

command.Parse(args).Invoke();

Calling the former program with --name ptr727 will print "Hello, ptr727!", and calling it without arguments wi…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@ptr727
Comment options

@jonsequitur
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by ptr727
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants