Skip to content
This repository has been archived by the owner on Jun 12, 2018. It is now read-only.

Commit

Permalink
Merge pull request #30 from closeio/nicer-args-mgmt
Browse files Browse the repository at this point in the history
Nicer handling of command line arguments
  • Loading branch information
Travis Redman committed Nov 10, 2015
2 parents 1e61454 + 320c208 commit 07f0657
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions cmd/flashback/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"flag"
"fmt"
"math"
Expand Down Expand Up @@ -100,7 +99,7 @@ func init() {
"[Optional] Number of workers that sends ops to database.")
flag.IntVar(&maxOps,
"maxOps",
0,
math.MaxUint32, // default value for maxOps is maxUint32
"[Optional] Maximal amount of ops to be replayed from the "+
"ops_filename file. By setting it to `0`, replayer will "+
"replay all the ops.")
Expand Down Expand Up @@ -158,18 +157,30 @@ func init() {

func parseFlags() error {
flag.Parse()
if style != "stress" && style != "real" {
return errors.New("Missing or invalid `style` argument passed to program: " + style)
}
if opsFilename == "" {
return errors.New("Missing required `ops_filename` argument")
}
if workers <= 0 {
return errors.New("The `workers` argument must be a positive number")
validArgs := true
errorMsg := ""

if style == "" {
validArgs = false
errorMsg = "Missing `style` argument."
} else if style != "stress" && style != "real" {
validArgs = false
errorMsg = "Invalid `style` argument passed to program: " + style + ". The only acceptable values are \"stress\" and \"real\"."
} else if opsFilename == "" {
validArgs = false
errorMsg = "Missing required `ops_filename` argument."
} else if workers <= 0 {
validArgs = false
errorMsg = "The `workers` argument must be a positive number."
}
if maxOps == 0 {
maxOps = math.MaxUint32

if !validArgs {
fmt.Println(errorMsg)
fmt.Println("\nUsage:")
flag.PrintDefaults()
os.Exit(1)
}

var err error
if logger, err = flashback.NewLogger(stdout, stderr); err != nil {
return err
Expand Down

0 comments on commit 07f0657

Please sign in to comment.