Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handling flag without '=' #5

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func RunTasks(names ...string) {
}

// RunTasksFromArgs reads the command line and runs each named task.
// A task name is an argument that does not start with a dash "-"
// Make sure any flags are on the commandline are present before the task names.
// A task name is an argument that does not start with a dash "-".
// Flags can be set before or after the task names.
// Boolean flags must have the form `-v=true`.
func RunTasksFromArgs() {
defer deferList.run()
if len(os.Args) == 1 {
Expand All @@ -57,8 +58,12 @@ func RunTasksFromArgs() {
}
for i := 1; i < len(os.Args); i++ {
each := os.Args[i]
if !strings.HasPrefix(each, "-") {
RunTasks(os.Args[i])
if strings.HasPrefix(each, "-") {
if !strings.Contains(each, "=") {
i++ // next can be flag argument or a bool flag
}
} else {
RunTasks(each)
}
}
}
Expand Down
76 changes: 76 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package muts

import (
"os"
"testing"
)

func TestRunTasksFromArgs(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "test"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}
func TestRunTasksFromArgsWithFlag(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "-flag", "flag", "test"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}
func TestRunTasksFromArgsWithFlagAssign(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "-flag=flag", "test"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}

func TestRunTasksFromArgsWithFlagAssignWithEquals(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "-flag=flag=1", "test"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}

func TestRunTasksFromArgsWithFlagAtEnd(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "test", "-flag", "flag"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}

func TestRunTasksFromArgsWithBoolFlag(t *testing.T) {
testHasRun := false
Task("test", func() {
testHasRun = true
})
os.Args = []string{"muts", "-v=true", "test"}
RunTasksFromArgs()
if !testHasRun {
t.Fail()
}
}