You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. type Command struct {
  9. // Run runs the command.
  10. // The args are the arguments after the command name.
  11. Run func(cmd *Command, args []string) bool
  12. // UsageLine is the one-line usage message.
  13. // The first word in the line is taken to be the command name.
  14. UsageLine string
  15. // Short is the short description shown in the 'go help' output.
  16. Short string
  17. // Long is the long message shown in the 'go help <this-command>' output.
  18. Long string
  19. // Flag is a set of flags specific to this command.
  20. Flag flag.FlagSet
  21. IsDebug *bool
  22. }
  23. // Name returns the command's name: the first word in the usage line.
  24. func (c *Command) Name() string {
  25. name := c.UsageLine
  26. i := strings.Index(name, " ")
  27. if i >= 0 {
  28. name = name[:i]
  29. }
  30. return name
  31. }
  32. func (c *Command) Usage() {
  33. fmt.Fprintf(os.Stderr, "Example: weed %s\n", c.UsageLine)
  34. fmt.Fprintf(os.Stderr, "Default Usage:\n")
  35. c.Flag.PrintDefaults()
  36. fmt.Fprintf(os.Stderr, "Description:\n")
  37. fmt.Fprintf(os.Stderr, " %s\n", strings.TrimSpace(c.Long))
  38. os.Exit(2)
  39. }
  40. // Runnable reports whether the command can be run; otherwise
  41. // it is a documentation pseudo-command such as importpath.
  42. func (c *Command) Runnable() bool {
  43. return c.Run != nil
  44. }