Browse Source

Proper usage functions

pull/8/head
Daniel Martí 11 years ago
parent
commit
68a776329f
  1. 4
      cmd/fdroidcl/devices.go
  2. 4
      cmd/fdroidcl/installed.go
  3. 4
      cmd/fdroidcl/list.go
  4. 32
      cmd/fdroidcl/main.go
  5. 4
      cmd/fdroidcl/search.go
  6. 4
      cmd/fdroidcl/show.go
  7. 4
      cmd/fdroidcl/update.go

4
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() {

4
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() {

4
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() {

32
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] <command> [<args>]")
@ -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)

4
cmd/fdroidcl/search.go

@ -12,8 +12,8 @@ import (
)
var cmdSearch = &Command{
Name: "search",
Short: "Search available apps",
UsageLine: "search <regexp...>",
Short: "Search available apps",
}
var (

4
cmd/fdroidcl/show.go

@ -13,8 +13,8 @@ import (
)
var cmdShow = &Command{
Name: "show",
Short: "Show detailed info of an app",
UsageLine: "show <appid...>",
Short: "Show detailed info of an app",
}
func init() {

4
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() {

Loading…
Cancel
Save