Skip to content

Commit

Permalink
only add flags to parsed list if they ended up setting a value. all t…
Browse files Browse the repository at this point in the history
…ests passing
  • Loading branch information
integrii committed Nov 6, 2019
1 parent 1239754 commit 29183fd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
14 changes: 9 additions & 5 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"errors"
"fmt"
"os"
"strconv"

"text/template"
)

// Parser represents the set of vars and subcommands we are expecting
// from our input args, and the parser than handles them all.
// Parser represents the set of flags and subcommands we are expecting
// from our input arguments. Parser is the top level struct responsible for
// parsing an entire set of subcommands and flags.
type Parser struct {
Subcommand
Version string // the optional version of the parser.
Expand Down Expand Up @@ -60,11 +62,11 @@ func (p *Parser) ParseArgs(args []string) error {
argsNotParsed := findArgsNotInParsedValues(args, parsedValues)
if len(argsNotParsed) > 0 {
// flatten out unused args for our error message
var argsNotParedFlat string
var argsNotParsedFlat string
for _, a := range argsNotParsed {
argsNotParedFlat = argsNotParedFlat + " " + a
argsNotParsedFlat = argsNotParsedFlat + " " + a
}
p.ShowHelpAndExit("Unknown arguments supplied: " + argsNotParedFlat)
p.ShowHelpAndExit("Unknown arguments supplied: " + argsNotParsedFlat)
}
}

Expand Down Expand Up @@ -103,7 +105,9 @@ func findArgsNotInParsedValues(args []string, parsedValues []parsedValue) []stri
for _, pv := range parsedValues {
// this argumenet 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) {
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
// value that was not blank, we skip the next value in the argument list
Expand Down
23 changes: 14 additions & 9 deletions subCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func NewSubcommand(name string) *Subcommand {
// requested, and any errors found during parsing
func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string, bool, error) {

var err error
var positionalOnlyArguments []string
var helpRequested bool // indicates the user has supplied -h and we
// should render help if we are the last subcommand
Expand Down Expand Up @@ -130,7 +129,7 @@ func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string,
positionalOnlyArguments = append(positionalOnlyArguments, a)
// track this as a parsed value with the subcommand
sc.addParsedPositionalValue(a)
case argIsFlagWithSpace:
case argIsFlagWithSpace: // a flag with a space. ex) -k v or --key value
a = parseFlagToName(a)

// debugPrint("Arg", i, "is flag with space:", a)
Expand All @@ -140,7 +139,7 @@ func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string,
if flagIsBool(sc, p, a) {
debugPrint(sc.Name, "bool flag", a, "next var is:", nextArg)
// set the value in this subcommand and its root parser
_, err = setValueForParsers(a, "true", p, sc)
valueSet, err := setValueForParsers(a, "true", p, sc)

// if an error occurs, just return it and quit parsing
if err != nil {
Expand All @@ -149,7 +148,9 @@ func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string,

// log all values parsed by this subcommand. We leave the value blank
// because the bool value had no explicit true or false supplied
sc.addParsedFlag(a, "")
if valueSet {
sc.addParsedFlag(a, "")
}

// we've found and set a standalone bool flag, so we move on to the next
// argument in the list of arguments
Expand All @@ -164,28 +165,32 @@ func (sc *Subcommand) parseAllFlagsFromArgs(p *Parser, args []string) ([]string,
p.ShowHelpWithMessage("Expected a following arg for flag " + a + ", but it did not exist.")
exitOrPanic(2)
}
_, err = setValueForParsers(a, nextArg, p, sc)
valueSet, err := setValueForParsers(a, nextArg, p, sc)
if err != nil {
return []string{}, false, err
}

// log all parsed values in the subcommand
sc.addParsedFlag(a, nextArg)
case argIsFlagWithValue:
if valueSet {
sc.addParsedFlag(a, nextArg)
}
case argIsFlagWithValue: // a flag with an equals sign. ex) -k=v or --key=value
// debugPrint("Arg", i, "is flag with value:", a)
a = parseFlagToName(a)

// parse flag into key and value and apply to subcommand flags
key, val := parseArgWithValue(a)

// set the value in this subcommand and its root parser
_, err := setValueForParsers(key, val, p, sc)
valueSet, err := setValueForParsers(key, val, p, sc)
if err != nil {
return []string{}, false, err
}

// log all values parsed by the subcommand
sc.addParsedFlag(key, val)
if valueSet {
sc.addParsedFlag(a, val)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions subcommand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ func TestExitOnUnknownFlag(t *testing.T) {
// is supplied and the ShowHelpOnUnexpected value is set, an error is thrown on
// the unknown flags.
func TestExitOnUnknownFlagWithValue(t *testing.T) {
flaggy.ResetParser()
flaggy.ShowHelpOnUnexpectedEnable()
defer func() {
r := recover()
if r == nil {
Expand Down

0 comments on commit 29183fd

Please sign in to comment.