Simplify error messages

pull/97/head
rwxrob 2 years ago
parent 61c742eb35
commit a4a073847b
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -298,22 +298,22 @@ func (x *Cmd) Run() {
switch { switch {
case len(args) > 0 && cmd.NoArgs: case len(args) > 0 && cmd.NoArgs:
ExitError(TooManyArgs{cmd}) ExitError(TooManyArgs{len(args), 0})
return return
case len(args) < cmd.MinArgs: case len(args) < cmd.MinArgs:
ExitError(NotEnoughArgs{cmd}) ExitError(NotEnoughArgs{len(args), cmd.MinArgs})
return return
case cmd.MaxArgs > 0 && len(args) > cmd.MaxArgs: case cmd.MaxArgs > 0 && len(args) > cmd.MaxArgs:
ExitError(TooManyArgs{cmd}) ExitError(TooManyArgs{len(args), cmd.MaxArgs})
return return
case cmd.NumArgs > 0 && len(args) != cmd.NumArgs: case cmd.NumArgs > 0 && len(args) != cmd.NumArgs:
ExitError(WrongNumArgs{cmd}) ExitError(WrongNumArgs{len(args), cmd.NumArgs})
return return
case cmd.UseConf && Conf == nil: case cmd.UseConf && Conf == nil:
ExitError(UsesConf{cmd}) ExitError(UsesConf{})
return return
case cmd.UseVars && Vars == nil: case cmd.UseVars && Vars == nil:
ExitError(UsesVars{cmd}) ExitError(UsesVars{})
return return
} }

@ -3,36 +3,33 @@ package Z
import "fmt" import "fmt"
type NotEnoughArgs struct { type NotEnoughArgs struct {
Cmd *Cmd Count int
Min int
} }
func (e NotEnoughArgs) Error() string { func (e NotEnoughArgs) Error() string {
return fmt.Sprintf( return fmt.Sprintf(
"usage: %v %v (not enough args, %v required)", "error: %v is not enough args, %v required", e.Count, e.Min)
e.Cmd.Name, e.Cmd.GetUsage(), e.Cmd.MinArgs,
)
} }
type TooManyArgs struct { type TooManyArgs struct {
Cmd *Cmd Count int
Max int
} }
func (e TooManyArgs) Error() string { func (e TooManyArgs) Error() string {
return fmt.Sprintf( return fmt.Sprintf(
"usage: %v %v (too many args, %v maximum)", "error: %v is too many args, %v maximum", e.Count, e.Max)
e.Cmd.Name, e.Cmd.GetUsage(), e.Cmd.MaxArgs,
)
} }
type WrongNumArgs struct { type WrongNumArgs struct {
Cmd *Cmd Count int
Num int
} }
func (e WrongNumArgs) Error() string { func (e WrongNumArgs) Error() string {
return fmt.Sprintf( return fmt.Sprintf(
"usage: %v %v (wrong number of args, %v required)", "error: %v is wrong number of args, %v required", e.Count, e.Num)
e.Cmd.Name, e.Cmd.GetUsage(), e.Cmd.NumArgs,
)
} }
type MissingConf struct { type MissingConf struct {

Loading…
Cancel
Save