1

I want to add a Option<bool> to the root command but still show the default output when the option is not provided.

RootCommand rootCommand = new RootCommand("My root command");

Option<bool> moviesOption = new("--movies") { Description = "Show available movies" };

rootCommand.Options.Add(moviesOption);

rootCommand.SetAction(parseResult =>
{
    if (parseResult.GetValue(moviesOption))
    {
        foreach (KeyValuePair<int, string> movie in _movies)
            WriteLine($"{movie.Key}: {movie.Value}");
    }
    return 0;
});
return rootCommand.Parse(args).Invoke();

This works when the --movie and/or -h is provided. However, when there is no option provided, there is no output at the console at all.

How to still show the default output (which is the same as -h option output) when no option is provided on the command line?

2
  • 2
    You can use Option<bool?> instead of Option<bool> and check for null to handle the default output when the option isn’t provided Commented Oct 4 at 11:27
  • How do I "handle the default output when the option isn’t provided"? Commented Oct 4 at 11:33

2 Answers 2

1

You can first check the other options, and if no option is handled, print the default help message.

rootCommand.SetAction(parseResult =>
{
    bool optionHandled = false;
    
    //handle option1
    if (parseResult.GetValue(option1))
    {
        ....
        optionHandled = true;
    }

    //check other options
    ....
    
    return optionHandled ? 0 :
        new HelpAction().Invoke(parseResult);
});
Sign up to request clarification or add additional context in comments.

3 Comments

No. This just runs the command with the bool option. What I want is that if no option is provided, use the default rootcommand handler which shows the same output as using -h option.
Do you just want to know if args.Length == 0?
Whatever that works.
1

Consider a slight change (pre-processor lines to allow single file execution under .NET 10 RC1 SDK)

#:package [email protected]
#nullable enable
using System.CommandLine;

RootCommand rootCommand = new RootCommand("My root command");

Option<bool> moviesOption = new("--movies") { Description = "Show available movies" };

rootCommand.Options.Add(moviesOption);

rootCommand.SetAction(parseResult =>
{
    var movies = parseResult.GetValue(moviesOption);
    Console.WriteLine($"Movies = {movies}");
    return 0;
});
return rootCommand.Parse(args).Invoke();

And then run with

dotnet run .\so_q79782454.cs -- --movies

and the result is

Movies = True

But when run with (no arguments to pass to the program):

dotnet run .\so_q79782454.cs

and the result is

Movies = False

So the reason you get no output when --movies is not on the command line is you lack an else on the if.

If you just want to check if a particular option is present (rather than look at its value as ParseResult.GetValue() does) then you need an option type & default value that you can distinguish from --movies=false, or look at ParseResults.Tokens.

3 Comments

How is that different from getting the value? What should be in the else block to accomplish what I want?
It is not clear what you mean by "default content", and thus I cannot fill that in (your code is incomplete as _movies is never defined. This is ultimately just showing that you need more than just getting the value of an option to know if the option was included on the command line, Option<bool> doubly so.
Just seen the edit, and I see shingo's answer shows how to invoke the help. Albeit I wouldn't do that, instead I would make --movies required, and you'll get the help with a message saying --movies is required.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.