Skip to content

Help fails when a command argument is specified #2126

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

Open
3 tasks done
bigluck opened this issue May 9, 2025 · 1 comment
Open
3 tasks done

Help fails when a command argument is specified #2126

bigluck opened this issue May 9, 2025 · 1 comment
Labels
area/v3 relates to / is being considered for v3 kind/bug describes or fixes a bug status/triage maintainers still need to look into this

Comments

@bigluck
Copy link

bigluck commented May 9, 2025

My urfave/cli version is

v3.3.2

Checklist

  • Are you running the latest v3 release? The list of releases is here.
  • Did you check the manual for your release? The v3 manual is here
  • Did you perform a search about this problem? Here's the GitHub guide about searching.

Describe the bug

The --help argument causes an error when specified in a command with arguments.

To reproduce

package main

import (
	"context"
	"fmt"
	"os"

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

func main() {
	app := &cli.Command{
		Name:  "My CLI",
		Usage: "My Usage",
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:    "profile",
				Aliases: []string{"p"},
				Usage:   "Name of the profile to use",
				Value:   "default",
			},
		},
		Commands: []*cli.Command{
			{
				Name: "command",
				Arguments: []cli.Argument{
					&cli.StringArg{
						Name:      "arg1",
						UsageText: "ARG1",
					},
				},
				Action: func(ctx context.Context, cmd *cli.Command) error {
					fmt.Printf("My profile: %s\n", cmd.String("profile"))
					fmt.Printf("My command: arg1=%s\n", cmd.StringArg("arg1"))
					return nil
				},
				Usage: "Show the version of My CLI",
			},
		},
	}

	err := app.Run(context.Background(), os.Args)
	if err != nil {
		fmt.Println("Error:", err)
		os.Exit(1)
	}
}

Observed behavior

Both these commands work:

$ go run ./cli-error command --help
...
$ go run ./cli-error command --profile dev --help
...

But when I specify one of the command's arguments, it raises an error:

$ go run ./cli-error command ciao --help
Error: No help topic for 'ciao'
exit status 1

$ go run ./cli-error command --help ciao
Error: No help topic for 'ciao'
exit status 1

Expected behavior

I'm not sure if that's intentional behavior, but with nano for example (just a random command), I can specify --help both before and after an argument.

$ nano ./poetry.lock --help
Usage: nano [OPTIONS] [[+LINE[,COLUMN]] FILE]...

$ nano --help poetry.lock
Usage: nano [OPTIONS] [[+LINE[,COLUMN]] FILE]..

$ nano --help
Usage: nano [OPTIONS] [[+LINE[,COLUMN]] FILE]...

$ nano --help2
nano: unrecognized option '--help2'
Type 'nano -h' for a list of available options.

Run go version and paste its output here

$ go version
go version go1.23.3 darwin/arm64
@bigluck bigluck added kind/bug describes or fixes a bug status/triage maintainers still need to look into this area/v3 relates to / is being considered for v3 labels May 9, 2025
@bigluck bigluck changed the title your bug title goes here Help fails when a command argument is specified May 9, 2025
@phlashdev
Copy link

I also think that specifying the --help flag should work regardless of it's position. This is also stated in the CLI Guidelines:

Ignore any other flags and arguments that are passed—you should be able to add -h to the end of anything and it should show help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 kind/bug describes or fixes a bug status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

2 participants