Skip to content

CommandLineBuilder inconsistency? #2034

Closed

Description

@cyungmann

Invoking the following program with no arguments:

using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;

internal static class Program
{
    //private static Task<int> Main(string[] args) =>
    //    new CommandLineBuilder(CreateRootCommand())
    //    .Build()
    //    .InvokeAsync(args);

    private static Task<int> Main(string[] args) =>
        CreateRootCommand().InvokeAsync(args);

    private static Command CreateRootCommand()
    {
        var rootCommand = new RootCommand("Specifying a sub-command is required.");
        var fooCommand = new Command("foo");
        rootCommand.Add(fooCommand);
        return rootCommand;
    }
}

results in a red error message that says "Required command was not provided." followed by the program help text, and the program returns an error exit code.

However, if I replace the Main method above with the commented-out Main method and invoke the program with no arguments, then nothing appears and the program returns a successful exit code.

Is this discrepancy intentional? If so, how do I make the CommandLineBuilder version behave the same as the non-CommandLineBuilder version?

This is using System.CommandLine 2.0.0-beta4.22272.1

Thanks!

Activity

  1. KalleOlaviNiemitalo commented on Jan 24, 2023

    @KalleOlaviNiemitalo

    Before Build(), call CommandLineBuilderExtensions.UseDefaults or just CommandLineBuilderExtensions.UseParseErrorReporting.

  2. self-assigned this
    on Mar 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions