implement-modules #43

Closed
sashakoshka wants to merge 502 commits from implement-modules into main
2 changed files with 18 additions and 8 deletions
Showing only changes of commit 58a15d3da0 - Show all commits

View File

@ -64,7 +64,7 @@ func (this *Cli) Parse (args []string) error {
err := flag.Validate(value)
if err != nil {
return errors.New(fmt.Sprint (
"could not parse ", flag.String(), ": ",
"bad value for ", flag.String(), ": ",
err))
}
flag.Value = value
@ -238,3 +238,19 @@ func ValInt (value string) error {
_, err := strconv.Atoi(value)
return err
}
// NewValSet returns a validation function that accepts a set of strings.
func NewValSet (allowed ...string) func (value string) error {
return func (value string) error {
allowedFmt := ""
for index, test := range allowed {
if test == value { return nil }
if index > 0 { allowedFmt += ", " }
allowedFmt += test
}
return errors.New (
"value must be one of (" + allowedFmt + ")")
}
}

View File

@ -2,7 +2,6 @@ package main
import "os"
import "fmt"
import "errors"
import "git.tebibyte.media/sashakoshka/fspl/cli"
import ferrors "git.tebibyte.media/sashakoshka/fspl/errors"
@ -17,12 +16,7 @@ func main () {
optimization := cli.NewInputFlag (
'O', "optimization",
"Optimization level (0-3)", "0",
func (value string) error {
switch value {
case "0", "1", "2", "3": return nil
default: return errors.New("optimization level must be 0-3")
}
})
cli.NewValSet("0", "1", "2", "3"))
application := cli.New("Compile FSPL source files", help, output, optimization)
application.Syntax = "[OPTION]... [FILE]..."