Rename opts package to opt

This commit is contained in:
Evan Fiordeliso 2023-11-12 17:19:34 -05:00
parent 306443ae81
commit 9e6ca2ce76
12 changed files with 105 additions and 105 deletions

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"go.fifitido.net/cmd/opts" "go.fifitido.net/cmd/opt"
) )
type Command struct { type Command struct {
@ -14,7 +14,7 @@ type Command struct {
longDescription string longDescription string
aliases []string aliases []string
arguments []*Argument arguments []*Argument
opts opts.Set opts opt.Set
subcommands Set subcommands Set
parent *Command parent *Command
run func(args []string) run func(args []string)
@ -68,7 +68,7 @@ func (c *Command) Execute(args []string) {
args = args[1:] args = args[1:]
} }
parser := opts.NewParser(args, c.opts, false) parser := opt.NewParser(args, c.opts, false)
restArgs, err := parser.Parse() restArgs, err := parser.Parse()
if err != nil { if err != nil {
fmt.Println(err.Error()) 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() { if ok && helpOpt.Value() {
c.ShowHelp() c.ShowHelp()
return return

View File

@ -3,7 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"go.fifitido.net/cmd/opts" "go.fifitido.net/cmd/opt"
) )
func (c *Command) ShowHelp() { func (c *Command) ShowHelp() {
@ -43,16 +43,16 @@ func (c *Command) ShowHelp() {
paddedWidth := c.opts.MaxWidth() paddedWidth := c.opts.MaxWidth()
for _, f := range c.opts { 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 { if len(globalOpts) > 0 {
paddedWidth = globalOpts.MaxWidth() paddedWidth = globalOpts.MaxWidth()
fmt.Println() fmt.Println()
fmt.Println("Global options:") fmt.Println("Global options:")
for _, f := range globalOpts { for _, f := range globalOpts {
fmt.Println(" " + opts.HelpLine(f, paddedWidth)) fmt.Println(" " + opt.HelpLine(f, paddedWidth))
} }
} }

View File

@ -1,4 +1,4 @@
package opts package opt
import ( import (
"strconv" "strconv"

View File

@ -1,4 +1,4 @@
package opts package opt
import "strconv" import "strconv"

View File

@ -1,4 +1,4 @@
package opts package opt
var globalOpts = Set{ var globalOpts = Set{
Bool("help", "h", false, "Show the help menu"), Bool("help", "h", false, "Show the help menu"),

View File

@ -1,4 +1,4 @@
package opts package opt
import "strconv" import "strconv"

View File

@ -1,4 +1,4 @@
package opts package opt
import "fmt" import "fmt"

View File

@ -1,4 +1,4 @@
package opts package opt
import ( import (
"errors" "errors"

View File

@ -1,15 +1,15 @@
package opts_test package opt_test
import ( import (
"testing" "testing"
"go.fifitido.net/cmd/opts" "go.fifitido.net/cmd/opt"
) )
func TestParseUnknownLongOption(t *testing.T) { func TestParseUnknownLongOption(t *testing.T) {
set := opts.Set{} set := opt.Set{}
args := []string{"--unknown"} args := []string{"--unknown"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -19,9 +19,9 @@ func TestParseUnknownLongOption(t *testing.T) {
} }
func TestParseUnknownShortOption(t *testing.T) { func TestParseUnknownShortOption(t *testing.T) {
set := opts.Set{} set := opt.Set{}
args := []string{"-u"} args := []string{"-u"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -31,12 +31,12 @@ func TestParseUnknownShortOption(t *testing.T) {
} }
func TestParseUnknownShortChainedOption1(t *testing.T) { func TestParseUnknownShortChainedOption1(t *testing.T) {
set := opts.Set{ set := opt.Set{
opts.Bool("banana", "b", false, ""), opt.Bool("banana", "b", false, ""),
opts.Bool("cucumber", "c", false, ""), opt.Bool("cucumber", "c", false, ""),
} }
args := []string{"-abc"} args := []string{"-abc"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -46,12 +46,12 @@ func TestParseUnknownShortChainedOption1(t *testing.T) {
} }
func TestParseUnknownShortChainedOption2(t *testing.T) { func TestParseUnknownShortChainedOption2(t *testing.T) {
set := opts.Set{ set := opt.Set{
opts.Bool("apple", "a", false, ""), opt.Bool("apple", "a", false, ""),
opts.Bool("cucumber", "c", false, ""), opt.Bool("cucumber", "c", false, ""),
} }
args := []string{"-abc"} args := []string{"-abc"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -61,12 +61,12 @@ func TestParseUnknownShortChainedOption2(t *testing.T) {
} }
func TestParseUnknownShortChainedOption3(t *testing.T) { func TestParseUnknownShortChainedOption3(t *testing.T) {
set := opts.Set{ set := opt.Set{
opts.Bool("apple", "a", false, ""), opt.Bool("apple", "a", false, ""),
opts.Bool("banana", "b", false, ""), opt.Bool("banana", "b", false, ""),
} }
args := []string{"-abc"} args := []string{"-abc"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -76,12 +76,12 @@ func TestParseUnknownShortChainedOption3(t *testing.T) {
} }
func TestParseUnchaninableOption(t *testing.T) { func TestParseUnchaninableOption(t *testing.T) {
set := opts.Set{ set := opt.Set{
opts.Bool("apple", "a", false, ""), opt.Bool("apple", "a", false, ""),
opts.String("banana", "b", "", ""), opt.String("banana", "b", "", ""),
} }
args := []string{"-ab"} args := []string{"-ab"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -91,92 +91,92 @@ func TestParseUnchaninableOption(t *testing.T) {
} }
func TestParseShortOptionWithValueAndNoSpace(t *testing.T) { func TestParseShortOptionWithValueAndNoSpace(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-fapple"} args := []string{"-fapple"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
} }
func TestParseShortOptionWithValueAndSpace(t *testing.T) { func TestParseShortOptionWithValueAndSpace(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-f", "apple"} args := []string{"-f", "apple"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
} }
func TestParseShortOptionWithValueAndEqual(t *testing.T) { func TestParseShortOptionWithValueAndEqual(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-f=apple"} args := []string{"-f=apple"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
} }
func TestParseLongOptionWithValueAndEqual(t *testing.T) { func TestParseLongOptionWithValueAndEqual(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"--fruit=apple"} args := []string{"--fruit=apple"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
} }
func TestParseLongOptionWithValueAndSpace(t *testing.T) { func TestParseLongOptionWithValueAndSpace(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"--fruit", "apple"} args := []string{"--fruit", "apple"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
} }
func TestParseOptionTerminator(t *testing.T) { func TestParseOptionTerminator(t *testing.T) {
opt := opts.String("fruit", "f", "banana", "") o := opt.String("fruit", "f", "banana", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"--fruit", "apple", "--", "hello", "world"} args := []string{"--fruit", "apple", "--", "hello", "world"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
restArgs, err := parser.Parse() restArgs, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "apple" { if o.Value() != "apple" {
t.Errorf("Expected fruit to be 'apple', got: %s", opt.Value()) t.Errorf("Expected fruit to be 'apple', got: %s", o.Value())
} }
if len(restArgs) != 2 { if len(restArgs) != 2 {
@ -193,9 +193,9 @@ func TestParseOptionTerminator(t *testing.T) {
} }
func TestParseLongOptionIgnoreUnknown(t *testing.T) { func TestParseLongOptionIgnoreUnknown(t *testing.T) {
set := opts.Set{} set := opt.Set{}
args := []string{"--unknown", "hello", "world"} args := []string{"--unknown", "hello", "world"}
parser := opts.NewParser(args, set, true) parser := opt.NewParser(args, set, true)
restArgs, err := parser.Parse() restArgs, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
@ -215,9 +215,9 @@ func TestParseLongOptionIgnoreUnknown(t *testing.T) {
} }
func TestParseShortOptionIgnoreUnknown(t *testing.T) { func TestParseShortOptionIgnoreUnknown(t *testing.T) {
set := opts.Set{} set := opt.Set{}
args := []string{"-q"} args := []string{"-q"}
parser := opts.NewParser(args, set, true) parser := opt.NewParser(args, set, true)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
@ -226,12 +226,12 @@ func TestParseShortOptionIgnoreUnknown(t *testing.T) {
} }
func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) { func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) {
apple := opts.Bool("apple", "a", false, "") apple := opt.Bool("apple", "a", false, "")
banana := opts.Bool("banana", "b", false, "") banana := opt.Bool("banana", "b", false, "")
cucumber := opts.Bool("cucumber", "c", false, "") cucumber := opt.Bool("cucumber", "c", false, "")
set := opts.Set{apple, banana, cucumber} set := opt.Set{apple, banana, cucumber}
args := []string{"-adc"} args := []string{"-adc"}
parser := opts.NewParser(args, set, true) parser := opt.NewParser(args, set, true)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
@ -252,25 +252,25 @@ func TestParseChainedShortOptionIgnoreUnknown(t *testing.T) {
} }
func TestParseSingleDashValue(t *testing.T) { func TestParseSingleDashValue(t *testing.T) {
opt := opts.String("fruit", "f", "", "") o := opt.String("fruit", "f", "", "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-f-"} args := []string{"-f-"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err != nil { if err != nil {
t.Errorf("Expected no error, got: %s", err.Error()) t.Errorf("Expected no error, got: %s", err.Error())
} }
if opt.Value() != "-" { if o.Value() != "-" {
t.Errorf("Expected fruit to be '-', got: %s", opt.Value()) t.Errorf("Expected fruit to be '-', got: %s", o.Value())
} }
} }
func TestParseLongOptionBadValue(t *testing.T) { func TestParseLongOptionBadValue(t *testing.T) {
opt := opts.Int("fruit", "f", 0, "") o := opt.Int("fruit", "f", 0, "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"--fruit=five"} args := []string{"--fruit=five"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -278,10 +278,10 @@ func TestParseLongOptionBadValue(t *testing.T) {
} }
func TestParseShortOptionWithSpaceBadValue(t *testing.T) { func TestParseShortOptionWithSpaceBadValue(t *testing.T) {
opt := opts.Int("fruit", "f", 0, "") o := opt.Int("fruit", "f", 0, "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-f", "five"} args := []string{"-f", "five"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -289,11 +289,11 @@ func TestParseShortOptionWithSpaceBadValue(t *testing.T) {
} }
func TestParseShortOptionWithoutEqualBadValue(t *testing.T) { func TestParseShortOptionWithoutEqualBadValue(t *testing.T) {
optG := opts.Bool("green", "g", false, "") og := opt.Bool("green", "g", false, "")
optF := opts.Int("fruit", "f", 0, "") of := opt.Int("fruit", "f", 0, "")
set := opts.Set{optG, optF} set := opt.Set{og, of}
args := []string{"-ffive"} args := []string{"-ffive"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -301,11 +301,11 @@ func TestParseShortOptionWithoutEqualBadValue(t *testing.T) {
} }
func TestParseShortOptionWithEqualBadValue(t *testing.T) { func TestParseShortOptionWithEqualBadValue(t *testing.T) {
optG := opts.Bool("green", "g", false, "") og := opt.Bool("green", "g", false, "")
optF := opts.Int("fruit", "f", 0, "") of := opt.Int("fruit", "f", 0, "")
set := opts.Set{optG, optF} set := opt.Set{og, of}
args := []string{"-f=five"} args := []string{"-f=five"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -313,10 +313,10 @@ func TestParseShortOptionWithEqualBadValue(t *testing.T) {
} }
func TestParseLongOptionMissingValue(t *testing.T) { func TestParseLongOptionMissingValue(t *testing.T) {
opt := opts.Float("fruit-price", "f", 0, "") o := opt.Float("fruit-price", "f", 0, "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"--fruit-price"} args := []string{"--fruit-price"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")
@ -324,10 +324,10 @@ func TestParseLongOptionMissingValue(t *testing.T) {
} }
func TestParseShortOptionMissingValue(t *testing.T) { func TestParseShortOptionMissingValue(t *testing.T) {
opt := opts.Float("fruit-price", "f", 0, "") o := opt.Float("fruit-price", "f", 0, "")
set := opts.Set{opt} set := opt.Set{o}
args := []string{"-f"} args := []string{"-f"}
parser := opts.NewParser(args, set, false) parser := opt.NewParser(args, set, false)
_, err := parser.Parse() _, err := parser.Parse()
if err == nil { if err == nil {
t.Error("Expected error") t.Error("Expected error")

View File

@ -1,4 +1,4 @@
package opts package opt
type Set []Option type Set []Option

View File

@ -1,4 +1,4 @@
package opts package opt
type StringOption struct { type StringOption struct {
name string name string

View File

@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"go.fifitido.net/cmd/opts" "go.fifitido.net/cmd/opt"
) )
type Option func(*Command) 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) { return func(c *Command) {
c.opts = append(c.opts, f) c.opts = append(c.opts, f)
} }
} }
func WithOptions(os ...opts.Option) Option { func WithOptions(os ...opt.Option) Option {
return func(c *Command) { return func(c *Command) {
c.opts = append(c.opts, os...) c.opts = append(c.opts, os...)
} }