Implement label only flag
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2022-01-21 13:06:37 +01:00
parent b98e23d060
commit 01b05aaed0
9 changed files with 311 additions and 11 deletions

View file

@ -95,11 +95,37 @@ func getRawKey(input interface{}, validatorRe string, ordered bool, showLines bo
return
}
func buildLabelFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int) (f *fic.FlagLabel, errs []string) {
if len(flag.Label) == 0 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: Label cannot be empty.", path.Base(exercice.Path), flagline))
return
}
if flag.Raw != nil {
errs = append(errs, fmt.Sprintf("%q: flag #%d: raw cannot be defined.", path.Base(exercice.Path), flagline))
}
if len(flag.Choice) != 0 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: choices cannot be defined.", path.Base(exercice.Path), flagline))
}
f = &fic.FlagLabel{
Order: int8(flagline),
Label: flag.Label,
Variant: flag.Variant,
}
return
}
func buildKeyFlag(exercice *fic.Exercice, flag ExerciceFlag, flagline int, defaultLabel string) (f *fic.Flag, choices []*fic.FlagChoice, errs []string) {
if len(flag.Label) == 0 {
flag.Label = defaultLabel
}
if len(flag.Variant) != 0 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: variant is not defined for this kind of flag.", path.Base(exercice.Path), flagline))
}
if flag.Label[0] == '`' {
errs = append(errs, fmt.Sprintf("%q: flag #%d: Label should not begin with `.", path.Base(exercice.Path), flagline))
flag.Label = flag.Label[1:]
@ -221,6 +247,8 @@ func buildExerciceFlag(i Importer, exercice *fic.Exercice, flag ExerciceFlag, nl
switch strings.ToLower(flag.Type) {
case "":
flag.Type = "key"
case "label":
flag.Type = "label"
case "key":
flag.Type = "key"
case "number":
@ -273,7 +301,18 @@ func buildExerciceFlag(i Importer, exercice *fic.Exercice, flag ExerciceFlag, nl
}
}
if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "radio" || flag.Type == "vector" {
if flag.Type == "label" {
addedFlag, berrs := buildLabelFlag(exercice, flag, nline+1)
if len(berrs) > 0 {
errs = append(errs, berrs...)
}
if addedFlag != nil {
ret = append(ret, importFlag{
Line: nline + 1,
Flag: addedFlag,
})
}
} else if flag.Type == "key" || strings.HasPrefix(flag.Type, "number") || flag.Type == "text" || flag.Type == "ucq" || flag.Type == "radio" || flag.Type == "vector" {
addedFlag, choices, berrs := buildKeyFlag(exercice, flag, nline+1, "Flag")
if len(berrs) > 0 {
errs = append(errs, berrs...)
@ -296,6 +335,10 @@ func buildExerciceFlag(i Importer, exercice *fic.Exercice, flag ExerciceFlag, nl
hasOne := false
isJustified := false
if len(flag.Variant) != 0 {
errs = append(errs, fmt.Sprintf("%q: flag #%d: variant is not defined for this kind of flag.", path.Base(exercice.Path), nline+1))
}
if !flag.NoShuffle {
rand.Shuffle(len(flag.Choice), func(i, j int) {
flag.Choice[i], flag.Choice[j] = flag.Choice[j], flag.Choice[i]