aboutsummaryrefslogtreecommitdiffhomepage
path: root/command/parse_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-02-23 02:25:31 +0900
committerOphestra <cat@gensokyo.uk>2025-02-23 02:25:31 +0900
commit89970f51970b320430852ed57dcfc9d6c841322e (patch)
tree5119243e0a5c0f542be845fe4d621657cdf92aaa /command/parse_test.go
parent35037705a90b61ac52a38f6f41ce48b071537143 (diff)
command/flag: implement repeatable flag
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'command/parse_test.go')
-rw-r--r--command/parse_test.go19
1 files changed, 16 insertions, 3 deletions
diff --git a/command/parse_test.go b/command/parse_test.go
index 911389ac..579edd90 100644
--- a/command/parse_test.go
+++ b/command/parse_test.go
@@ -84,6 +84,12 @@ func TestParse(t *testing.T) {
"2147483647", "", nil,
},
{
+ "d=0 repeat flag",
+ buildTestCommand,
+ []string{"--repeat", "0", "--repeat", "1", "--repeat", "2", "--repeat", "3", "--repeat", "4", "repeat"},
+ "[0 1 2 3 4]", "", nil,
+ },
+ {
"d=0 bool flag",
buildTestCommand,
[]string{"-v", "succeed"},
@@ -144,13 +150,14 @@ func TestParse(t *testing.T) {
buildTestCommand,
[]string{},
`
-Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] COMMAND [OPTIONS]
+Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] [--repeat <value>] COMMAND [OPTIONS]
Commands:
error return an error
print wraps Fprint
string print string passed by flag
int print int passed by flag
+ repeat print repeated values passed by flag
empty empty subcommand
join wraps strings.Join
succeed this command succeeds
@@ -163,13 +170,14 @@ Commands:
buildTestCommand,
[]string{"-h"},
`
-Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] COMMAND [OPTIONS]
+Usage: test [-h | --help] [-v] [--fail] [--string <value>] [--int <int>] [--repeat <value>] COMMAND [OPTIONS]
Commands:
error return an error
print wraps Fprint
string print string passed by flag
int print int passed by flag
+ repeat print repeated values passed by flag
empty empty subcommand
join wraps strings.Join
succeed this command succeeds
@@ -180,6 +188,8 @@ Flags:
fail early
-int int
store value for the "int" command (default -1)
+ -repeat value
+ store value for the "repeat" command
-string string
store value for the "string" command (default "default")
-v verbose output
@@ -269,6 +279,7 @@ func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
flagString string
flagInt int
+ flagRepeat command.RepeatableFlag
)
logf := newLogFunc(wlog)
@@ -297,7 +308,9 @@ func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
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 })
+ Command("int", "print int passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagInt); return err }).
+ Flag(nil, "repeat", &flagRepeat, "store value for the \"repeat\" command").
+ Command("repeat", "print repeated values passed by flag", func(args []string) error { _, err := fmt.Fprint(wout, flagRepeat); return err })
c.New("empty", "empty subcommand")
c.New("hidden", command.UsageInternal)