Skip to content

Commit

Permalink
Fix #1105: Execute commands with zero arguments correctly (#1117)
Browse files Browse the repository at this point in the history
* Don't return help for commands with no arguments

* Standarise --help flag across different commands

* Update changelog
  • Loading branch information
willdollman authored Oct 15, 2024
1 parent 847a899 commit a9118d5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions cmd/src/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,23 @@ 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)
cmd.flagSet.Usage()
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 {
Expand Down

0 comments on commit a9118d5

Please sign in to comment.