Skip to content

Commit

Permalink
fix edge case on single and double '-', '--'
Browse files Browse the repository at this point in the history
  • Loading branch information
davidovich committed Aug 29, 2020
1 parent d564ec7 commit c566824
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cmd/run.go
Expand Up @@ -56,7 +56,8 @@ func newRunCmd(driver summon.ConfigurableRunner, main *mainCmd) *cobra.Command {
// We are lucky, we know the prefix order of params,
// extract args after the run command [summon run handle]
// see https://github.com/spf13/pflag/pull/160
// and https://github.com/spf13/cobra/issues/739
// https://github.com/spf13/cobra/issues/739
// and https://github.com/spf13/pflag/pull/199
runCmd.args = extractUnknownArgs(cmd.Flags(), osArgs[3:])
return runCmd.run()
}
Expand All @@ -78,7 +79,7 @@ func extractUnknownArgs(flags *pflag.FlagSet, args []string) []string {
for i := 0; i < len(args); i++ {
a := args[i]
var f *pflag.Flag
if a[0] == '-' {
if a[0] == '-' && len(a) > 1 {
if a[1] == '-' {
f = flags.Lookup(strings.SplitN(a[2:], "=", 2)[0])
} else {
Expand Down
6 changes: 6 additions & 0 deletions cmd/run_test.go
Expand Up @@ -109,6 +109,12 @@ func TestExtractUnknownArgs(t *testing.T) {
unknown := extractUnknownArgs(fset, []string{"--json", "{}", "--unknown"})
assert.Equal(t, []string{"--unknown"}, unknown)

unknown = extractUnknownArgs(fset, []string{"--"})
assert.Equal(t, []string{"--"}, unknown)

unknownShort := extractUnknownArgs(fset, []string{"-j", "--unknown"})
assert.Equal(t, []string{"--unknown"}, unknownShort)

unknownShort = extractUnknownArgs(fset, []string{"-"})
assert.Equal(t, []string{"-"}, unknownShort)
}

0 comments on commit c566824

Please sign in to comment.