Skip to content

Commit

Permalink
Add help command, ensure -help works on all commands.
Browse files Browse the repository at this point in the history
fixes #91
  • Loading branch information
stapelberg committed Mar 1, 2018
1 parent 6015af9 commit 51d628b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 18 deletions.
12 changes: 9 additions & 3 deletions create_salsa_project.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -10,15 +11,20 @@ import (
)

func execCreateSalsaProject(args []string) {
fs := flag.NewFlagSet("search", flag.ExitOnError)
fs := flag.NewFlagSet("create-salsa-project", flag.ExitOnError)

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s create-salsa-project <project-name>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example: %s create-salsa-project golang-github-mattn-go-sqlite3\n", os.Args[0])
}

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

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

projectName := fs.Arg(0)
Expand Down
12 changes: 8 additions & 4 deletions estimate.go
Expand Up @@ -117,17 +117,21 @@ func estimate(importpath string) error {
}

func execEstimate(args []string) {
fs := flag.NewFlagSet("search", flag.ExitOnError)
fs := flag.NewFlagSet("estimate", flag.ExitOnError)

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s estimate <go-package-importpath>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring <go-package-importpath> into Debian by printing all currently unpacked repositories\n")
fmt.Fprintf(os.Stderr, "Example: %s estimate github.com/Debian/dh-make-golang\n", os.Args[0])
}

err := fs.Parse(args)
if err != nil {
log.Fatal(err)
}

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

Expand Down
22 changes: 20 additions & 2 deletions main.go
@@ -1,9 +1,23 @@
package main

import (
"fmt"
"os"
)

func usage() {
fmt.Fprintf(os.Stderr, "Usage: %s [globalflags] <command> [flags] <args>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "%s commands:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\tmake\t\t\tcreate a Debian package\n")
fmt.Fprintf(os.Stderr, "\tsearch\t\t\tsearch Debian for already-existing packages\n")
fmt.Fprintf(os.Stderr, "\testimate\t\testimate the amount of work for a package\n")
fmt.Fprintf(os.Stderr, "\tcreate-salsa-project\tcreate a project for hosting Debian packaging\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "For backwards compatibility, when no command is specified, the make command is executed.\n")
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])
}

func main() {
// Retrieve args and Shift binary name off argument list.
args := os.Args[1:]
Expand All @@ -15,14 +29,18 @@ func main() {
}

switch cmd {
case "help":
usage()
case "search":
execSearch(args[1:])
case "create-salsa-project":
execCreateSalsaProject(args[1:])
case "estimate":
execEstimate(args[1:])
case "make":
execMake(args[1:], nil)
default:
execMake(args)
// redirect -help to the global usage
execMake(args, usage)
}

}
22 changes: 16 additions & 6 deletions make.go
Expand Up @@ -682,8 +682,22 @@ func copyFile(src, dest string) error {
return output.Close()
}

func execMake(args []string) {
func execMake(args []string, usage func()) {
fs := flag.NewFlagSet("make", flag.ExitOnError)
if usage != nil {
fs.Usage = usage
} else {
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [make] <go-package-importpath>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Example: %s make golang.org/x/oauth2\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "%s will create new files and directories in the current working directory.\n", os.Args[0])
fmt.Fprintf(os.Stderr, "%s will connect to the internet to download the specified Go package.\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
fs.PrintDefaults()
}
}

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

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

Expand Down
10 changes: 7 additions & 3 deletions search.go
Expand Up @@ -48,15 +48,19 @@ func getGolangBinaries() (map[string]string, error) {
func execSearch(args []string) {
fs := flag.NewFlagSet("search", flag.ExitOnError)

fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s search <pattern>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Uses Go's default regexp syntax (https://golang.org/pkg/regexp/syntax/)\n")
fmt.Fprintf(os.Stderr, "Example: %s search 'debi.*'\n", os.Args[0])
}

err := fs.Parse(args)
if err != nil {
log.Fatal(err)
}

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

Expand Down

0 comments on commit 51d628b

Please sign in to comment.