Implement flag arrays

This commit is contained in:
nemunaire 2018-12-04 18:34:38 +01:00
parent 3056a19d09
commit dbf1985d25
6 changed files with 90 additions and 8 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"path"
"strings"
"unicode"
"srs.epita.fr/fic-server/libfic"
@ -43,16 +44,61 @@ func SyncExerciceFlags(i Importer, exercice fic.Exercice) (errs []string) {
kmap := map[int64]fic.Flag{}
// Import normal flags
flags:
for nline, flag := range params.Flags {
if len(flag.Label) == 0 {
flag.Label = "Flag"
}
if !isFullGraphic(flag.Raw) {
if flag.Label[0] == '`' {
errs = append(errs, fmt.Sprintf("%q: flag #%d: Label should not begin with `.", path.Base(exercice.Path), nline + 1))
flag.Label = flag.Label[1:]
}
// Concatenate array
var raw string
if f, ok := flag.Raw.([]interface{}); ok {
if len(flag.ValidatorRe) > 0 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: ValidatorRe cannot be defined for this kind of flag.", path.Base(exercice.Path), nline + 1))
flag.ValidatorRe = ""
}
if len(flag.Separator) == 0 {
flag.Separator = ","
} else if len(flag.Separator) > 1 {
flag.Separator = string(flag.Separator[0])
errs = append(errs, fmt.Sprintf("%q: flag #%d: separator truncated to %q", path.Base(exercice.Path), nline + 1, flag.Separator))
}
for i, v := range f {
if g, ok := v.(string); ok {
if strings.Index(g, flag.Separator) != -1 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: flag items cannot contain %q character as it is used as separator. Change the separator attribute for this flag.", path.Base(exercice.Path), nline + 1, flag.Separator))
continue flags
} else {
raw += g + flag.Separator
}
} else {
errs = append(errs, fmt.Sprintf("%q: flag #%d, item %d has an invalid type: can only be string, is %T.", path.Base(exercice.Path), nline + 1, i, v))
continue flags
}
}
flag.Label = "`" + flag.Separator + flag.Label
} else if f, ok := flag.Raw.(int64); ok {
raw = fmt.Sprintf("%d", f)
} else if f, ok := flag.Raw.(string); !ok {
errs = append(errs, fmt.Sprintf("%q: flag #%d has an invalid type: can only be []string or string, not %T", path.Base(exercice.Path), nline + 1, flag.Raw))
continue
} else {
raw = f
}
if !isFullGraphic(raw) {
errs = append(errs, fmt.Sprintf("%q: WARNING flag #%d: non-printable characters in flag, is this really expected?", path.Base(exercice.Path), nline + 1))
}
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, validatorRegexp(flag.ValidatorRe), []byte(flag.Raw), 0); err != nil {
if k, err := exercice.AddRawFlag(flag.Label, flag.Help, flag.IgnoreCase, validatorRegexp(flag.ValidatorRe), []byte(raw), 0); err != nil {
errs = append(errs, fmt.Sprintf("%q: error flag #%d: %s", path.Base(exercice.Path), nline + 1, err))
continue
} else {