diff --git a/command.go b/command.go index 77bf364..9741938 100644 --- a/command.go +++ b/command.go @@ -5,7 +5,7 @@ import ( "os" "path/filepath" - "go.fifitido.net/cmd/opts" + "go.fifitido.net/cmd/opt" ) type Command struct { @@ -14,7 +14,7 @@ type Command struct { longDescription string aliases []string arguments []*Argument - opts opts.Set + opts opt.Set subcommands Set parent *Command run func(args []string) @@ -68,7 +68,7 @@ func (c *Command) Execute(args []string) { args = args[1:] } - parser := opts.NewParser(args, c.opts, false) + parser := opt.NewParser(args, c.opts, false) restArgs, err := parser.Parse() if err != nil { fmt.Println(err.Error()) @@ -84,7 +84,7 @@ func (c *Command) Execute(args []string) { } } - helpOpt, ok := opts.Globals().GetBool("help") + helpOpt, ok := opt.Globals().GetBool("help") if ok && helpOpt.Value() { c.ShowHelp() return diff --git a/help.go b/help.go index c4e513d..03f6984 100644 --- a/help.go +++ b/help.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "go.fifitido.net/cmd/opts" + "go.fifitido.net/cmd/opt" ) func (c *Command) ShowHelp() { @@ -43,16 +43,16 @@ func (c *Command) ShowHelp() { paddedWidth := c.opts.MaxWidth() for _, f := range c.opts { - fmt.Println(" " + opts.HelpLine(f, paddedWidth)) + fmt.Println(" " + opt.HelpLine(f, paddedWidth)) } - globalOpts := opts.Globals() + globalOpts := opt.Globals() if len(globalOpts) > 0 { paddedWidth = globalOpts.MaxWidth() fmt.Println() fmt.Println("Global options:") for _, f := range globalOpts { - fmt.Println(" " + opts.HelpLine(f, paddedWidth)) + fmt.Println(" " + opt.HelpLine(f, paddedWidth)) } } diff --git a/opts/bool.go b/opt/bool.go similarity index 98% rename from opts/bool.go rename to opt/bool.go index 2674109..b7848d3 100644 --- a/opts/bool.go +++ b/opt/bool.go @@ -1,4 +1,4 @@ -package opts +package opt import ( "strconv" diff --git a/opts/float.go b/opt/float.go similarity index 98% rename from opts/float.go rename to opt/float.go index 309cd2d..b59157c 100644 --- a/opts/float.go +++ b/opt/float.go @@ -1,4 +1,4 @@ -package opts +package opt import "strconv" diff --git a/opts/global.go b/opt/global.go similarity index 94% rename from opts/global.go rename to opt/global.go index a1fa276..54413d9 100644 --- a/opts/global.go +++ b/opt/global.go @@ -1,4 +1,4 @@ -package opts +package opt var globalOpts = Set{ Bool("help", "h", false, "Show the help menu"), diff --git a/opts/int.go b/opt/int.go similarity index 98% rename from opts/int.go rename to opt/int.go index d8860f6..3c57d67 100644 --- a/opts/int.go +++ b/opt/int.go @@ -1,4 +1,4 @@ -package opts +package opt import "strconv" diff --git a/opts/option.go b/opt/option.go similarity index 98% rename from opts/option.go rename to opt/option.go index 9cda97f..f013927 100644 --- a/opts/option.go +++ b/opt/option.go @@ -1,4 +1,4 @@ -package opts +package opt import "fmt" diff --git a/opts/parser.go b/opt/parser.go similarity index 99% rename from opts/parser.go rename to opt/parser.go index 88f788e..aebad7c 100644 --- a/opts/parser.go +++ b/opt/parser.go @@ -1,4 +1,4 @@ -package opts +package opt import ( "errors" diff --git a/opts/parser_test.go b/opt/parser_test.go similarity index 63% rename from opts/parser_test.go rename to opt/parser_test.go index 9690c92..0501dd1 100644 --- a/opts/parser_test.go +++ b/opt/parser_test.go @@ -1,15 +1,15 @@ -package opts_test +package opt_test import ( "testing" - "go.fifitido.net/cmd/opts" + "go.fifitido.net/cmd/opt" ) func TestParseUnknownLongOption(t *testing.T) { - set := opts.Set{} + set := opt.Set{} args := []string{"--unknown"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -19,9 +19,9 @@ func TestParseUnknownLongOption(t *testing.T) { } func TestParseUnknownShortOption(t *testing.T) { - set := opts.Set{} + set := opt.Set{} args := []string{"-u"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -31,12 +31,12 @@ func TestParseUnknownShortOption(t *testing.T) { } func TestParseUnknownShortChainedOption1(t *testing.T) { - set := opts.Set{ - opts.Bool("banana", "b", false, ""), - opts.Bool("cucumber", "c", false, ""), + set := opt.Set{ + opt.Bool("banana", "b", false, ""), + opt.Bool("cucumber", "c", false, ""), } args := []string{"-abc"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -46,12 +46,12 @@ func TestParseUnknownShortChainedOption1(t *testing.T) { } func TestParseUnknownShortChainedOption2(t *testing.T) { - set := opts.Set{ - opts.Bool("apple", "a", false, ""), - opts.Bool("cucumber", "c", false, ""), + set := opt.Set{ + opt.Bool("apple", "a", false, ""), + opt.Bool("cucumber", "c", false, ""), } args := []string{"-abc"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -61,12 +61,12 @@ func TestParseUnknownShortChainedOption2(t *testing.T) { } func TestParseUnknownShortChainedOption3(t *testing.T) { - set := opts.Set{ - opts.Bool("apple", "a", false, ""), - opts.Bool("banana", "b", false, ""), + set := opt.Set{ + opt.Bool("apple", "a", false, ""), + opt.Bool("banana", "b", false, ""), } args := []string{"-abc"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -76,12 +76,12 @@ func TestParseUnknownShortChainedOption3(t *testing.T) { } func TestParseUnchaninableOption(t *testing.T) { - set := opts.Set{ - opts.Bool("apple", "a", false, ""), - opts.String("banana", "b", "", ""), + set := opt.Set{ + opt.Bool("apple", "a", false, ""), + opt.String("banana", "b", "", ""), } args := []string{"-ab"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -91,92 +91,92 @@ func TestParseUnchaninableOption(t *testing.T) { } func TestParseShortOptionWithValueAndNoSpace(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"-fapple"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseShortOptionWithValueAndSpace(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"-f", "apple"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseShortOptionWithValueAndEqual(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"-f=apple"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseLongOptionWithValueAndEqual(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"--fruit=apple"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseLongOptionWithValueAndSpace(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"--fruit", "apple"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } } func TestParseOptionTerminator(t *testing.T) { - opt := opts.String("fruit", "f", "banana", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "banana", "") + set := opt.Set{o} args := []string{"--fruit", "apple", "--", "hello", "world"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) restArgs, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "apple" { - t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) + if o.Value() != "apple" { + t.Errorf("Expected fruit to be 'apple', got: %s", o.Value()) } if len(restArgs) != 2 { @@ -193,9 +193,9 @@ func TestParseOptionTerminator(t *testing.T) { } func TestParseLongOptionIgnoreUnknown(t *testing.T) { - set := opts.Set{} + set := opt.Set{} args := []string{"--unknown", "hello", "world"} - parser := opts.NewParser(args, set, true) + parser := opt.NewParser(args, set, true) restArgs, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) @@ -215,9 +215,9 @@ func TestParseLongOptionIgnoreUnknown(t *testing.T) { } func TestParseShortOptionIgnoreUnknown(t *testing.T) { - set := opts.Set{} + set := opt.Set{} args := []string{"-q"} - parser := opts.NewParser(args, set, true) + parser := opt.NewParser(args, set, true) _, err := parser.Parse() if err != nil { @@ -226,12 +226,12 @@ func TestParseShortOptionIgnoreUnknown(t *testing.T) { } func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) { - apple := opts.Bool("apple", "a", false, "") - banana := opts.Bool("banana", "b", false, "") - cucumber := opts.Bool("cucumber", "c", false, "") - set := opts.Set{apple, banana, cucumber} + apple := opt.Bool("apple", "a", false, "") + banana := opt.Bool("banana", "b", false, "") + cucumber := opt.Bool("cucumber", "c", false, "") + set := opt.Set{apple, banana, cucumber} args := []string{"-adc"} - parser := opts.NewParser(args, set, true) + parser := opt.NewParser(args, set, true) _, err := parser.Parse() if err != nil { @@ -252,25 +252,25 @@ func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) { } func TestParseSingleDashValue(t *testing.T) { - opt := opts.String("fruit", "f", "", "") - set := opts.Set{opt} + o := opt.String("fruit", "f", "", "") + set := opt.Set{o} args := []string{"-f-"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err != nil { t.Errorf("Expected no error, got: %s", err.Error()) } - if opt.Value() != "-" { - t.Errorf("Expected fruit to be '-', got: %s", opt.Value()) + if o.Value() != "-" { + t.Errorf("Expected fruit to be '-', got: %s", o.Value()) } } func TestParseLongOptionBadValue(t *testing.T) { - opt := opts.Int("fruit", "f", 0, "") - set := opts.Set{opt} + o := opt.Int("fruit", "f", 0, "") + set := opt.Set{o} args := []string{"--fruit=five"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -278,10 +278,10 @@ func TestParseLongOptionBadValue(t *testing.T) { } func TestParseShortOptionWithSpaceBadValue(t *testing.T) { - opt := opts.Int("fruit", "f", 0, "") - set := opts.Set{opt} + o := opt.Int("fruit", "f", 0, "") + set := opt.Set{o} args := []string{"-f", "five"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -289,11 +289,11 @@ func TestParseShortOptionWithSpaceBadValue(t *testing.T) { } func TestParseShortOptionWithoutEqualBadValue(t *testing.T) { - optG := opts.Bool("green", "g", false, "") - optF := opts.Int("fruit", "f", 0, "") - set := opts.Set{optG, optF} + og := opt.Bool("green", "g", false, "") + of := opt.Int("fruit", "f", 0, "") + set := opt.Set{og, of} args := []string{"-ffive"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -301,11 +301,11 @@ func TestParseShortOptionWithoutEqualBadValue(t *testing.T) { } func TestParseShortOptionWithEqualBadValue(t *testing.T) { - optG := opts.Bool("green", "g", false, "") - optF := opts.Int("fruit", "f", 0, "") - set := opts.Set{optG, optF} + og := opt.Bool("green", "g", false, "") + of := opt.Int("fruit", "f", 0, "") + set := opt.Set{og, of} args := []string{"-f=five"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -313,10 +313,10 @@ func TestParseShortOptionWithEqualBadValue(t *testing.T) { } func TestParseLongOptionMissingValue(t *testing.T) { - opt := opts.Float("fruit-price", "f", 0, "") - set := opts.Set{opt} + o := opt.Float("fruit-price", "f", 0, "") + set := opt.Set{o} args := []string{"--fruit-price"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") @@ -324,10 +324,10 @@ func TestParseLongOptionMissingValue(t *testing.T) { } func TestParseShortOptionMissingValue(t *testing.T) { - opt := opts.Float("fruit-price", "f", 0, "") - set := opts.Set{opt} + o := opt.Float("fruit-price", "f", 0, "") + set := opt.Set{o} args := []string{"-f"} - parser := opts.NewParser(args, set, false) + parser := opt.NewParser(args, set, false) _, err := parser.Parse() if err == nil { t.Error("Expected error") diff --git a/opts/set.go b/opt/set.go similarity index 99% rename from opts/set.go rename to opt/set.go index c13dab6..7a76aca 100644 --- a/opts/set.go +++ b/opt/set.go @@ -1,4 +1,4 @@ -package opts +package opt type Set []Option diff --git a/opts/string.go b/opt/string.go similarity index 98% rename from opts/string.go rename to opt/string.go index 7b436f7..f9b4b83 100644 --- a/opts/string.go +++ b/opt/string.go @@ -1,4 +1,4 @@ -package opts +package opt type StringOption struct { name string diff --git a/option.go b/option.go index a1c36bc..6f0db66 100644 --- a/option.go +++ b/option.go @@ -1,7 +1,7 @@ package cmd import ( - "go.fifitido.net/cmd/opts" + "go.fifitido.net/cmd/opt" ) type Option func(*Command) @@ -30,13 +30,13 @@ func WithArguments(args ...*Argument) Option { } } -func WithOption(f opts.Option) Option { +func WithOption(f opt.Option) Option { return func(c *Command) { c.opts = append(c.opts, f) } } -func WithOptions(os ...opts.Option) Option { +func WithOptions(os ...opt.Option) Option { return func(c *Command) { c.opts = append(c.opts, os...) }