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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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