From 51d628b8dca9262719b3d4ba4186cdb8dbb97ed4 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Thu, 1 Mar 2018 17:47:28 +0100 Subject: [PATCH] Add help command, ensure -help works on all commands. fixes #91 --- create_salsa_project.go | 12 +++++++++--- estimate.go | 12 ++++++++---- main.go | 22 ++++++++++++++++++++-- make.go | 22 ++++++++++++++++------ search.go | 10 +++++++--- 5 files changed, 60 insertions(+), 18 deletions(-) diff --git a/create_salsa_project.go b/create_salsa_project.go index c1ece46..745f050 100644 --- a/create_salsa_project.go +++ b/create_salsa_project.go @@ -2,6 +2,7 @@ package main import ( "flag" + "fmt" "io/ioutil" "log" "net/http" @@ -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 \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 \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) diff --git a/estimate.go b/estimate.go index c956420..bd89ea9 100644 --- a/estimate.go +++ b/estimate.go @@ -117,7 +117,13 @@ 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 \n", os.Args[0]) + fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring 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 { @@ -125,9 +131,7 @@ func execEstimate(args []string) { } if fs.NArg() != 1 { - fmt.Fprintf(os.Stderr, "Usage: %s estimate \n", os.Args[0]) - fmt.Fprintf(os.Stderr, "Estimates the work necessary to bring 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) } diff --git a/main.go b/main.go index 36e76db..192ce61 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,23 @@ package main import ( + "fmt" "os" ) +func usage() { + fmt.Fprintf(os.Stderr, "Usage: %s [globalflags] [flags] \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 -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:] @@ -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) } - } diff --git a/make.go b/make.go index df173af..784c538 100644 --- a/make.go +++ b/make.go @@ -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] \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, @@ -709,11 +723,7 @@ func execMake(args []string) { } if fs.NArg() < 1 { - fmt.Fprintf(os.Stderr, "Usage: %s \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) } diff --git a/search.go b/search.go index 2fdb9ef..286669e 100644 --- a/search.go +++ b/search.go @@ -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 \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 \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) }