Make counter flags more flexible.

They now support three forms:

    -s
    --long
    --long=N

The last of which explicitly sets the counter value.

Fixes #87.
This commit is contained in:
Alec Thomas
2020-06-10 15:40:22 +10:00
parent d4822839a2
commit d480572d75
3 changed files with 32 additions and 2 deletions
+22
View File
@@ -597,6 +597,28 @@ func existingDirMapper(r *Registry) MapperFunc {
func counterMapper() MapperFunc {
return func(ctx *DecodeContext, target reflect.Value) error {
if ctx.Scan.Peek().Type == FlagValueToken {
t, err := ctx.Scan.PopValue("counter")
if err != nil {
return err
}
switch v := t.Value.(type) {
case string:
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return errors.Errorf("expected a counter but got %q (%T)", t, t.Value)
}
target.SetInt(n)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
target.Set(reflect.ValueOf(v))
default:
return errors.Errorf("expected a counter but got %q (%T)", t, t.Value)
}
return nil
}
switch target.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
target.SetInt(target.Int() + 1)