Skip to content

Commit f6be776

Browse files
committed
implement help; move 'no types' message up to gen out of typewriter
1 parent 8b3f894 commit f6be776

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

help.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"text/template"
6+
)
7+
8+
func help() error {
9+
info := helpInfo{
10+
Name: os.Args[0],
11+
CustomName: customName,
12+
}
13+
14+
helpTmpl.Execute(out, info)
15+
return nil
16+
}
17+
18+
type helpInfo struct {
19+
Name, CustomName string
20+
}
21+
22+
var helpTmpl = template.Must(template.New("help").Parse(`
23+
Usage:
24+
{{.Name}} Generate files for types marked with +gen
25+
{{.Name}} get Download and install typewriters (standard or custom)
26+
{{.Name}} list List available typewriters (standard or custom)
27+
{{.Name}} custom Create a standard {{.CustomName}} file for importing custom typewriters
28+
{{.Name}} help Print usage
29+
30+
Further details are available at http://clipperhouse.github.io/gen
31+
`))

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ func main() {
2020
case "get":
2121
runCmd(get)
2222
// TODO: pass subsequent flags (such as -u) to get
23+
case "help":
24+
runCmd(help)
2325
case "list":
2426
runCmd(list)
2527
}
26-
// TODO: add help command
2728
// TODO: verbosity?
2829
}
2930

run.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package main
22

3-
import "github.com/clipperhouse/gen/typewriter"
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/clipperhouse/gen/typewriter"
8+
)
49

510
func run() error {
611
imports := []string{
12+
`"fmt"`,
713
`"os"`,
814
`"github.com/clipperhouse/gen/typewriter"`,
915
}
@@ -18,6 +24,14 @@ func runStandard() error {
1824
return err
1925
}
2026

27+
if len(app.Types) == 0 {
28+
return fmt.Errorf("No types marked with +gen were found. See http://clipperhouse.github.io/gen to get started, or type %s help.", os.Args[0])
29+
}
30+
31+
if len(app.TypeWriters) == 0 {
32+
return fmt.Errorf("No typewriters were imported. See http://clipperhouse.github.io/gen to get started, or type %s help.", os.Args[0])
33+
}
34+
2135
if err := app.WriteAll(); err != nil {
2236
return err
2337
}
@@ -27,16 +41,31 @@ func runStandard() error {
2741

2842
const runBody string = `
2943
func main() {
44+
if err := gen(); err != nil {
45+
os.Stderr.WriteString(err.Error() + "\n")
46+
os.Exit(1)
47+
}
48+
}
49+
50+
func gen() error {
3051
app, err := typewriter.NewApp("+gen")
3152
3253
if err != nil {
33-
os.Stderr.WriteString(err.Error() + "\n")
34-
os.Exit(1)
54+
return err
55+
}
56+
57+
if len(app.Types) == 0 {
58+
return fmt.Errorf("No types marked with +gen were found. See http://clipperhouse.github.io/gen to get started, or type %s help.", os.Args[0])
59+
}
60+
61+
if len(app.TypeWriters) == 0 {
62+
return fmt.Errorf("No typewriters were imported. See http://clipperhouse.github.io/gen to get started, or type %s help.", os.Args[0])
3563
}
3664
3765
if err := app.WriteAll(); err != nil {
38-
os.Stderr.WriteString(err.Error() + "\n")
39-
os.Exit(1)
66+
return err
4067
}
68+
69+
return nil
4170
}
4271
`

typewriter/app.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ func NewAppFiltered(directive string, filter func(os.FileInfo) bool) (*app, erro
3838
return a, err
3939
}
4040

41-
if len(typs) == 0 {
42-
return a, fmt.Errorf("no types marked with %s were found, so there is nothing to gen. see http://clipperhouse.github.io/gen to get started.", directive)
43-
}
44-
4541
a.Types = typs
4642
a.TypeWriters = typeWriters
4743
return a, nil

0 commit comments

Comments
 (0)