Read --index and --words as decimal numbers (closes #75)
check / check (push) Successful in 3m14s
check / check (push) Successful in 3m14s
Both flags were uint32 flags, which the flag library reads with Go's base-0 rules, so `-n 010` derived key 8 and `0x10` or `1_0` were accepted. They are now string flags read by options.Decimal, which takes decimal digits only: a leading zero stays decimal, and a base prefix, an underscore or a sign is refused with an error naming the flag, before the mnemonic is looked for. --help shows both as a number, and the README says the index is a decimal number. Model: opus-5-5
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,20 +17,35 @@ func Add(cmd *cobra.Command) {
|
||||
"mnemonic-command", "",
|
||||
"shell command whose output is the mnemonic",
|
||||
)
|
||||
cmd.PersistentFlags().Uint32P(
|
||||
"index", "n", 0,
|
||||
"which key to derive",
|
||||
cmd.PersistentFlags().StringP(
|
||||
"index", "n", "0",
|
||||
"which key to derive, as a decimal `number`",
|
||||
)
|
||||
}
|
||||
|
||||
// Index returns the key index the user asked for.
|
||||
func Index(cmd *cobra.Command) (uint32, error) {
|
||||
index, err := cmd.Flags().GetUint32("index")
|
||||
return Decimal(cmd, "index")
|
||||
}
|
||||
|
||||
// Decimal reads the named string flag as a number written in decimal
|
||||
// digits only: a leading zero is still decimal, and a base prefix, an
|
||||
// underscore or a sign is refused. The back-quoted word in the usage
|
||||
// of such a flag is what --help shows as its type.
|
||||
func Decimal(cmd *cobra.Command, name string) (uint32, error) {
|
||||
text, err := cmd.Flags().GetString(name)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reading the index: %w", err)
|
||||
return 0, fmt.Errorf("reading --%s: %w", name, err)
|
||||
}
|
||||
|
||||
return index, nil
|
||||
number, err := strconv.ParseUint(text, 10, 32)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf(
|
||||
"reading --%s as a decimal number: %w", name, err,
|
||||
)
|
||||
}
|
||||
|
||||
return uint32(number), nil
|
||||
}
|
||||
|
||||
// Mnemonic returns the mnemonic, from the first source that has one.
|
||||
|
||||
Reference in New Issue
Block a user