Skip to content

Commit 51d628b

Browse files
committed
Add help command, ensure -help works on all commands.
fixes #91
1 parent 6015af9 commit 51d628b

File tree

5 files changed

+60
-18
lines changed

5 files changed

+60
-18
lines changed

create_salsa_project.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"io/ioutil"
67
"log"
78
"net/http"
@@ -10,15 +11,20 @@ import (
1011
)
1112

1213
func execCreateSalsaProject(args []string) {
13-
fs := flag.NewFlagSet("search", flag.ExitOnError)
14+
fs := flag.NewFlagSet("create-salsa-project", flag.ExitOnError)
15+
16+
fs.Usage = func() {
17+
fmt.Fprintf(os.Stderr, "Usage: %s create-salsa-project <project-name>\n", os.Args[0])
18+
fmt.Fprintf(os.Stderr, "Example: %s create-salsa-project golang-github-mattn-go-sqlite3\n", os.Args[0])
19+
}
1420

1521
if err := fs.Parse(args); err != nil {
1622
log.Fatal(err)
1723
}
1824

1925
if fs.NArg() != 1 {
20-
log.Printf("Usage: %s create-salsa-project <project-name>\n", os.Args[0])
21-
log.Fatalf("Example: %s create-salsa-project golang-github-mattn-go-sqlite3\n", os.Args[0])
26+
fs.Usage()
27+
os.Exit(1)
2228
}
2329

2430
projectName := fs.Arg(0)

estimate.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,21 @@ func estimate(importpath string) error {
117117
}
118118

119119
func execEstimate(args []string) {
120-
fs := flag.NewFlagSet("search", flag.ExitOnError)
120+
fs := flag.NewFlagSet("estimate", flag.ExitOnError)
121+
122+
fs.Usage = func() {
123+
fmt.Fprintf(os.Stderr, "Usage: %s estimate <go-package-importpath>\n", os.Args[0])
124+
fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring <go-package-importpath> into Debian by printing all currently unpacked repositories\n")
125+
fmt.Fprintf(os.Stderr, "Example: %s estimate github.com/Debian/dh-make-golang\n", os.Args[0])
126+
}
121127

122128
err := fs.Parse(args)
123129
if err != nil {
124130
log.Fatal(err)
125131
}
126132

127133
if fs.NArg() != 1 {
128-
fmt.Fprintf(os.Stderr, "Usage: %s estimate <importpath>\n", os.Args[0])
129-
fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring <importpath> into Debian by printing all currently unpacked repositories\n")
130-
fmt.Fprintf(os.Stderr, "Example: %s estimate github.com/Debian/dh-make-golang\n", os.Args[0])
134+
fs.Usage()
131135
os.Exit(1)
132136
}
133137

main.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56
)
67

8+
func usage() {
9+
fmt.Fprintf(os.Stderr, "Usage: %s [globalflags] <command> [flags] <args>\n", os.Args[0])
10+
fmt.Fprintf(os.Stderr, "\n")
11+
fmt.Fprintf(os.Stderr, "%s commands:\n", os.Args[0])
12+
fmt.Fprintf(os.Stderr, "\tmake\t\t\tcreate a Debian package\n")
13+
fmt.Fprintf(os.Stderr, "\tsearch\t\t\tsearch Debian for already-existing packages\n")
14+
fmt.Fprintf(os.Stderr, "\testimate\t\testimate the amount of work for a package\n")
15+
fmt.Fprintf(os.Stderr, "\tcreate-salsa-project\tcreate a project for hosting Debian packaging\n")
16+
fmt.Fprintf(os.Stderr, "\n")
17+
fmt.Fprintf(os.Stderr, "For backwards compatibility, when no command is specified, the make command is executed.\n")
18+
fmt.Fprintf(os.Stderr, "To learn more about a command, run %s <command> -help, e.g. %s make -help\n", os.Args[0], os.Args[0])
19+
}
20+
721
func main() {
822
// Retrieve args and Shift binary name off argument list.
923
args := os.Args[1:]
@@ -15,14 +29,18 @@ func main() {
1529
}
1630

1731
switch cmd {
32+
case "help":
33+
usage()
1834
case "search":
1935
execSearch(args[1:])
2036
case "create-salsa-project":
2137
execCreateSalsaProject(args[1:])
2238
case "estimate":
2339
execEstimate(args[1:])
40+
case "make":
41+
execMake(args[1:], nil)
2442
default:
25-
execMake(args)
43+
// redirect -help to the global usage
44+
execMake(args, usage)
2645
}
27-
2846
}

make.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,22 @@ func copyFile(src, dest string) error {
682682
return output.Close()
683683
}
684684

685-
func execMake(args []string) {
685+
func execMake(args []string, usage func()) {
686686
fs := flag.NewFlagSet("make", flag.ExitOnError)
687+
if usage != nil {
688+
fs.Usage = usage
689+
} else {
690+
fs.Usage = func() {
691+
fmt.Fprintf(os.Stderr, "Usage: %s [make] <go-package-importpath>\n", os.Args[0])
692+
fmt.Fprintf(os.Stderr, "Example: %s make golang.org/x/oauth2\n", os.Args[0])
693+
fmt.Fprintf(os.Stderr, "\n")
694+
fmt.Fprintf(os.Stderr, "%s will create new files and directories in the current working directory.\n", os.Args[0])
695+
fmt.Fprintf(os.Stderr, "%s will connect to the internet to download the specified Go package.\n", os.Args[0])
696+
fmt.Fprintf(os.Stderr, "\n")
697+
fmt.Fprintf(os.Stderr, "Flags:\n")
698+
fs.PrintDefaults()
699+
}
700+
}
687701

688702
var gitRevision string
689703
fs.StringVar(&gitRevision,
@@ -709,11 +723,7 @@ func execMake(args []string) {
709723
}
710724

711725
if fs.NArg() < 1 {
712-
fmt.Fprintf(os.Stderr, "Usage: %s <go-package-name>\n", os.Args[0])
713-
fmt.Fprintf(os.Stderr, "Example: %s golang.org/x/oauth2\n", os.Args[0])
714-
fmt.Fprintf(os.Stderr, "\n")
715-
fmt.Fprintf(os.Stderr, "%s will create new files and directories in the current working directory.\n", os.Args[0])
716-
fmt.Fprintf(os.Stderr, "%s will connect to the internet to download the specified Go package and its dependencies.\n", os.Args[0])
726+
fs.Usage()
717727
os.Exit(1)
718728
}
719729

search.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,19 @@ func getGolangBinaries() (map[string]string, error) {
4848
func execSearch(args []string) {
4949
fs := flag.NewFlagSet("search", flag.ExitOnError)
5050

51+
fs.Usage = func() {
52+
fmt.Fprintf(os.Stderr, "Usage: %s search <pattern>\n", os.Args[0])
53+
fmt.Fprintf(os.Stderr, "Uses Go's default regexp syntax (https://golang.org/pkg/regexp/syntax/)\n")
54+
fmt.Fprintf(os.Stderr, "Example: %s search 'debi.*'\n", os.Args[0])
55+
}
56+
5157
err := fs.Parse(args)
5258
if err != nil {
5359
log.Fatal(err)
5460
}
5561

5662
if fs.NArg() != 1 {
57-
fmt.Fprintf(os.Stderr, "Usage: %s search <pattern>\n", os.Args[0])
58-
fmt.Fprintf(os.Stderr, "Uses Go's default regexp syntax (https://golang.org/pkg/regexp/syntax/)\n")
59-
fmt.Fprintf(os.Stderr, "Example: %s search debi.*\n", os.Args[0])
63+
fs.Usage()
6064
os.Exit(1)
6165
}
6266

0 commit comments

Comments
 (0)