Skip to content

Commit

Permalink
feat: addressed pr comments and updated functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
whutchinson98 committed Jul 11, 2022
1 parent cb98230 commit 3159bf5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"fmt"
"os"

"github.com/rog-golang-buddies/internal/utils"
"github.com/rog-golang-buddies/internal/parser"
)

func main() {
commands, flags, err := utils.ParseCliArguments(os.Args[1:])
commands, flags, err := parser.ParseCliArguments(os.Args[1:])

if err != nil {
panic(err)
Expand Down
11 changes: 10 additions & 1 deletion internal/utils/parse_cli.go → internal/parser/parse_cli.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package utils
package parser

import (
"fmt"
Expand Down Expand Up @@ -32,6 +32,11 @@ func ParseCliArguments(arguments []string) ([]string, map[string]interface{}, er
if lastValueWasFlag {
// The current value is also a flag so we just add true to the flags value
flag := strings.ReplaceAll(lastValueFlag, "-", "")

if flag == "" {
return nil, nil, fmt.Errorf("empty flag was passed in")
}

if flags[flag] != nil {
return nil, nil, fmt.Errorf("flag %v was set multiple times", flag)
}
Expand All @@ -42,6 +47,7 @@ func ParseCliArguments(arguments []string) ([]string, map[string]interface{}, er
}
lastValueWasFlag = false
lastValueFlag = ""

} else {
// the last value was not a flag
// if this value is also not a flag the user entered in 2 values for a single flag
Expand All @@ -59,6 +65,9 @@ func ParseCliArguments(arguments []string) ([]string, map[string]interface{}, er

if lastValueWasFlag {
flag := strings.ReplaceAll(lastValueFlag, "-", "")
if flag == "" {
return nil, nil, fmt.Errorf("empty flag was passed in")
}
if flags[flag] != nil {
return nil, nil, fmt.Errorf("flag %v was set multiple times", flag)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package utils_test
package parser

import (
"testing"

"github.com/rog-golang-buddies/internal/utils"
)

func TestParseCliArguments(t *testing.T) {

t.Run("Correctly gets commands", func(t *testing.T) {
commands, _, _ := utils.ParseCliArguments([]string{"test_command1", "test_command2"})
commands, _, _ := ParseCliArguments([]string{"test_command1", "test_command2"})

if len(commands) != 2 {
t.Fatalf("Invalid command length. Expected %v got %v\n", 2, len(commands))
Expand All @@ -25,7 +23,7 @@ func TestParseCliArguments(t *testing.T) {
})

t.Run("Correctly creates flags that have values", func(t *testing.T) {
_, flags, _ := utils.ParseCliArguments([]string{"test_command1", "--foo", "bar", "--baz", "foo"})
_, flags, _ := ParseCliArguments([]string{"test_command1", "--foo", "bar", "--baz", "foo"})

if len(flags) != 2 {
t.Fatalf("Invalid flags length. Expected %v got %v\n", 2, len(flags))
Expand All @@ -41,7 +39,7 @@ func TestParseCliArguments(t *testing.T) {
})

t.Run("Correctly creates flags that are booleans", func(t *testing.T) {
_, flags, _ := utils.ParseCliArguments([]string{"test_command1", "--foo", "--baz", "foo"})
_, flags, _ := ParseCliArguments([]string{"test_command1", "--foo", "--baz", "foo"})

if len(flags) != 2 {
t.Fatalf("Invalid flags length. Expected %v got %v\n", 2, len(flags))
Expand All @@ -56,7 +54,7 @@ func TestParseCliArguments(t *testing.T) {
}
})
t.Run("Correctly errors if you enter too many values for a given flag", func(t *testing.T) {
_, _, err := utils.ParseCliArguments([]string{"test_command1", "--foo", "value1", "value2", "--bar"})
_, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "value2", "--bar"})

if err == nil {
t.Fatalf("Error was expected but not received")
Expand All @@ -67,7 +65,7 @@ func TestParseCliArguments(t *testing.T) {
}
})
t.Run("Correctly errors if you try to pass in the same flag twice", func(t *testing.T) {
_, _, err := utils.ParseCliArguments([]string{"test_command1", "--foo", "value1", "--foo", "--bar"})
_, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "--foo", "--bar"})

if err == nil {
t.Fatalf("Error was expected but not received")
Expand All @@ -78,7 +76,7 @@ func TestParseCliArguments(t *testing.T) {
}
})
t.Run("Correctly errors if you try to pass in the same flag twice, duplicate flag is the last value", func(t *testing.T) {
_, _, err := utils.ParseCliArguments([]string{"test_command1", "--foo", "value1", "--bar", "--foo"})
_, _, err := ParseCliArguments([]string{"test_command1", "--foo", "value1", "--bar", "--foo"})

if err == nil {
t.Fatalf("Error was expected but not received")
Expand All @@ -88,5 +86,16 @@ func TestParseCliArguments(t *testing.T) {
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "flag foo was set multiple times", err.Error())
}
})
t.Run("Correctly errors if you pass in empty -", func(t *testing.T) {
_, _, err := ParseCliArguments([]string{"test_command1", "--", "value1", "--bar", "--foo"})

if err == nil {
t.Fatalf("Error was expected but not received")
}

if err.Error() != "empty flag was passed in" {
t.Fatalf("Error message incorrect. Expected \"%v\" got \"%v\"", "empty flag was passed in", err.Error())
}
})

}

0 comments on commit 3159bf5

Please sign in to comment.