command/flag: implement integer flag
All checks were successful
Test / Create distribution (push) Successful in 25s
Test / Run NixOS test (push) Successful in 3m23s

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
Ophestra 2025-02-23 02:02:01 +09:00
parent 647c6ea21b
commit 35037705a9
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 36 additions and 17 deletions

View File

@ -28,6 +28,14 @@ func (v StringFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, u
b.WriteString(" [" + prettyFlag(name) + " <value>]")
}
// IntFlag is the default value of an int flag.
type IntFlag int
func (v IntFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usage string) {
set.IntVar(p.(*int), name, int(v), usage)
b.WriteString(" [" + prettyFlag(name) + " <int>]")
}
// BoolFlag is the default value of a bool flag.
type BoolFlag bool

View File

@ -64,18 +64,24 @@ func TestParse(t *testing.T) {
[]string{"print", "0", "1", "2"},
"012", "", nil,
},
{
"d=0 out of order string flag",
buildTestCommand,
[]string{"string", "--string", "64d3b4b7b21788585845060e2199a78f"},
"flag provided but not defined: -string\n\nUsage:\ttest string [-h | --help] COMMAND [OPTIONS]\n\n", "",
errors.New("flag provided but not defined: -string"),
},
{
"d=0 string flag",
buildTestCommand,
[]string{"--val", "64d3b4b7b21788585845060e2199a78f", "flag"},
[]string{"--string", "64d3b4b7b21788585845060e2199a78f", "string"},
"64d3b4b7b21788585845060e2199a78f", "", nil,
},
{
"d=0 out of order string flag",
"d=0 int flag",
buildTestCommand,
[]string{"flag", "--val", "64d3b4b7b21788585845060e2199a78f"},
"flag provided but not defined: -val\n\nUsage:\ttest flag [-h | --help] COMMAND [OPTIONS]\n\n", "",
errors.New("flag provided but not defined: -val"),
[]string{"--int", "2147483647", "int"},
"2147483647", "", nil,
},
{
"d=0 bool flag",
@ -138,12 +144,13 @@ func TestParse(t *testing.T) {
buildTestCommand,
[]string{},
`
Usage: test [-h | --help] [-v] [--fail] [--val <value>] COMMAND [OPTIONS]
Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] COMMAND [OPTIONS]
Commands:
error return an error
print wraps Fprint
flag print value passed by flag
string print string passed by flag
int print int passed by flag
empty empty subcommand
join wraps strings.Join
succeed this command succeeds
@ -156,12 +163,13 @@ Commands:
buildTestCommand,
[]string{"-h"},
`
Usage: test [-h | --help] [-v] [--fail] [--val <value>] COMMAND [OPTIONS]
Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] COMMAND [OPTIONS]
Commands:
error return an error
print wraps Fprint
flag print value passed by flag
string print string passed by flag
int print int passed by flag
empty empty subcommand
join wraps strings.Join
succeed this command succeeds
@ -170,9 +178,11 @@ Commands:
Flags:
-fail
fail early
-int int
store value for the "int" command (default -1)
-string string
store value for the "string" command (default "default")
-v verbose output
-val string
store val for the "flag" command (default "default")
`, "", flag.ErrHelp,
},
@ -256,7 +266,9 @@ func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
var (
flagVerbose bool
flagFail bool
flagVal string
flagString string
flagInt int
)
logf := newLogFunc(wlog)
@ -282,11 +294,10 @@ func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
_, err := fmt.Fprint(wout, a...)
return err
}).
Flag(&flagVal, "val", command.StringFlag("default"), "store val for the \"flag\" command").
Command("flag", "print value passed by flag", func(args []string) error {
_, err := fmt.Fprint(wout, flagVal)
return err
})
Flag(&flagString, "string", command.StringFlag("default"), "store value for the \"string\" command").
Command("string", "print string passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagString); return err }).
Flag(&flagInt, "int", command.IntFlag(-1), "store value for the \"int\" command").
Command("int", "print int passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagInt); return err })
c.New("empty", "empty subcommand")
c.New("hidden", command.UsageInternal)