Read --index and --words as decimal numbers (closes #75)
check / check (push) Failing after 3m40s
check / check (push) Failing after 3m40s
Both flags were uint32 flags, which the flag library reads with base-0 rules, so `-n 010` derived key 8 and `0x10` or `1_0` were accepted. Both now hold options.Decimal, a flag value whose Set parses with strconv.ParseUint(text, 10, 32): a leading zero stays decimal, and a base prefix, an underscore, a sign or an empty value is refused with an error naming the flag while the flags are parsed, before any command runs. Its type is still named uint32, so --help is unchanged and GetUint32 still reads it back. The README says the index is a decimal number. Model: opus-5-5
This commit was merged in pull request #81.
This commit is contained in:
@@ -4,6 +4,7 @@ package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sneak.berlin/go/keyfunc/internal/mnemonic"
|
||||
@@ -16,8 +17,8 @@ func Add(cmd *cobra.Command) {
|
||||
"mnemonic-command", "",
|
||||
"shell command whose output is the mnemonic",
|
||||
)
|
||||
cmd.PersistentFlags().Uint32P(
|
||||
"index", "n", 0,
|
||||
cmd.PersistentFlags().VarP(
|
||||
new(Decimal), "index", "n",
|
||||
"which key to derive",
|
||||
)
|
||||
}
|
||||
@@ -41,3 +42,33 @@ func Mnemonic(cmd *cobra.Command) (string, error) {
|
||||
|
||||
return mnemonic.Read(cmd.Context(), command)
|
||||
}
|
||||
|
||||
// Decimal is a flag value for a number written in decimal digits only.
|
||||
// The flag library's own uint32 flags read 010 as octal 8 and also
|
||||
// take 0x10 or 1_0.
|
||||
type Decimal uint32
|
||||
|
||||
// String returns the number in decimal digits.
|
||||
func (d *Decimal) String() string {
|
||||
return strconv.FormatUint(uint64(*d), 10)
|
||||
}
|
||||
|
||||
// Set reads text as a decimal number that fits in 32 bits: a leading
|
||||
// zero is still decimal, and a base prefix, an underscore or a sign is
|
||||
// refused.
|
||||
func (d *Decimal) Set(text string) error {
|
||||
number, err := strconv.ParseUint(text, 10, 32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d = Decimal(number)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Type is what --help shows as the flag's type, and what GetUint32
|
||||
// checks before it reads the value back.
|
||||
func (d *Decimal) Type() string {
|
||||
return "uint32"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user