Skip to content

Commit 9bde6a2

Browse files
committed
parse flags in runner command, to be able to show version or help, closes #34
1 parent f9eab9d commit 9bde6a2

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ See implementation examples:
157157

158158
### Changes
159159

160+
**2016-06-01**
161+
- parse flags in main command, to show version and help without needing
162+
to compile test package and buildable go sources.
163+
160164
**2016-05-28**
161165
- show nicely formatted called step func name and file path
162166

cmd/godog/main.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
var statusMatch = regexp.MustCompile("^exit status (\\d+)")
1818
var parsedStatus int
1919

20+
var stdout = createAnsiColorWriter(os.Stdout)
21+
var stderr = createAnsiColorWriter(statusOutputFilter(os.Stderr))
22+
2023
func buildAndRun() (int, error) {
2124
var status int
22-
// will support Ansi colors for windows
23-
stdout := createAnsiColorWriter(os.Stdout)
24-
stderr := createAnsiColorWriter(statusOutputFilter(os.Stderr))
2525

2626
dir := fmt.Sprintf(filepath.Join("%s", "godog-%d"), os.TempDir(), time.Now().UnixNano())
2727
err := godog.Build(dir)
@@ -75,9 +75,25 @@ func buildAndRun() (int, error) {
7575
}
7676

7777
func main() {
78+
var vers, defs, sof bool
79+
var tags, format string
80+
var concurrency int
81+
82+
flagSet := godog.FlagSet(&format, &tags, &defs, &sof, &vers, &concurrency)
83+
err := flagSet.Parse(os.Args[1:])
84+
if err != nil {
85+
fmt.Fprintln(stderr, err)
86+
os.Exit(1)
87+
}
88+
89+
if vers {
90+
fmt.Fprintln(stdout, "Godog version is", godog.Version)
91+
os.Exit(0) // should it be 0?
92+
}
93+
7894
status, err := buildAndRun()
7995
if err != nil {
80-
fmt.Fprintln(os.Stderr, err)
96+
fmt.Fprintln(stderr, err)
8197
os.Exit(1)
8298
}
8399
// it might be a case, that status might not be resolved

examples/api/version.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ Feature: get version
2020
And the response should match json:
2121
"""
2222
{
23-
"version": "v0.4.2"
23+
"version": "v0.4.3"
2424
}
2525
"""

flags.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"fmt"
66
)
77

8-
func flags(format, tags *string, defs, sof, vers *bool, cl *int) *flag.FlagSet {
8+
// FlagSet allows to manage flags by external suite runner
9+
func FlagSet(format, tags *string, defs, sof, vers *bool, cl *int) *flag.FlagSet {
910
set := flag.NewFlagSet("godog", flag.ExitOnError)
1011
set.StringVar(format, "format", "pretty", "")
1112
set.StringVar(format, "f", "pretty", "")
@@ -32,7 +33,10 @@ func usage() {
3233

3334
// --- GENERAL ---
3435
fmt.Println(cl("Usage:", yellow))
35-
fmt.Println(s(2) + "godog [options] [<features>]\n")
36+
fmt.Printf(s(2) + "godog [options] [<features>]\n\n")
37+
// description
38+
fmt.Println("Builds a test package and runs given feature files.")
39+
fmt.Printf("Command should be run from the directory of tested package and contain buildable go source.\n\n")
3640

3741
// --- ARGUMENTS ---
3842
fmt.Println(cl("Arguments:", yellow))

godog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ Godog was inspired by Behat and the above description is taken from it's documen
4444
package godog
4545

4646
// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
47-
const Version = "v0.4.2"
47+
const Version = "v0.4.3"

run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func Run(contextInitializer func(suite *Suite)) int {
5656
var vers, defs, sof bool
5757
var tags, format string
5858
var concurrency int
59-
flagSet := flags(&format, &tags, &defs, &sof, &vers, &concurrency)
59+
flagSet := FlagSet(&format, &tags, &defs, &sof, &vers, &concurrency)
6060
err := flagSet.Parse(os.Args[1:])
6161
fatal(err)
6262

0 commit comments

Comments
 (0)