1 Commits
Author SHA1 Message Date
sneak c3c7108ed7 Read --index and --words as decimal numbers (closes #75)
check / check (push) Failing after 3m37s
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. Both now hold an 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
2026-10-12 23:55:08 +00:00
3 changed files with 63 additions and 28 deletions
+21 -1
View File
@@ -123,7 +123,7 @@ func TestAnIndexWithALeadingZeroIsDecimal(t *testing.T) {
func TestAnIndexThatIsNotDecimalDigitsIsRefused(t *testing.T) {
t.Parallel()
for _, index := range []string{"0x10", "1_0", "+1", "-1"} {
for _, index := range []string{"0x10", "1_0", "+1", "-1", ""} {
t.Run(index, func(t *testing.T) {
t.Parallel()
@@ -137,6 +137,26 @@ func TestAnIndexThatIsNotDecimalDigitsIsRefused(t *testing.T) {
}
}
// Neither a command group nor --version reads the index, so these
// show that it is refused while the flags are parsed.
func TestAnIndexNotInDecimalIsRefusedBeforeAnyCommandRuns(t *testing.T) {
t.Parallel()
for _, args := range [][]string{
{"ssh", "-n", "0x10"},
{"-n", "0x10", "--version"},
} {
t.Run(strings.Join(args, " "), func(t *testing.T) {
t.Parallel()
out, err := execute(t, args...)
require.ErrorIs(t, err, strconv.ErrSyntax)
require.ErrorContains(t, err, "--index")
require.Empty(t, out)
})
}
}
func TestTheMnemonicCommandIsUsed(t *testing.T) {
t.Setenv(mnemonic.Variable, "")
+6 -6
View File
@@ -3,7 +3,6 @@ package mnemonic
import (
"fmt"
"strconv"
"github.com/spf13/cobra"
"sneak.berlin/go/keyfunc/internal/childmnemonic"
@@ -32,9 +31,10 @@ func Command() *cobra.Command {
},
}
cmd.Flags().String(
"words", strconv.Itoa(childmnemonic.DefaultWords),
"the `number` of words in the child mnemonic: 12, 18 or 24",
words := options.Decimal(childmnemonic.DefaultWords)
cmd.Flags().Var(
&words, "words",
"how long the child mnemonic is: 12, 18 or 24 words",
)
return cmd
@@ -47,9 +47,9 @@ func derived(cmd *cobra.Command) (string, error) {
return "", err
}
words, err := options.Decimal(cmd, "words")
words, err := cmd.Flags().GetUint32("words")
if err != nil {
return "", err
return "", fmt.Errorf("reading the word count: %w", err)
}
parent, err := options.Mnemonic(cmd)
+36 -21
View File
@@ -17,35 +17,20 @@ func Add(cmd *cobra.Command) {
"mnemonic-command", "",
"shell command whose output is the mnemonic",
)
cmd.PersistentFlags().StringP(
"index", "n", "0",
"which key to derive, as a decimal `number`",
cmd.PersistentFlags().VarP(
new(Decimal), "index", "n",
"which key to derive",
)
}
// Index returns the key index the user asked for.
func Index(cmd *cobra.Command) (uint32, error) {
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)
index, err := cmd.Flags().GetUint32("index")
if err != nil {
return 0, fmt.Errorf("reading --%s: %w", name, err)
return 0, fmt.Errorf("reading the index: %w", err)
}
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
return index, nil
}
// Mnemonic returns the mnemonic, from the first source that has one.
@@ -57,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"
}