From a9118d50fb0092999082a5987f84a8cfe642ee1a Mon Sep 17 00:00:00 2001 From: Will Dollman Date: Tue, 15 Oct 2024 11:04:27 +0100 Subject: [PATCH] Fix #1105: Execute commands with zero arguments correctly (#1117) * Don't return help for commands with no arguments * Standarise --help flag across different commands * Update changelog --- CHANGELOG.md | 6 ++++++ cmd/src/cmd.go | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52962df8bf..ef1ec46a4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ All notable changes to `src-cli` are documented in this file. ## Unreleased +## 5.8.1 + +### Fixed + +- Fixed an issue preventing some commands from executing correctly when no arguments are passed [#1117](https://github.com/sourcegraph/src-cli/pull/1117) + ## 5.8.0 ### Added diff --git a/cmd/src/cmd.go b/cmd/src/cmd.go index 3ffe235d7d..35cc240fac 100644 --- a/cmd/src/cmd.go +++ b/cmd/src/cmd.go @@ -89,15 +89,9 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args [] log.Fatal("reading config: ", err) } - // Parse subcommand flags. - args := flagSet.Args()[1:] - if err := cmd.flagSet.Parse(args); err != nil { - panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err)) - } - - // Show usage examples for subcommand - if len(args) == 0 || slices.IndexFunc(args, func(s string) bool { - return s == "help" || s == "--help" + // Print help to stdout if requested + if slices.IndexFunc(args, func(s string) bool { + return s == "--help" }) >= 0 { cmd.flagSet.SetOutput(os.Stdout) flag.CommandLine.SetOutput(os.Stdout) @@ -105,6 +99,13 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args [] os.Exit(0) } + // Parse subcommand flags. + args := flagSet.Args()[1:] + if err := cmd.flagSet.Parse(args); err != nil { + fmt.Printf("Error parsing subcommand flags: %s\n", err) + panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err)) + } + // Execute the subcommand. if err := cmd.handler(flagSet.Args()[1:]); err != nil { if _, ok := err.(*cmderrors.UsageError); ok {