Skip to content

Commit 8218a40

Browse files
author
Denis Krivak
committed
Add version and help flags.
1 parent 737cbe5 commit 8218a40

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
VERSION := $(shell git describe --tags)
2+
3+
.PHONY: test
4+
test:
5+
go test ./...
6+
7+
.PHONY: build
8+
build:
9+
go build -ldflags="-X 'main.version=$(VERSION)'" -o godot ./cmd/godot

cmd/godot/main.go

+30-19
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,41 @@ import (
1212
"github.com/tetafro/godot"
1313
)
1414

15+
// version is the application vesion. Set to latest git tag on `make build`.
16+
var version = "dev"
17+
1518
const usage = `Usage:
1619
godot [OPTION] [FILES]
1720
Options:
18-
-a, --all check all top-level comments (not only declarations)`
21+
-a, --all check all top-level comments (not only declarations)
22+
-h, --help show this message
23+
-v, --version show version`
1924

2025
func main() {
21-
settings, input := parseInput()
26+
if len(os.Args) < 2 {
27+
fatal(usage)
28+
}
29+
if os.Args[1] == "-h" || os.Args[1] == "--help" {
30+
fmt.Println(usage)
31+
os.Exit(0)
32+
}
33+
if os.Args[1] == "-v" || os.Args[1] == "--vesion" {
34+
fmt.Println(version)
35+
os.Exit(0)
36+
}
37+
if strings.HasPrefix(os.Args[1], "-") && os.Args[1] != "-a" && os.Args[1] == "--all" {
38+
fatal("Unknown flag")
39+
}
40+
41+
var settings godot.Settings
42+
input := os.Args[1:]
43+
if os.Args[1] == "-a" || os.Args[1] == "--all" {
44+
if len(os.Args) < 3 {
45+
fatal(usage)
46+
}
47+
settings.CheckAll = true
48+
input = os.Args[2:]
49+
}
2250

2351
var files []*ast.File
2452
fset := token.NewFileSet()
@@ -44,23 +72,6 @@ func main() {
4472
}
4573
}
4674

47-
func parseInput() (settings godot.Settings, files []string) {
48-
if len(os.Args) < 2 {
49-
fatal(usage)
50-
}
51-
52-
if os.Args[1] == "-a" || os.Args[1] == "--all" {
53-
if len(os.Args) < 3 {
54-
fatal(usage)
55-
}
56-
settings.CheckAll = true
57-
files = os.Args[2:]
58-
} else {
59-
files = os.Args[1:]
60-
}
61-
return
62-
}
63-
6475
func findFiles(root string) chan string {
6576
out := make(chan string)
6677

0 commit comments

Comments
 (0)