From c20a48d5a947954cd1bcf4824f75cb8648bee394 Mon Sep 17 00:00:00 2001 From: Suleiman Dibirov Date: Mon, 24 Jun 2024 21:09:30 +0300 Subject: [PATCH] updated flags.go, flags_test.go --- cmd/kubens/flags.go | 43 ++++++++++++++++++++++++++-------------- cmd/kubens/flags_test.go | 20 ++++++++++++++++++- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/cmd/kubens/flags.go b/cmd/kubens/flags.go index 3656fd97..b46fce9b 100644 --- a/cmd/kubens/flags.go +++ b/cmd/kubens/flags.go @@ -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} +} diff --git a/cmd/kubens/flags_test.go b/cmd/kubens/flags_test.go index e3e75516..19102a10 100644 --- a/cmd/kubens/flags_test.go +++ b/cmd/kubens/flags_test.go @@ -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")}},