Skip to content

Commit

Permalink
Use a non-zero exit code for 'gs' without arguments (#279)
Browse files Browse the repository at this point in the history
Follow up to #278

We present --help when invoked without any arguments,
but that's still an invalid invocation,
so we should fail with a non-zero exit code.
  • Loading branch information
abhinav authored Jul 23, 2024
1 parent 251ed0b commit 1282a9d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .changes/unreleased/Changed-20240723-091300.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kind: Changed
body: 'cli: Run the help command when no subcommands are included'
body: 'cli: Show --help when run without arguments'
time: 2024-07-23T09:13:00.419337+09:00
25 changes: 17 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,6 @@ func main() {
}
}

args := os.Args[1:]
if len(args) == 0 {
args = []string{"--help"}
} else if short, ok := shorthands[args[0]]; ok {
// TODO: Replace first non-flag argument instead.
args = slices.Replace(args, 0, 1, short.Expanded...)
}

komplete.Run(parser,
komplete.WithTransformCompleted(func(args []string) []string {
if len(args) > 0 {
Expand All @@ -184,6 +176,23 @@ func main() {
komplete.WithPredictor("forges", komplete.PredictFunc(predictForges)),
)

args := os.Args[1:]
if len(args) == 0 {
// If invoked with no arguments, show help,
// but then exit with a non-zero status code.
args = []string{"--help"}
parser.Exit = func(int) {
logger.Print("")
logger.Fatal("gs: please provide a command")
}
} else {
// Otherwise, expand the first argument if it's a shorthand.
if short, ok := shorthands[args[0]]; ok {
// TODO: Replace first non-flag argument instead.
args = slices.Replace(args, 0, 1, short.Expanded...)
}
}

kctx, err := parser.Parse(args)
if err != nil {
logger.Fatalf("gs: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions testdata/script/no_args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Running 'gs' with no arguments
# exits with a non-zero status code,
# but prints a help message to stderr.

! gs
stdout 'Usage: gs '
stderr 'please provide a command'

0 comments on commit 1282a9d

Please sign in to comment.