Skip to content

Commit

Permalink
Handle "version" shorthand
Browse files Browse the repository at this point in the history
-v has special meaning for Cobra which it uses to print version. Don't
convert it to --v
  • Loading branch information
kislaykishore committed Jun 18, 2024
1 parent cd63292 commit 640881b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ func logPanic() {

// convertToPosixArgs converts a slice of commandline args and transforms them
// into POSIX compliant args. All it does is that it converts flags specified
// using a single-hyphen to double-hyphens.
// using a single-hyphen to double-hyphens. We are excluding "-v" because it's
// reserved for showing version in Cobra.
func convertToPosixArgs(args []string) []string {
pArgs := make([]string, 0, len(args))
for _, a := range args {
if strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") {
if strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") && a != "-v" {
pArgs = append(pArgs, "-"+a)
} else {
pArgs = append(pArgs, a)
Expand Down
5 changes: 5 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func TestPosixFlagsConversion(t *testing.T) {
input: []string{"abc", "--flag=\"-test\""},
expected: []string{"abc", "--flag=\"-test\""},
},
{
name: "Version shorthand stays unchanged.",
input: []string{"abc", "-v"},
expected: []string{"abc", "-v"},
},
}
for _, tc := range data {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit 640881b

Please sign in to comment.