Skip to content

Commit 47f065a

Browse files
committed
Merge pull request #1 from yuya-takeyama/options
Add command-line options: -h (--help) and -v (--version)
2 parents 4d83474 + 277b408 commit 47f065a

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Command to execute command N times
55
## Usage
66

77
```
8-
$ ntimes 3 echo foo bar baz
8+
$ ntimes 3 -- echo foo bar baz
99
foo bar baz
1010
foo bar baz
1111
foo bar baz

main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
package main
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67
"os/exec"
78
"strconv"
9+
10+
flags "github.com/jessevdk/go-flags"
811
)
912

1013
const AppName = "ntimes"
1114

15+
type Options struct {
16+
ShowVersion bool `short:"v" long:"version" description:"Show version"`
17+
}
18+
19+
var opts Options
20+
1221
func main() {
13-
cnt, err := strconv.Atoi(os.Args[1])
14-
cmdName := os.Args[2]
15-
cmdArgs := os.Args[3:]
22+
parser := flags.NewParser(&opts, flags.Default^flags.PrintErrors)
23+
parser.Name = AppName
24+
parser.Usage = "[OPTIONS] -- COMMAND"
25+
26+
args, err := parser.Parse()
27+
28+
if err != nil {
29+
fmt.Fprint(os.Stderr, err)
30+
return
31+
}
32+
33+
if opts.ShowVersion {
34+
io.WriteString(os.Stdout, fmt.Sprintf("%s v%s, build %s\n", AppName, Version, GitCommit))
35+
return
36+
}
37+
38+
cnt, err := strconv.Atoi(args[0])
39+
cmdName := args[1]
40+
cmdArgs := args[2:]
1641

1742
if err != nil {
1843
panic(err)

wercker.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
box: wercker/golang
2-
services:
3-
- wercker/mysql
1+
box: tcnksm/gox
42
build:
53
steps:
64
- setup-go-workspace
5+
- wercker/golint
76
- script:
87
name: go get
98
code: |
10-
cd $WERCKER_SOURCE_DIR
11-
go version
129
go get -t ./...
1310
- script:
1411
name: go build
@@ -17,9 +14,4 @@ build:
1714
- script:
1815
name: go test
1916
code: |
20-
export DB2YAML_MYSQL_HOST=$WERCKER_MYSQL_HOST
21-
export DB2YAML_MYSQL_PORT=$WERCKER_MYSQL_PORT
22-
export DB2YAML_MYSQL_USERNAME=$WERCKER_MYSQL_USERNAME
23-
export DB2YAML_MYSQL_PASSWORD=$WERCKER_MYSQL_PASSWORD
24-
export DB2YAML_MYSQL_DATABASE=$WERCKER_MYSQL_DATABASE
2517
go test ./...

0 commit comments

Comments
 (0)