required positional args

This commit is contained in:
Mario Hros 2017-03-30 20:32:39 +02:00
parent 8111804d17
commit 58e62faa3d
2 changed files with 10 additions and 1 deletions

View File

@ -356,6 +356,9 @@ func process(specs []*spec, args []string) error {
for _, spec := range specs {
if spec.positional {
if spec.multiple {
if spec.required && len(positionals) == 0 {
return fmt.Errorf("%s is required", spec.long)
}
err := setSlice(spec.dest, positionals, true)
if err != nil {
return fmt.Errorf("error processing %s: %v", spec.long, err)

View File

@ -54,7 +54,13 @@ func (p *Parser) WriteUsage(w io.Writer) {
fmt.Fprint(w, " ")
up := strings.ToUpper(spec.long)
if spec.multiple {
fmt.Fprintf(w, "[%s [%s ...]]", up, up)
if !spec.required {
fmt.Fprint(w, "[")
}
fmt.Fprintf(w, "%s [%s ...]", up, up)
if !spec.required {
fmt.Fprint(w, "]")
}
} else {
fmt.Fprint(w, up)
}