command/flag: implement repeatable flag
Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
parent
35037705a9
commit
89970f5197
@ -44,6 +44,26 @@ func (v BoolFlag) Define(b *strings.Builder, set *flag.FlagSet, p any, name, usa
|
|||||||
b.WriteString(" [" + prettyFlag(name) + "]")
|
b.WriteString(" [" + prettyFlag(name) + "]")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RepeatableFlag implements an ordered, repeatable string flag.
|
||||||
|
type RepeatableFlag []string
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) String() string {
|
||||||
|
if r == nil {
|
||||||
|
return "<nil>"
|
||||||
|
}
|
||||||
|
return strings.Join(*r, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) Set(v string) error {
|
||||||
|
*r = append(*r, v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RepeatableFlag) Define(b *strings.Builder, set *flag.FlagSet, _ any, name, usage string) {
|
||||||
|
set.Var(r, name, usage)
|
||||||
|
b.WriteString(" [" + prettyFlag(name) + " <value>]")
|
||||||
|
}
|
||||||
|
|
||||||
// this has no effect on parse outcome
|
// this has no effect on parse outcome
|
||||||
func prettyFlag(name string) string {
|
func prettyFlag(name string) string {
|
||||||
switch len(name) {
|
switch len(name) {
|
||||||
|
@ -83,6 +83,12 @@ func TestParse(t *testing.T) {
|
|||||||
[]string{"--int", "2147483647", "int"},
|
[]string{"--int", "2147483647", "int"},
|
||||||
"2147483647", "", nil,
|
"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",
|
"d=0 bool flag",
|
||||||
buildTestCommand,
|
buildTestCommand,
|
||||||
@ -144,13 +150,14 @@ func TestParse(t *testing.T) {
|
|||||||
buildTestCommand,
|
buildTestCommand,
|
||||||
[]string{},
|
[]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:
|
Commands:
|
||||||
error return an error
|
error return an error
|
||||||
print wraps Fprint
|
print wraps Fprint
|
||||||
string print string passed by flag
|
string print string passed by flag
|
||||||
int print int passed by flag
|
int print int passed by flag
|
||||||
|
repeat print repeated values passed by flag
|
||||||
empty empty subcommand
|
empty empty subcommand
|
||||||
join wraps strings.Join
|
join wraps strings.Join
|
||||||
succeed this command succeeds
|
succeed this command succeeds
|
||||||
@ -163,13 +170,14 @@ Commands:
|
|||||||
buildTestCommand,
|
buildTestCommand,
|
||||||
[]string{"-h"},
|
[]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:
|
Commands:
|
||||||
error return an error
|
error return an error
|
||||||
print wraps Fprint
|
print wraps Fprint
|
||||||
string print string passed by flag
|
string print string passed by flag
|
||||||
int print int passed by flag
|
int print int passed by flag
|
||||||
|
repeat print repeated values passed by flag
|
||||||
empty empty subcommand
|
empty empty subcommand
|
||||||
join wraps strings.Join
|
join wraps strings.Join
|
||||||
succeed this command succeeds
|
succeed this command succeeds
|
||||||
@ -180,6 +188,8 @@ Flags:
|
|||||||
fail early
|
fail early
|
||||||
-int int
|
-int int
|
||||||
store value for the "int" command (default -1)
|
store value for the "int" command (default -1)
|
||||||
|
-repeat value
|
||||||
|
store value for the "repeat" command
|
||||||
-string string
|
-string string
|
||||||
store value for the "string" command (default "default")
|
store value for the "string" command (default "default")
|
||||||
-v verbose output
|
-v verbose output
|
||||||
@ -269,6 +279,7 @@ func buildTestCommand(wout, wlog io.Writer) (c command.Command) {
|
|||||||
|
|
||||||
flagString string
|
flagString string
|
||||||
flagInt int
|
flagInt int
|
||||||
|
flagRepeat command.RepeatableFlag
|
||||||
)
|
)
|
||||||
|
|
||||||
logf := newLogFunc(wlog)
|
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").
|
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 }).
|
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").
|
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("empty", "empty subcommand")
|
||||||
c.New("hidden", command.UsageInternal)
|
c.New("hidden", command.UsageInternal)
|
||||||
|
Loading…
Reference in New Issue
Block a user