Skip to content

Commit

Permalink
parse flags in runner command, to be able to show version or help, cl…
Browse files Browse the repository at this point in the history
…oses #34
  • Loading branch information
l3pp4rd committed Jun 1, 2016
1 parent f9eab9d commit 9bde6a2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ See implementation examples:

### Changes

**2016-06-01**
- parse flags in main command, to show version and help without needing
to compile test package and buildable go sources.

**2016-05-28**
- show nicely formatted called step func name and file path

Expand Down
24 changes: 20 additions & 4 deletions cmd/godog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
var statusMatch = regexp.MustCompile("^exit status (\\d+)")
var parsedStatus int

var stdout = createAnsiColorWriter(os.Stdout)
var stderr = createAnsiColorWriter(statusOutputFilter(os.Stderr))

func buildAndRun() (int, error) {
var status int
// will support Ansi colors for windows
stdout := createAnsiColorWriter(os.Stdout)
stderr := createAnsiColorWriter(statusOutputFilter(os.Stderr))

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

func main() {
var vers, defs, sof bool
var tags, format string
var concurrency int

flagSet := godog.FlagSet(&format, &tags, &defs, &sof, &vers, &concurrency)
err := flagSet.Parse(os.Args[1:])
if err != nil {
fmt.Fprintln(stderr, err)
os.Exit(1)
}

if vers {
fmt.Fprintln(stdout, "Godog version is", godog.Version)
os.Exit(0) // should it be 0?
}

status, err := buildAndRun()
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(stderr, err)
os.Exit(1)
}
// it might be a case, that status might not be resolved
Expand Down
2 changes: 1 addition & 1 deletion examples/api/version.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Feature: get version
And the response should match json:
"""
{
"version": "v0.4.2"
"version": "v0.4.3"
}
"""
8 changes: 6 additions & 2 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
)

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

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

// --- ARGUMENTS ---
fmt.Println(cl("Arguments:", yellow))
Expand Down
2 changes: 1 addition & 1 deletion godog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Godog was inspired by Behat and the above description is taken from it's documen
package godog

// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
const Version = "v0.4.2"
const Version = "v0.4.3"
2 changes: 1 addition & 1 deletion run.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Run(contextInitializer func(suite *Suite)) int {
var vers, defs, sof bool
var tags, format string
var concurrency int
flagSet := flags(&format, &tags, &defs, &sof, &vers, &concurrency)
flagSet := FlagSet(&format, &tags, &defs, &sof, &vers, &concurrency)
err := flagSet.Parse(os.Args[1:])
fatal(err)

Expand Down

0 comments on commit 9bde6a2

Please sign in to comment.