Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ figs := figtree.With(Options{Tracking: true, Harvest: 1776, Pollinate: true})
figs.NewString(kDomain, "", "Domain name")
figs.WithValidator(kDomain, figtree.AssureStringLengthGreaterThan(3))
figs.WithValidator(kDomain, figtree.AssureStringHasPrefix("https://"))
figs.WithCallback(kDomain, figree.CallbackAfterVerify, func(value interface{}) error {
figs.WithCallback(kDomain, figtree.CallbackAfterVerify, func(value interface{}) error {
var s string
switch v := value.(type) {
case *string:
Expand All @@ -214,7 +214,7 @@ figs.WithCallback(kDomain, figree.CallbackAfterVerify, func(value interface{}) e
// try connecting to the domain now
return CheckAvailability(s)
})
figs.WithCallback(kDomain, figree.CallbackAfterRead, func(value interface{}) error {
figs.WithCallback(kDomain, figtree.CallbackAfterRead, func(value interface{}) error {
// every time *figs.String(kDomain) is called, run this
var s string
switch v := value.(type) {
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0.10
v2.0.11
24 changes: 22 additions & 2 deletions alias.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
package figtree

func (tree *figTree) WithAlias(name, alias string) {
import (
"flag"
"fmt"
"strings"
)

func (tree *figTree) WithAlias(name, alias string) Plant {
tree.mu.Lock()
defer tree.mu.Unlock()
name = strings.ToLower(name)
alias = strings.ToLower(alias)
if _, exists := tree.aliases[alias]; exists {
return
return tree
}
tree.aliases[alias] = name
ptr, ok := tree.values.Load(name)
if !ok {
fmt.Println("failed to load -" + name + " value")
return tree
}
value, ok := ptr.(*Value)
if !ok {
fmt.Println("failed to cast -" + name + " value")
Comment on lines +20 to +25
Copy link

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Instead of using fmt.Println for error messages, consider using a logging framework (e.g., log.Printf) or propagating an error to ensure consistent error handling in the library.

Suggested change
fmt.Println("failed to load -" + name + " value")
return tree
}
value, ok := ptr.(*Value)
if !ok {
fmt.Println("failed to cast -" + name + " value")
log.Printf("failed to load value for name: %s", name)
return tree
}
value, ok := ptr.(*Value)
if !ok {
log.Printf("failed to cast value for name: %s", name)

Copilot uses AI. Check for mistakes.

return tree
}
flag.Var(value, alias, "Alias of -"+name)
return tree
}
50 changes: 28 additions & 22 deletions alias_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package figtree

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,26 +12,30 @@ func TestWithAlias(t *testing.T) {
const cmdShort, cmdAliasShort, valueShort = "short", "s", "default"

t.Run("basic_usage", func(t *testing.T) {
os.Args = []string{os.Args[0], "-l", t.Name()}
figs := With(Options{Germinate: true, Tracking: false})
figs.NewString(cmdLong, valueLong, usage)
figs.WithAlias(cmdLong, cmdAliasLong)
figs = figs.NewString(cmdLong, valueLong, usage)
figs = figs.WithAlias(cmdLong, cmdAliasLong)
assert.NoError(t, figs.Parse())

assert.Equal(t, valueLong, *figs.String(cmdLong))
assert.Equal(t, valueLong, *figs.String(cmdAliasLong))
assert.NotEqual(t, valueLong, *figs.String(cmdLong))
assert.Equal(t, t.Name(), *figs.String(cmdLong))
assert.NotEqual(t, valueLong, *figs.String(cmdAliasLong))
assert.Equal(t, t.Name(), *figs.String(cmdAliasLong))
figs = nil
})

t.Run("multiple_aliases", func(t *testing.T) {
os.Args = []string{os.Args[0]}
const k, v, u = "name", "yeshua", "the real name of god"
ka1 := "father"
ka2 := "son"
ka3 := "rauch-hokadesch"
figs := With(Options{Germinate: true, Tracking: false})
figs.NewString(k, v, u)
figs.WithAlias(k, ka1)
figs.WithAlias(k, ka2)
figs.WithAlias(k, ka3)
figs = figs.NewString(k, v, u)
figs = figs.WithAlias(k, ka1)
figs = figs.WithAlias(k, ka2)
figs = figs.WithAlias(k, ka3)
assert.NoError(t, figs.Parse())

assert.Equal(t, v, *figs.String(k))
Expand All @@ -41,17 +46,17 @@ func TestWithAlias(t *testing.T) {
})

t.Run("complex_usage", func(t *testing.T) {

os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true, Tracking: false})
// long
figs.NewString(cmdLong, valueLong, usage)
figs.WithAlias(cmdLong, cmdAliasLong)
figs.WithValidator(cmdLong, AssureStringNotEmpty)
figs = figs.NewString(cmdLong, valueLong, usage)
figs = figs.WithAlias(cmdLong, cmdAliasLong)
figs = figs.WithValidator(cmdLong, AssureStringNotEmpty)

// short
figs.NewString(cmdShort, valueShort, usage)
figs.WithAlias(cmdShort, cmdAliasShort)
figs.WithValidator(cmdShort, AssureStringNotEmpty)
figs = figs.NewString(cmdShort, valueShort, usage)
figs = figs.WithAlias(cmdShort, cmdAliasShort)
figs = figs.WithValidator(cmdShort, AssureStringNotEmpty)

assert.NoError(t, figs.Parse())

Expand All @@ -63,24 +68,25 @@ func TestWithAlias(t *testing.T) {
assert.Equal(t, valueShort, *figs.String(cmdAliasShort))

figs = nil

})

t.Run("alias_with_int", func(t *testing.T) {
os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true})
figs.NewInt("count", 42, "usage")
figs.WithAlias("count", "c")
figs = figs.NewInt("count", 42, "usage")
figs = figs.WithAlias("count", "c")
assert.NoError(t, figs.Parse())
assert.Equal(t, 42, *figs.Int("count"))
assert.Equal(t, 42, *figs.Int("c"))
})

t.Run("alias_conflict", func(t *testing.T) {
os.Args = []string{os.Args[0]}
figs := With(Options{Germinate: true})
figs.NewString("one", "value1", "usage")
figs.NewString("two", "value2", "usage")
figs.WithAlias("one", "x")
figs.WithAlias("two", "x") // Should this overwrite or be ignored?
figs = figs.NewString("one", "value1", "usage")
figs = figs.NewString("two", "value2", "usage")
figs = figs.WithAlias("one", "x")
figs = figs.WithAlias("two", "x") // Should this overwrite or be ignored?
assert.NoError(t, figs.Parse())
assert.Equal(t, "value1", *figs.String("x")) // Clarify expected behavior
})
Expand Down
Loading
Loading