Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default --version option is not working for subcommands #1567

Open
ericsuhong opened this issue Jun 13, 2024 · 4 comments · May be fixed by #1664
Open

Default --version option is not working for subcommands #1567

ericsuhong opened this issue Jun 13, 2024 · 4 comments · May be fixed by #1664
Assignees
Labels
area-CLI Command-Line Interface bug Something isn't working ⭐ top bug Top bug.
Milestone

Comments

@ericsuhong
Copy link

Information

  • OS: Windows
  • Version: 0.47.0
  • Terminal: Windows Terminal

Describe the bug
--version option is working for the root command as expected.

However, when I run --version option for any subcommands, it does not print version and --version option is ignored.

mycommand --version
1.0.0

mycommand subcommand --version
... runs the subcommand without printing out the version

This is confusing because --help option displays --version as a valid option for all subcommands by default.

mycommand subcommand --help

USAGE:
    mycommand subcommand [OPTIONS]

OPTIONS:
                     DEFAULT
    -h, --help                           Prints help information
    -v, --version                        Prints version information
    -o, --output     test                   My custom option

To Reproduce
Run any spectre subcommands with --version.

Expected behavior
--version MUST NOT be displayed by default as part of --help output for subcommands that do not have explicitly defined --version option.


Please upvote 👍 this issue if you are interested in it.

@ericsuhong ericsuhong added bug Something isn't working needs triage labels Jun 13, 2024
@github-actions github-actions bot added the ⭐ top bug Top bug. label Jun 14, 2024
@ericsuhong
Copy link
Author

ericsuhong commented Jun 14, 2024

The issue does not occur when I enable StrictParsing, but I believe this behavior should still be fixed for normal, default case.

@MangelMaxime
Copy link

MangelMaxime commented Jun 18, 2024

I feel like I have a similar issue when using a "default command".

Main file

module CommitLinter.Main

open Spectre.Console.Cli
open CommitLinter.Commands

[<EntryPoint>]
let main args =

    let app = CommandApp<GenerateCommand>()

    app
        .WithDescription("Generate changelog based on the Git history.")
        .Configure(fun config ->
            config.Settings.ApplicationName <- "changelog-gen" // Find a name
        )

    app.Run(args)

Command file

namespace CommitLinter.Commands

open Spectre.Console
open Spectre.Console.Cli
open System.ComponentModel
open System.IO

type GenerateSettings() =
    inherit CommandSettings()

    [<CommandOption("-c|--config")>]
    [<Description("Path to the configuration file")>]
    member val Config: string option = None with get, set

type GenerateCommand() =
    inherit Command<GenerateSettings>()
    interface ICommandLimiter<GenerateSettings>

    override __.Execute(context, settings) =
        printfn "Generate command"
        0

Then if I run dotnet run --project myProject.fsproj -- --version it will prints Generate command when I expect it to print the version number of the tool.

Use strict parsing doesn't help, it only report an issue because --version is unknown probably because it is using the options config from the Generate command only ?

Or perhaps my issue was related to #1216?

@FrankRay78
Copy link
Contributor

Hello @MangelMaxime, your issue is #1216, which I have validated as still occurring in 0.49.1

@FrankRay78
Copy link
Contributor

Unfortunately it's still broken in 0.49.1 @ericsuhong

Here is my command and application:

using Spectre.Console;
using Spectre.Console.Cli;


public class EmptyCommand : Command<EmptyCommandSettings>
{
    public override int Execute(CommandContext context, EmptyCommandSettings settings)
    {
        AnsiConsole.MarkupLine("Hello world");
        return 0;
    }
}


public static class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandApp();

        app.Configure(config =>
        {
            config.SetApplicationVersion("1.0");

            config.AddCommand<EmptyCommand>("hello");
        });

        return app.Run(args);
    }
}

Help for the subcommand shows the version option, but when the version option is specified, the subcommand executes instead:

image

Expected behaviour

  • Subcommand help doesn't show the version option
  • Specifying -v/--version for the subcommand adds these to the remaining arguments (relaxed parsing)
  • Specifying -v/--version for the subcommand throws an exception (strict parsing)

@FrankRay78 FrankRay78 added area-CLI Command-Line Interface and removed needs triage labels Aug 3, 2024
@FrankRay78 FrankRay78 added this to the 1.0 milestone Aug 12, 2024
@FrankRay78 FrankRay78 linked a pull request Aug 13, 2024 that will close this issue
@FrankRay78 FrankRay78 linked a pull request Oct 5, 2024 that will close this issue
6 tasks
@FrankRay78 FrankRay78 linked a pull request Oct 9, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment