Rename flags to options

This commit is contained in:
Evan Fiordeliso 2023-11-11 19:28:58 -05:00
parent 43191d3ea2
commit 9079086626
16 changed files with 288 additions and 286 deletions

View File

@ -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!")

View File

@ -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

View File

@ -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
}

View File

@ -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()
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

24
help.go
View File

@ -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))
}
}

View File

@ -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...)
}
}

56
opts/bool.go Normal file
View File

@ -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
}

56
opts/float.go Normal file
View File

@ -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
}

View File

@ -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
}

56
opts/int.go Normal file
View File

@ -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
}

34
opts/option.go Normal file
View File

@ -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()
}

View File

@ -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

48
opts/string.go Normal file
View File

@ -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
}