diff --git a/cmd/test.go b/cmd/test.go index 34fb99e..ac667ef 100644 --- a/cmd/test.go +++ b/cmd/test.go @@ -5,13 +5,13 @@ import ( "os" "go.fifitido.net/cmd" - "go.fifitido.net/cmd/flags" + "go.fifitido.net/cmd/opts" ) -var stringFlag = flags.String("string", "s", "", "example string flag") -var intFlag = flags.Int("int", "i", 0, "example int flag") -var boolFlag = flags.Bool("bool", "b", false, "example bool flag") -var floatFlag = flags.Float("float", "f", 0, "example float flag") +var stringOpt = opts.String("string", "s", "", "example string option") +var intOpt = opts.Int("int", "i", 0, "example int option") +var boolOpt = opts.Bool("bool", "b", false, "example bool option") +var floatOpt = opts.Float("float", "f", 0, "example float option") var root = cmd.NewRoot( cmd.WithShortDescription("Example command"), @@ -21,7 +21,7 @@ this example is just a simple hello world program to show the basics of the library.`), cmd.WithSubcommand(subcmd), cmd.WithArgument("name", false), - cmd.WithFlags(stringFlag, intFlag, boolFlag, floatFlag), + cmd.WithOptions(stringOpt, intOpt, boolOpt, floatOpt), cmd.WithRunFunc(func(args []string) { if len(args) == 0 { fmt.Println("Hello World!") diff --git a/command.go b/command.go index 6395ceb..ee01327 100644 --- a/command.go +++ b/command.go @@ -4,7 +4,7 @@ import ( "os" "path/filepath" - "go.fifitido.net/cmd/flags" + "go.fifitido.net/cmd/opts" ) type Command struct { @@ -13,7 +13,7 @@ type Command struct { longDescription string aliases []string arguments []*Argument - flags flags.Set + opts opts.Set subcommands Set parent *Command run func(args []string) @@ -67,7 +67,7 @@ func (c *Command) Execute(args []string) { args = args[1:] } - if err := c.flags.Parse(args); err != nil { + if err := c.opts.Parse(args); err != nil { c.ShowHelp() return } @@ -79,7 +79,7 @@ func (c *Command) Execute(args []string) { return } - // TODO: remove when done with flag parsing + // TODO: remove when done with option parsing if args[0] == "--help" { c.ShowHelp() return diff --git a/flags/bool.go b/flags/bool.go deleted file mode 100644 index fb5f45e..0000000 --- a/flags/bool.go +++ /dev/null @@ -1,56 +0,0 @@ -package flags - -import "strconv" - -type BoolFlag struct { - name string - shortName string - description string - value bool -} - -var _ Flag = (*StringFlag)(nil) - -func Bool(name, shortName string, defaultValue bool, description string) *BoolFlag { - return &BoolFlag{ - name: name, - shortName: shortName, - description: description, - value: defaultValue, - } -} - -// Description implements Flag. -func (f *BoolFlag) Description() string { - return f.description -} - -// Name implements Flag. -func (f *BoolFlag) Name() string { - return f.name -} - -// ShortName implements Flag. -func (f *BoolFlag) ShortName() string { - return f.shortName - -} - -// Value implements Flag. -func (f *BoolFlag) Parse(raw string) error { - if raw == "" { - return nil - } - - boolVal, err := strconv.ParseBool(raw) - if err != nil { - return err - } - - f.value = boolVal - return nil -} - -func (f *BoolFlag) Value() bool { - return f.value -} diff --git a/flags/flag.go b/flags/flag.go deleted file mode 100644 index 3661bd6..0000000 --- a/flags/flag.go +++ /dev/null @@ -1,34 +0,0 @@ -package flags - -import "fmt" - -type Flag interface { - Name() string - ShortName() string - Description() string - Parse(raw string) error -} - -func Names(f Flag) string { - names := "" - if f.ShortName() != "" { - names += fmt.Sprintf("-%s, ", f.ShortName()) - } - names += fmt.Sprintf("--%s", f.Name()) - return names -} - -func HelpLine(f Flag, paddedWidth int) string { - argNames := "" - if f.ShortName() != "" { - argNames += fmt.Sprintf("-%s, ", f.ShortName()) - } - argNames += fmt.Sprintf("--%s", f.Name()) - - spaceSize := paddedWidth - len(argNames) - if spaceSize > 0 { - argNames += fmt.Sprintf("%*s", spaceSize, "") - } - - return argNames + " " + f.Description() -} diff --git a/flags/float.go b/flags/float.go deleted file mode 100644 index d7d7c83..0000000 --- a/flags/float.go +++ /dev/null @@ -1,56 +0,0 @@ -package flags - -import "strconv" - -type FloatFlag struct { - name string - shortName string - description string - value float64 -} - -var _ Flag = (*FloatFlag)(nil) - -func Float(name, shortName string, defaultValue float64, description string) *FloatFlag { - return &FloatFlag{ - name: name, - shortName: shortName, - description: description, - value: defaultValue, - } -} - -// Description implements Flag. -func (f *FloatFlag) Description() string { - return f.description -} - -// Name implements Flag. -func (f *FloatFlag) Name() string { - return f.name -} - -// ShortName implements Flag. -func (f *FloatFlag) ShortName() string { - return f.shortName - -} - -// Value implements Flag. -func (f *FloatFlag) Parse(raw string) error { - if raw == "" { - return nil - } - - floatVal, err := strconv.ParseFloat(raw, 64) - if err != nil { - return err - } - - f.value = floatVal - return nil -} - -func (f *FloatFlag) Value() float64 { - return f.value -} diff --git a/flags/int.go b/flags/int.go deleted file mode 100644 index 63dfb12..0000000 --- a/flags/int.go +++ /dev/null @@ -1,56 +0,0 @@ -package flags - -import "strconv" - -type IntFlag struct { - name string - shortName string - description string - value int -} - -var _ Flag = (*StringFlag)(nil) - -func Int(name, shortName string, defaultValue int, description string) *IntFlag { - return &IntFlag{ - name: name, - shortName: shortName, - description: description, - value: defaultValue, - } -} - -// Description implements Flag. -func (f *IntFlag) Description() string { - return f.description -} - -// Name implements Flag. -func (f *IntFlag) Name() string { - return f.name -} - -// ShortName implements Flag. -func (f *IntFlag) ShortName() string { - return f.shortName - -} - -// Value implements Flag. -func (f *IntFlag) Parse(raw string) error { - if raw == "" { - return nil - } - - intVal, err := strconv.Atoi(raw) - if err != nil { - return err - } - - f.value = intVal - return nil -} - -func (f *IntFlag) Value() int { - return f.value -} diff --git a/flags/string.go b/flags/string.go deleted file mode 100644 index e2ccf2b..0000000 --- a/flags/string.go +++ /dev/null @@ -1,48 +0,0 @@ -package flags - -type StringFlag struct { - name string - shortName string - description string - value string -} - -var _ Flag = (*StringFlag)(nil) - -func String(name, shortName, defaultValue, description string) *StringFlag { - return &StringFlag{ - name: name, - shortName: shortName, - description: description, - value: defaultValue, - } -} - -// Description implements Flag. -func (f *StringFlag) Description() string { - return f.description -} - -// Name implements Flag. -func (f *StringFlag) Name() string { - return f.name -} - -// ShortName implements Flag. -func (f *StringFlag) ShortName() string { - return f.shortName -} - -// Value implements Flag. -func (f *StringFlag) Parse(raw string) error { - if raw == "" { - return nil - } - - f.value = raw - return nil -} - -func (f *StringFlag) Value() string { - return f.value -} diff --git a/help.go b/help.go index f6d8de9..c4e513d 100644 --- a/help.go +++ b/help.go @@ -3,7 +3,7 @@ package cmd import ( "fmt" - "go.fifitido.net/cmd/flags" + "go.fifitido.net/cmd/opts" ) func (c *Command) ShowHelp() { @@ -22,7 +22,7 @@ func (c *Command) ShowHelp() { } } - fmt.Println("[flags]") + fmt.Println("[options]") if len(c.subcommands) > 0 { fmt.Println() @@ -39,20 +39,20 @@ func (c *Command) ShowHelp() { } fmt.Println() - fmt.Println("Available flags:") + fmt.Println("Available options:") - paddedWidth := c.flags.MaxWidth() - for _, f := range c.flags { - fmt.Println(" " + flags.HelpLine(f, paddedWidth)) + paddedWidth := c.opts.MaxWidth() + for _, f := range c.opts { + fmt.Println(" " + opts.HelpLine(f, paddedWidth)) } - globalFlags := flags.Globals() - if len(globalFlags) > 0 { - paddedWidth = globalFlags.MaxWidth() + globalOpts := opts.Globals() + if len(globalOpts) > 0 { + paddedWidth = globalOpts.MaxWidth() fmt.Println() - fmt.Println("Global flags:") - for _, f := range globalFlags { - fmt.Println(" " + flags.HelpLine(f, paddedWidth)) + fmt.Println("Global options:") + for _, f := range globalOpts { + fmt.Println(" " + opts.HelpLine(f, paddedWidth)) } } diff --git a/option.go b/option.go index 266a7e0..a1c36bc 100644 --- a/option.go +++ b/option.go @@ -1,6 +1,8 @@ package cmd -import "go.fifitido.net/cmd/flags" +import ( + "go.fifitido.net/cmd/opts" +) type Option func(*Command) @@ -28,15 +30,15 @@ func WithArguments(args ...*Argument) Option { } } -func WithFlag(f flags.Flag) Option { +func WithOption(f opts.Option) Option { return func(c *Command) { - c.flags = append(c.flags, f) + c.opts = append(c.opts, f) } } -func WithFlags(fs ...flags.Flag) Option { +func WithOptions(os ...opts.Option) Option { return func(c *Command) { - c.flags = append(c.flags, fs...) + c.opts = append(c.opts, os...) } } diff --git a/opts/bool.go b/opts/bool.go new file mode 100644 index 0000000..33d19c2 --- /dev/null +++ b/opts/bool.go @@ -0,0 +1,56 @@ +package opts + +import "strconv" + +type BoolOption struct { + name string + shortName string + description string + value bool +} + +var _ Option = (*StringOption)(nil) + +func Bool(name, shortName string, defaultValue bool, description string) *BoolOption { + return &BoolOption{ + name: name, + shortName: shortName, + description: description, + value: defaultValue, + } +} + +// Description implements Option. +func (o *BoolOption) Description() string { + return o.description +} + +// Name implements Option. +func (o *BoolOption) Name() string { + return o.name +} + +// ShortName implements Option. +func (o *BoolOption) ShortName() string { + return o.shortName + +} + +// Value implements Option. +func (o *BoolOption) Parse(raw string) error { + if raw == "" { + return nil + } + + boolVal, err := strconv.ParseBool(raw) + if err != nil { + return err + } + + o.value = boolVal + return nil +} + +func (o *BoolOption) Value() bool { + return o.value +} diff --git a/opts/float.go b/opts/float.go new file mode 100644 index 0000000..b01c73f --- /dev/null +++ b/opts/float.go @@ -0,0 +1,56 @@ +package opts + +import "strconv" + +type FloatOption struct { + name string + shortName string + description string + value float64 +} + +var _ Option = (*FloatOption)(nil) + +func Float(name, shortName string, defaultValue float64, description string) *FloatOption { + return &FloatOption{ + name: name, + shortName: shortName, + description: description, + value: defaultValue, + } +} + +// Description implements Option. +func (o *FloatOption) Description() string { + return o.description +} + +// Name implements Option. +func (o *FloatOption) Name() string { + return o.name +} + +// ShortName implements Option. +func (o *FloatOption) ShortName() string { + return o.shortName + +} + +// Value implements Option. +func (o *FloatOption) Parse(raw string) error { + if raw == "" { + return nil + } + + floatVal, err := strconv.ParseFloat(raw, 64) + if err != nil { + return err + } + + o.value = floatVal + return nil +} + +func (o *FloatOption) Value() float64 { + return o.value +} diff --git a/flags/global.go b/opts/global.go similarity index 51% rename from flags/global.go rename to opts/global.go index acc1da6..a1fa276 100644 --- a/flags/global.go +++ b/opts/global.go @@ -1,14 +1,14 @@ -package flags +package opts -var globalFlags = Set{ +var globalOpts = Set{ Bool("help", "h", false, "Show the help menu"), Bool("version", "v", false, "Show the app version"), } -func Global(fs ...Flag) { - globalFlags = append(globalFlags, fs...) +func Global(os ...Option) { + globalOpts = append(globalOpts, os...) } func Globals() Set { - return globalFlags + return globalOpts } diff --git a/opts/int.go b/opts/int.go new file mode 100644 index 0000000..7b862fc --- /dev/null +++ b/opts/int.go @@ -0,0 +1,56 @@ +package opts + +import "strconv" + +type IntOption struct { + name string + shortName string + description string + value int +} + +var _ Option = (*StringOption)(nil) + +func Int(name, shortName string, defaultValue int, description string) *IntOption { + return &IntOption{ + name: name, + shortName: shortName, + description: description, + value: defaultValue, + } +} + +// Description implements Option. +func (o *IntOption) Description() string { + return o.description +} + +// Name implements Option. +func (o *IntOption) Name() string { + return o.name +} + +// ShortName implements Option. +func (o *IntOption) ShortName() string { + return o.shortName + +} + +// Value implements Option. +func (o *IntOption) Parse(raw string) error { + if raw == "" { + return nil + } + + intVal, err := strconv.Atoi(raw) + if err != nil { + return err + } + + o.value = intVal + return nil +} + +func (o *IntOption) Value() int { + return o.value +} diff --git a/opts/option.go b/opts/option.go new file mode 100644 index 0000000..3c1e30b --- /dev/null +++ b/opts/option.go @@ -0,0 +1,34 @@ +package opts + +import "fmt" + +type Option interface { + Name() string + ShortName() string + Description() string + Parse(raw string) error +} + +func Names(o Option) string { + names := "" + if o.ShortName() != "" { + names += fmt.Sprintf("-%s, ", o.ShortName()) + } + names += fmt.Sprintf("--%s", o.Name()) + return names +} + +func HelpLine(o Option, paddedWidth int) string { + argNames := "" + if o.ShortName() != "" { + argNames += fmt.Sprintf("-%s, ", o.ShortName()) + } + argNames += fmt.Sprintf("--%s", o.Name()) + + spaceSize := paddedWidth - len(argNames) + if spaceSize > 0 { + argNames += fmt.Sprintf("%*s", spaceSize, "") + } + + return argNames + " " + o.Description() +} diff --git a/flags/set.go b/opts/set.go similarity index 79% rename from flags/set.go rename to opts/set.go index 60438e3..f3daae4 100644 --- a/flags/set.go +++ b/opts/set.go @@ -1,16 +1,16 @@ -package flags +package opts -type Set []Flag +type Set []Option func NewSet() Set { return Set{} } -func (s *Set) Add(f Flag) { +func (s *Set) Add(f Option) { *s = append(*s, f) } -func (s Set) Get(name string) (Flag, bool) { +func (s Set) Get(name string) (Option, bool) { for _, f := range s { if f.Name() == name { return f, true diff --git a/opts/string.go b/opts/string.go new file mode 100644 index 0000000..b5e2b4c --- /dev/null +++ b/opts/string.go @@ -0,0 +1,48 @@ +package opts + +type StringOption struct { + name string + shortName string + description string + value string +} + +var _ Option = (*StringOption)(nil) + +func String(name, shortName, defaultValue, description string) *StringOption { + return &StringOption{ + name: name, + shortName: shortName, + description: description, + value: defaultValue, + } +} + +// Description implements Option. +func (o *StringOption) Description() string { + return o.description +} + +// Name implements Option. +func (o *StringOption) Name() string { + return o.name +} + +// ShortName implements Option. +func (o *StringOption) ShortName() string { + return o.shortName +} + +// Value implements Option. +func (o *StringOption) Parse(raw string) error { + if raw == "" { + return nil + } + + o.value = raw + return nil +} + +func (o *StringOption) Value() string { + return o.value +}