Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support user defined slice flag value separator #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 14 additions & 9 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

// Flag holds the base methods for all flag types
type Flag struct {
ShortName string
LongName string
Description string
rawValue string // the value as a string before being parsed
Hidden bool // indicates this flag should be hidden from help and suggestions
AssignmentVar interface{}
defaultValue string // the value (as a string), that was set by default before any parsing and assignment
parsed bool // indicates that this flag has already been parsed
ShortName string
LongName string
Description string
rawValue string // the value as a string before being parsed
Hidden bool // indicates this flag should be hidden from help and suggestions
AssignmentVar interface{}
ValueSeparator string
defaultValue string // the value (as a string), that was set by default before any parsing and assignment
parsed bool // indicates that this flag has already been parsed
}

// HasName indicates that this flag's short or long name matches the
Expand Down Expand Up @@ -62,7 +63,11 @@ func (f *Flag) identifyAndAssignValue(value string) error {
*v = value
case *[]string:
v := f.AssignmentVar.(*[]string)
splitString := strings.Split(value, ",")
sep := `,`
if f.ValueSeparator != `` {
sep = f.ValueSeparator
}
splitString := strings.Split(value, sep)
new := append(*v, splitString...)
*v = new
case *bool:
Expand Down
11 changes: 11 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func TestInputParsing(t *testing.T) {
inputArgs = append(inputArgs, "-ssf", "one", "-ssf", "two")
var stringSliceFlagExpected = []string{"one", "two"}

var stringSlicePipeFlag []string
StringSlice(&stringSlicePipeFlag, "sspf", "stringSlice1", "string slice flag", `|`)
inputArgs = append(inputArgs, "-sspf", "one|two|three")
var stringSlicePipeFlagExpected = []string{"one", "two", "three"}

var boolFlag bool
Bool(&boolFlag, "bf", "bool", "bool flag")
inputArgs = append(inputArgs, "-bf")
Expand Down Expand Up @@ -306,6 +311,12 @@ func TestInputParsing(t *testing.T) {
}
}

for i, f := range stringSlicePipeFlagExpected {
if stringSlicePipeFlag[i] != f {
t.Fatal("stringSlice value incorrect", stringSlicePipeFlag[i], f)
}
}

if boolFlag != boolFlagExpected {
t.Fatal("bool flag incorrect", boolFlag, boolFlagExpected)
}
Expand Down
4 changes: 2 additions & 2 deletions flaggy.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func String(assignmentVar *string, shortName string, longName string, descriptio

// StringSlice adds a new slice of strings flag
// Specify the flag multiple times to fill the slice
func StringSlice(assignmentVar *[]string, shortName string, longName string, description string) {
DefaultParser.add(assignmentVar, shortName, longName, description)
func StringSlice(assignmentVar *[]string, shortName, longName, description string, separator ...string) {
DefaultParser.add(assignmentVar, shortName, longName, description, separator...)
}

// Bool adds a new bool flag
Expand Down
10 changes: 5 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"strconv"
"strings"

"text/template"
)

Expand Down Expand Up @@ -94,7 +93,8 @@ func findArgsNotInParsedValues(args []string, parsedValues []parsedValue) []stri

// if the final argument (--) is seen, then we stop checking because all
// further values are trailing arguments.
if determineArgType(a) == argIsFinal {
argType := determineArgType(a)
if argType == argIsFinal {
return argsNotUsed
}

Expand Down Expand Up @@ -122,10 +122,10 @@ func findArgsNotInParsedValues(args []string, parsedValues []parsedValue) []stri

// search all args for a corresponding parsed value
for _, pv := range parsedValues {
// this argumenet was a key
// this argument was a key
// debugPrint(pv.Key, "==", arg)
debugPrint(pv.Key + "==" + arg + " || (" + strconv.FormatBool(pv.IsPositional) + " && " + pv.Value + " == " + arg + ")")
if pv.Key == arg || (pv.IsPositional && pv.Value == arg) {
if pv.Key == arg || (argType == argIsPositional && pv.IsPositional && pv.Value == arg) {
debugPrint("Found matching parsed arg for " + pv.Key)
foundArgUsed = true // the arg was used in this parsedValues set
// if the value is not a positional value and the parsed value had a
Expand All @@ -145,7 +145,7 @@ func findArgsNotInParsedValues(args []string, parsedValues []parsedValue) []stri
// if the arg was not used in any parsed values, then we add it to the slice
// of arguments not used
if !foundArgUsed {
argsNotUsed = append(argsNotUsed, arg)
argsNotUsed = append(argsNotUsed, a)
}
}

Expand Down
11 changes: 7 additions & 4 deletions subCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string,

// if the next arg was not found, then show a Help message
if !nextArgExists {
p.ShowHelpWithMessage("Expected a following arg for flag " + a + ", but it did not exist.")
p.ShowHelpWithMessage("Expected a following arg for flag " + args[i] + ", but it did not exist.")
exitOrPanic(2)
}
valueSet, err := setValueForParsers(a, nextArg, p, sc)
Expand Down Expand Up @@ -430,7 +430,7 @@ func (sc *Subcommand) AttachSubcommand(newSC *Subcommand, relativePosition int)
// add is a "generic" to add flags of any type. Checks the supplied parent
// parser to ensure that the user isn't setting version or help flags that
// conflict with the built-in help and version flag behavior.
func (sc *Subcommand) add(assignmentVar interface{}, shortName string, longName string, description string) {
func (sc *Subcommand) add(assignmentVar interface{}, shortName, longName, description string, separator ...string) {

// if the flag is already used, throw an error
for _, existingFlag := range sc.Flags {
Expand All @@ -448,6 +448,9 @@ func (sc *Subcommand) add(assignmentVar interface{}, shortName string, longName
LongName: longName,
Description: description,
}
if len(separator) > 0 {
newFlag.ValueSeparator = separator[0]
}
sc.Flags = append(sc.Flags, &newFlag)
}

Expand All @@ -458,8 +461,8 @@ func (sc *Subcommand) String(assignmentVar *string, shortName string, longName s

// StringSlice adds a new slice of strings flag
// Specify the flag multiple times to fill the slice
func (sc *Subcommand) StringSlice(assignmentVar *[]string, shortName string, longName string, description string) {
sc.add(assignmentVar, shortName, longName, description)
func (sc *Subcommand) StringSlice(assignmentVar *[]string, shortName, longName, description string, separator ...string) {
sc.add(assignmentVar, shortName, longName, description, separator...)
}

// Bool adds a new bool flag
Expand Down
10 changes: 10 additions & 0 deletions subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ func TestSCInputParsing(t *testing.T) {
inputArgs = append(inputArgs, "-sscf", "one,two")
var stringSliceCommaFlagExpected = []string{"one", "two"}

var stringSlicePipeFlag []string
sc.StringSlice(&stringSlicePipeFlag, "sspf", "stringSlice1", "string slice flag", `|`)
inputArgs = append(inputArgs, "-sspf", "one|two|three")
var stringSlicePipeFlagExpected = []string{"one", "two", "three"}

var boolFlag bool
sc.Bool(&boolFlag, "bf", "bool", "bool flag")
inputArgs = append(inputArgs, "-bf")
Expand Down Expand Up @@ -601,6 +606,11 @@ func TestSCInputParsing(t *testing.T) {
t.Fatal("stringSlice value incorrect", stringSliceCommaFlag[i], f)
}
}
for i, f := range stringSlicePipeFlagExpected {
if stringSlicePipeFlag[i] != f {
t.Fatal("stringSlice value incorrect", stringSlicePipeFlag[i], f)
}
}

if boolFlag != boolFlagExpected {
t.Fatal("bool flag incorrect", boolFlag, boolFlagExpected)
Expand Down