Skip to content

Commit a9118d5

Browse files
authored
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
1 parent 847a899 commit a9118d5

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ All notable changes to `src-cli` are documented in this file.
1111

1212
## Unreleased
1313

14+
## 5.8.1
15+
16+
### Fixed
17+
18+
- Fixed an issue preventing some commands from executing correctly when no arguments are passed [#1117](https://github.com/sourcegraph/src-cli/pull/1117)
19+
1420
## 5.8.0
1521

1622
### Added

cmd/src/cmd.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,23 @@ func (c commander) run(flagSet *flag.FlagSet, cmdName, usageText string, args []
8989
log.Fatal("reading config: ", err)
9090
}
9191

92-
// Parse subcommand flags.
93-
args := flagSet.Args()[1:]
94-
if err := cmd.flagSet.Parse(args); err != nil {
95-
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
96-
}
97-
98-
// Show usage examples for subcommand
99-
if len(args) == 0 || slices.IndexFunc(args, func(s string) bool {
100-
return s == "help" || s == "--help"
92+
// Print help to stdout if requested
93+
if slices.IndexFunc(args, func(s string) bool {
94+
return s == "--help"
10195
}) >= 0 {
10296
cmd.flagSet.SetOutput(os.Stdout)
10397
flag.CommandLine.SetOutput(os.Stdout)
10498
cmd.flagSet.Usage()
10599
os.Exit(0)
106100
}
107101

102+
// Parse subcommand flags.
103+
args := flagSet.Args()[1:]
104+
if err := cmd.flagSet.Parse(args); err != nil {
105+
fmt.Printf("Error parsing subcommand flags: %s\n", err)
106+
panic(fmt.Sprintf("all registered commands should use flag.ExitOnError: error: %s", err))
107+
}
108+
108109
// Execute the subcommand.
109110
if err := cmd.handler(flagSet.Args()[1:]); err != nil {
110111
if _, ok := err.(*cmderrors.UsageError); ok {

0 commit comments

Comments
 (0)