From 68a776329ff9d3061a36681fa46ee15c94285248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Thu, 18 Jun 2015 19:03:54 +0200 Subject: [PATCH] Proper usage functions --- cmd/fdroidcl/devices.go | 4 ++-- cmd/fdroidcl/installed.go | 4 ++-- cmd/fdroidcl/list.go | 4 ++-- cmd/fdroidcl/main.go | 32 +++++++++++++++++++++++++++++--- cmd/fdroidcl/search.go | 4 ++-- cmd/fdroidcl/show.go | 4 ++-- cmd/fdroidcl/update.go | 4 ++-- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/cmd/fdroidcl/devices.go b/cmd/fdroidcl/devices.go index 1b4f764..853c0d5 100644 --- a/cmd/fdroidcl/devices.go +++ b/cmd/fdroidcl/devices.go @@ -11,8 +11,8 @@ import ( ) var cmdDevices = &Command{ - Name: "devices", - Short: "List connected devices", + UsageLine: "devices", + Short: "List connected devices", } func init() { diff --git a/cmd/fdroidcl/installed.go b/cmd/fdroidcl/installed.go index 5c4657f..5622d36 100644 --- a/cmd/fdroidcl/installed.go +++ b/cmd/fdroidcl/installed.go @@ -11,8 +11,8 @@ import ( ) var cmdInstalled = &Command{ - Name: "installed", - Short: "List installed apps", + UsageLine: "installed", + Short: "List installed apps", } func init() { diff --git a/cmd/fdroidcl/list.go b/cmd/fdroidcl/list.go index 2850795..2158602 100644 --- a/cmd/fdroidcl/list.go +++ b/cmd/fdroidcl/list.go @@ -13,8 +13,8 @@ import ( ) var cmdList = &Command{ - Name: "list", - Short: "List all available apps", + UsageLine: "list", + Short: "List all available apps", } func init() { diff --git a/cmd/fdroidcl/main.go b/cmd/fdroidcl/main.go index 95c4a56..2e95d4d 100644 --- a/cmd/fdroidcl/main.go +++ b/cmd/fdroidcl/main.go @@ -8,9 +8,12 @@ import ( "fmt" "log" "os" + "strings" ) const ( + cmdName = "fdroidcl" + repoName = "repo" repoURL = "https://f-droid.org/repo" ) @@ -22,8 +25,9 @@ type Command struct { // The args are the arguments after the command name. Run func(args []string) - // Name of the command. - Name string + // UsageLine is the one-line usage message. + // The first word in the line is taken to be the command name. + UsageLine string // Short is the short description. Short string @@ -32,6 +36,27 @@ type Command struct { Flag flag.FlagSet } +// Name returns the command's name: the first word in the usage line. +func (c *Command) Name() string { + name := c.UsageLine + i := strings.Index(name, " ") + if i >= 0 { + name = name[:i] + } + return name +} + +func (c *Command) Usage() { + fmt.Fprintf(os.Stderr, "Usage: %s %s\n", cmdName, c.UsageLine) + anyFlags := false + c.Flag.VisitAll(func(f *flag.Flag) { anyFlags = true }) + if anyFlags { + fmt.Fprintf(os.Stderr, "\nAvailable options:\n") + c.Flag.PrintDefaults() + } + os.Exit(2) +} + func init() { flag.Usage = func() { fmt.Fprintln(os.Stderr, "Usage: fdroidcl [-h] []") @@ -66,9 +91,10 @@ func main() { } for _, cmd := range commands { - if cmd.Name != args[0] { + if cmd.Name() != args[0] { continue } + cmd.Flag.Usage = func() { cmd.Usage() } cmd.Flag.Parse(args[1:]) args = cmd.Flag.Args() cmd.Run(args) diff --git a/cmd/fdroidcl/search.go b/cmd/fdroidcl/search.go index 09cda44..d7d5b53 100644 --- a/cmd/fdroidcl/search.go +++ b/cmd/fdroidcl/search.go @@ -12,8 +12,8 @@ import ( ) var cmdSearch = &Command{ - Name: "search", - Short: "Search available apps", + UsageLine: "search ", + Short: "Search available apps", } var ( diff --git a/cmd/fdroidcl/show.go b/cmd/fdroidcl/show.go index ecd85d5..c5b7171 100644 --- a/cmd/fdroidcl/show.go +++ b/cmd/fdroidcl/show.go @@ -13,8 +13,8 @@ import ( ) var cmdShow = &Command{ - Name: "show", - Short: "Show detailed info of an app", + UsageLine: "show ", + Short: "Show detailed info of an app", } func init() { diff --git a/cmd/fdroidcl/update.go b/cmd/fdroidcl/update.go index cd569c5..a85ce2a 100644 --- a/cmd/fdroidcl/update.go +++ b/cmd/fdroidcl/update.go @@ -16,8 +16,8 @@ import ( ) var cmdUpdate = &Command{ - Name: "update", - Short: "Update the index", + UsageLine: "update", + Short: "Update the index", } func init() {