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
+1 -1
View File
@@ -375,7 +375,7 @@ function `NamedMapper(name, mapper)`.
| `path` | A path. ~ expansion is applied.
| `existingfile` | An existing file. ~ expansion is applied. `-` is accepted for stdin.
| `existingdir` | An existing directory. ~ expansion is applied.
| `counter` | Increment a numeric field. Useful for `-vvv`
| `counter` | Increment a numeric field. Useful for `-vvv`. Can accept `-s`, `--long` or `--long=N`.
Slices and maps treat type tags specially. For slices, the `type:""` tag
+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)
+9 -1
View File
@@ -262,7 +262,15 @@ func TestCounter(t *testing.T) {
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"-iii"})
_, err := p.Parse([]string{"--int", "--int", "--int"})
require.NoError(t, err)
require.Equal(t, 3, cli.Int)
_, err = p.Parse([]string{"--int=5"})
require.NoError(t, err)
require.Equal(t, 5, cli.Int)
_, err = p.Parse([]string{"-iii"})
require.NoError(t, err)
require.Equal(t, 3, cli.Int)