mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: provide usage instruction for CLI argument failures (#12309)
* chore: add usage to # cli arg failures
This commit is contained in:
+13
-2
@@ -562,6 +562,16 @@ func Chain(ms ...MiddlewareFunc) MiddlewareFunc {
|
||||
return chain(reversed...)
|
||||
}
|
||||
|
||||
func ShowUsageOnError(next HandlerFunc) HandlerFunc {
|
||||
return func(i *Invocation) error {
|
||||
err := next(i)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("Usage: %s\nError: %w", i.Command.FullUsage(), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func RequireNArgs(want int) MiddlewareFunc {
|
||||
return RequireRangeArgs(want, want)
|
||||
}
|
||||
@@ -574,7 +584,8 @@ func RequireRangeArgs(start, end int) MiddlewareFunc {
|
||||
panic("start must be >= 0")
|
||||
}
|
||||
return func(next HandlerFunc) HandlerFunc {
|
||||
return func(i *Invocation) error {
|
||||
// ShowUsageOnError will add the command usage before the error message.
|
||||
return ShowUsageOnError(func(i *Invocation) error {
|
||||
got := len(i.Args)
|
||||
switch {
|
||||
case start == end && got != start:
|
||||
@@ -614,7 +625,7 @@ func RequireRangeArgs(start, end int) MiddlewareFunc {
|
||||
default:
|
||||
return next(i)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user