Skip to content

Commit

Permalink
Use common output for usage and errors during parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
zanvd committed Dec 5, 2024
1 parent 438bbff commit a7ee83c
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,13 +746,13 @@ func (p *Parser) process(args []string) error {
if spec.cardinality == multiple {
err := setSliceOrMap(p.val(spec.dest), positionals, true)
if err != nil {
return fmt.Errorf("error processing %s: %v", spec.field.Name, err)
return fmt.Errorf("error processing %s: %v", spec.placeholder, err)
}
positionals = nil
} else {
err := scalar.ParseValue(p.val(spec.dest), positionals[0])
if err != nil {
return fmt.Errorf("error processing %s: %v", spec.field.Name, err)
return fmt.Errorf("error processing %s: %v", spec.placeholder, err)
}
positionals = positionals[1:]
}
Expand All @@ -767,18 +767,13 @@ func (p *Parser) process(args []string) error {
continue
}

name := strings.ToLower(spec.field.Name)
if spec.long != "" && !spec.positional {
name = "--" + spec.long
}

if spec.required {
if spec.short == "" && spec.long == "" {
msg := fmt.Sprintf("environment variable %s is required", spec.env)
return errors.New(msg)
}

msg := fmt.Sprintf("%s is required", name)
msg := fmt.Sprintf("%s is required", spec.placeholder)
if spec.env != "" {
msg += " (or environment variable " + spec.env + ")"
}
Expand Down

0 comments on commit a7ee83c

Please sign in to comment.