Skip to content

Commit

Permalink
Deal with repeated arguments
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola committed Nov 8, 2022
1 parent ff13cf6 commit 1762ac2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cmd/k3s/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,17 @@ func findDataDir() string {
// findPreferBundledBin searches for prefer-bundled-bin from the CLI args and config file.
func findPreferBundledBin(args []string) bool {
preferRes := configfilearg.MustFindString(args, "prefer-bundled-bin")
flagName := "--prefer-bundled-bin"
for i, arg := range args {
flagName := "--prefer-bundled-bin"
if flagName == arg {
if len(args) > i+1 {
if res, err := strconv.ParseBool(args[i+1]); err == nil {
return res
if _, err := strconv.ParseBool(args[i+1]); err == nil {
preferRes = args[i+1]
}
} else {
//Naked flag, assume true
preferRes = "true"
}
//Naked flag, assume true
return true
} else if strings.HasPrefix(arg, flagName+"=") {
preferRes = arg[len(flagName)+1:]
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/k3s/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func Test_UnitFindPreferBundledBin(t *testing.T) {
args: []string{"--abcd", "--prefer-bundled-bin", "false", "--efgh"},
want: false,
},
{
name: "Repeated arguments",
args: []string{"--abcd", "--prefer-bundled-bin", "false", "--prefer-bundled-bin"},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 1762ac2

Please sign in to comment.