From 51259808fff1f7384b556f6e127a090fe0362305 Mon Sep 17 00:00:00 2001 From: Lynn Cyrin Date: Tue, 1 Oct 2019 20:13:04 -0700 Subject: [PATCH] better leading dash handling --- command.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/command.go b/command.go index dcaf2b4..70502e8 100644 --- a/command.go +++ b/command.go @@ -240,8 +240,7 @@ func reorderArgs(commandFlags []Flag, args []string) []string { reorderedArgs = append(reorderedArgs, arg) // checks if this is an arg that should be re-ordered - } else if arg != "-" && strings.HasPrefix(arg, "-") && argIsFlag(commandFlags, arg) { - + } else if argIsFlag(commandFlags, arg) { // we have determined that this is a flag that we should re-order reorderedArgs = append(reorderedArgs, arg) // if this arg does not contain a "=", then the next index may contain the value for this flag @@ -258,8 +257,18 @@ func reorderArgs(commandFlags []Flag, args []string) []string { // argIsFlag checks if an arg is one of our command flags func argIsFlag(commandFlags []Flag, arg string) bool { + // checks if this is just a `-`, and so definitely not a flag + if arg == "-" { + return false + } // this line turns `--flag` into `flag` - arg = strings.Replace(arg, "-", "", -1) + if strings.HasPrefix(arg, "--") { + arg = strings.Replace(arg, "-", "", 2) + } + // this line turns `-flag` into `flag` + if strings.HasPrefix(arg, "-") { + arg = strings.Replace(arg, "-", "", 1) + } // this line turns `flag=value` into `flag` arg = strings.Split(arg, "=")[0] // look through all the flags, to see if the `arg` is one of our flags