Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit d27bbea

Browse files
committed
Fix side-effects of checking type of argument
1 parent 3e66d14 commit d27bbea

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

internals/cli/argument.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Argument struct {
1414
}
1515

1616
type ArgValue interface {
17+
String() string
1718
Set(string) error
1819
}
1920

@@ -46,30 +47,49 @@ func (s *StringValue) Set(replacer string) error {
4647
return nil
4748
}
4849

50+
func (s *StringValue) String() string {
51+
return s.Value
52+
}
53+
4954
type StringListValue []string
5055

5156
func (s *StringListValue) Set(replacer string) error {
5257
*s = append(*s, replacer)
5358
return nil
5459
}
5560

61+
func (s *StringListValue) String() string {
62+
return "StringListValue"
63+
}
64+
5665
type URLValue struct {
5766
*url.URL
5867
}
5968

60-
func (s *URLValue) Set(replacer string) error {
69+
func (s URLValue) Set(replacer string) error {
6170
var err error
6271
s.URL, err = url.Parse(replacer)
6372
return err
6473
}
6574

75+
func (s *URLValue) String() string {
76+
if s.URL == nil {
77+
return ""
78+
}
79+
return s.URL.String()
80+
}
81+
6682
type ByteValue []byte
6783

6884
func (s *ByteValue) Set(replacer string) error {
6985
*s = []byte(replacer)
7086
return nil
7187
}
7288

89+
func (s *ByteValue) String() string {
90+
return string(*s)
91+
}
92+
7393
func getRequired(params []Argument) int {
7494
required := 0
7595
for _, arg := range params {

internals/cli/error_handler.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package cli
22

33
import (
44
"fmt"
5-
"reflect"
6-
"strings"
75
)
86

97
func (c *CommandClause) argumentError(args []string) error {
@@ -12,7 +10,7 @@ func (c *CommandClause) argumentError(args []string) error {
1210
}
1311
errorText, minimum, maximum := "", getRequired(c.Args), len(c.Args)
1412

15-
if strings.Contains(reflect.TypeOf(c.Args[0].Value).String(), "List") {
13+
if c.Args[0].Value.String() == new(StringListValue).String() {
1614
errorText += fmt.Sprintf(`"%s" requires at least %d %s.`, c.fullCommand(), minimum, pluralize("argument", minimum))
1715
} else if minimum == maximum {
1816
errorText += fmt.Sprintf(`"%s" requires exactly %d %s.`, c.fullCommand(), minimum, pluralize("argument", minimum))

0 commit comments

Comments
 (0)