Skip to content

Commit

Permalink
updated flags.go, flags_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
idsulik committed Jun 24, 2024
1 parent 97fd236 commit c20a48d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
43 changes: 28 additions & 15 deletions cmd/kubens/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,40 @@ func parseArgs(argv []string) Op {
return ListOp{}
}

if n == 1 || n == 2 {
if n == 1 {
v := argv[0]
force := false

if n == 2 {
force = slices.Contains([]string{"-f", "--force"}, argv[1])
}

if v == "--help" || v == "-h" {
switch v {
case "--help", "-h":
return HelpOp{}
}
if v == "--version" || v == "-V" {
case "--version", "-V":
return VersionOp{}
}
if v == "--current" || v == "-c" {
case "--current", "-c":
return CurrentOp{}
default:
return getSwitchOp(v, false)
}
if strings.HasPrefix(v, "-") && v != "-" {
return UnsupportedOp{Err: fmt.Errorf("unsupported option '%s'", v)}
} else if n == 2 {
force := false
name := ""
if slices.Contains([]string{"-f", "--force"}, argv[0]) {
force = true
name = argv[1]
} else if slices.Contains([]string{"-f", "--force"}, argv[1]) {
force = true
name = argv[0]
} else {
return UnsupportedOp{Err: fmt.Errorf("unsupported arguments %q", argv)}
}
return SwitchOp{Target: argv[0], Force: force}

return getSwitchOp(name, force)
}

return UnsupportedOp{Err: fmt.Errorf("too many arguments")}
}

func getSwitchOp(v string, force bool) Op {
if strings.HasPrefix(v, "-") && v != "-" {
return UnsupportedOp{Err: fmt.Errorf("unsupported option %q", v)}
}
return SwitchOp{Target: v, Force: force}
}
20 changes: 19 additions & 1 deletion cmd/kubens/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,30 @@ func Test_parseArgs_new(t *testing.T) {
{name: "switch by name",
args: []string{"foo"},
want: SwitchOp{Target: "foo"}},
{name: "switch by name force short flag",
args: []string{"foo", "-f"},
want: SwitchOp{Target: "foo", Force: true}},
{name: "switch by name force long flag",
args: []string{"foo", "--force"},
want: SwitchOp{Target: "foo", Force: true}},
{name: "switch by name force short flag before name",
args: []string{"-f", "foo"},
want: SwitchOp{Target: "foo", Force: true}},
{name: "switch by name force long flag before name",
args: []string{"--force", "foo"},
want: SwitchOp{Target: "foo", Force: true}},
{name: "switch by name unknown arguments",
args: []string{"foo", "-x"},
want: UnsupportedOp{Err: fmt.Errorf("unsupported arguments %q", []string{"foo", "-x"})}},
{name: "switch by name unknown arguments",
args: []string{"-x", "foo"},
want: UnsupportedOp{Err: fmt.Errorf("unsupported arguments %q", []string{"-x", "foo"})}},
{name: "switch by swap",
args: []string{"-"},
want: SwitchOp{Target: "-"}},
{name: "unrecognized flag",
args: []string{"-x"},
want: UnsupportedOp{Err: fmt.Errorf("unsupported option '-x'")}},
want: UnsupportedOp{Err: fmt.Errorf("unsupported option %q", "-x")}},
{name: "too many args",
args: []string{"a", "b", "c"},
want: UnsupportedOp{Err: fmt.Errorf("too many arguments")}},
Expand Down

0 comments on commit c20a48d

Please sign in to comment.