aboutsummaryrefslogtreecommitdiffhomepage
path: root/command
diff options
context:
space:
mode:
Diffstat (limited to 'command')
-rw-r--r--command/flag.go8
-rw-r--r--command/parse_test.go45
2 files changed, 36 insertions, 17 deletions
diff --git a/command/flag.go b/command/flag.go
index 5765dd0d..8e890a1a 100644
--- a/command/flag.go
+++ b/command/flag.go
@@ -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
diff --git a/command/parse_test.go b/command/parse_test.go
index 4bb924f4..911389ac 100644
--- a/command/parse_test.go
+++ b/command/parse_test.go
@@ -65,17 +65,23 @@ func TestParse(t *testing.T) {
"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)