urfave / cli

A simple, fast, and fun package for building command line apps in Go
https://cli.urfave.org
MIT License
21.91k stars 1.69k forks source link

automatic version flag collides with verbose flag #1936

Closed Skeeve closed 2 days ago

Skeeve commented 4 days ago

Usually -v stands for "verbose output".

This is not possible when a version is set.

User defined -v should take precedence over the abbreviation of --version.

dearchap commented 3 days ago

@Skeeve You can set the HideVersion in command to disable version flag

Skeeve commented 3 days ago

I know. But given I *want the automatic version command AND a verbose flag, I would have thought that the automatic version refrains from setting "v" as an alias.

dearchap commented 3 days ago

The VersionFlag is exported by the cli package for application usage. So you can sets its Aliases to whatever you want or remove them altogether

Skeeve commented 3 days ago

Can you please elaborate on that? I tried to do that, but given my rudimentary go skills, I failed.

dearchap commented 3 days ago

Can you share the code you tried ?

dearchap commented 3 days ago

@Skeeve Here's an example

package main

import (
    "context"
    "log"
    "os"

    "github.com/urfave/cli/v3"
)

func main() {
    val := true
    app := &cli.Command{
        Version: "0.10.1",
        Flags: []cli.Flag{
            &cli.BoolFlag{
                Name:        "v",
                Usage:       "verbose mode",
                Destination: &val,
            },
        },
        Action: func(ctx context.Context, c *cli.Command) error {
            log.Printf("action %v", val)
            return nil
        },
    }
    cli.VersionFlag.(*cli.BoolFlag).Aliases = nil
    if err := app.Run(context.Background(), os.Args); err != nil {
        log.Fatal(err)
    }
}

This produces the following output

$ go run main1.go -h
NAME:
   main1 - A new cli application

USAGE:
   main1 [global options] [command [command options]] [arguments...]

VERSION:
   0.10.1

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   -v          verbose mode (default: false)
   --help, -h  show help (default: false)
   --version   print the version (default: false)

I think it is a bit cumbersome for the user. VersionFlag should be made exportable as a BoolFlag by itself.

Skeeve commented 3 days ago

Can you share the code you tried ?

Sure: https://play.golang.com/p/zfKOggluwK2

package main

import (
    "context"
    "fmt"
    "os"
    "github.com/urfave/cli/v3"
)

func main() {
    cmd := &cli.Command{
        Name:                   "tester",
        Usage:                  "Tool for sorting anbernic favorites",
        Version:                "0.1",
        UseShortOptionHandling: true,
        Flags: []cli.Flag{
            &cli.BoolFlag{
                Name:    "verbose",
                Aliases: []string{"v"},
                Usage:   "Be verbose.",
            },
        },
        Action: testit,
    }

    if err := cmd.Run(context.Background(), os.Args); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}

func testit(_ context.Context, cmd *cli.Command) (err error) {
    return
}
Skeeve commented 3 days ago

@Skeeve Here's an example

Ah! Okay! I see how you did it. For me, as you can see in my example, "v" is the alias for "verbose". In that case, it'll conflict.

Skeeve commented 2 days ago

So the Solution ist to use "v" as the name and "verbose" as an alias?

dearchap commented 2 days ago

@Skeeve Its upto you. You can disable version command or enable version command and change its name/aliases to whatever you want and add your verbose flag with whatever name/alias.

Skeeve commented 2 days ago

@Skeeve Its upto you. You can disable version command or enable version command and change its name/aliases to whatever you want and add your verbose flag with whatever name/alias.

Thanks for your patience with me. I understood now.