Code to conveniently read option values into a struct

This commit is contained in:
Kovid Goyal
2022-11-14 15:42:00 +05:30
parent 1811949706
commit 707963b694
3 changed files with 154 additions and 19 deletions
+5 -2
View File
@@ -25,13 +25,16 @@ func (self *Command) parse_args(ctx *Context, args []string) error {
return &ParseError{Message: fmt.Sprintf("Unknown option: :yellow:`%s`", opt_str)}
}
opt.seen_option = opt_str
needs_arg := opt.needs_argument()
if has_val {
if !opt.needs_argument() {
if !needs_arg {
return &ParseError{Message: fmt.Sprintf("The option: :yellow:`%s` does not take values", opt_str)}
}
return opt.add_value(opt_val)
} else if opt.needs_argument() {
} else if needs_arg {
expecting_arg_for = opt
} else {
opt.add_value("")
}
return nil
}