main.go 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/codegangsta/cli"
  5. )
  6. func main() {
  7. app := cli.NewApp()
  8. app.EnableBashCompletion = true
  9. app.Name = "greet"
  10. app.Usage = "fight the loneliness!"
  11. app.Flags = []cli.Flag{
  12. cli.StringFlag{
  13. Name: "lang, l",
  14. Value: "english",
  15. Usage: "language for the greeting",
  16. EnvVar: "APP_LANG",
  17. },
  18. }
  19. app.Action = func(c *cli.Context) {
  20. name := "someone"
  21. if len(c.Args()) > 0 {
  22. name = c.Args()[0]
  23. }
  24. if c.String("lang") == "spanish" {
  25. fmt.Println("Hola", name)
  26. } else {
  27. fmt.Println("Hello", name)
  28. }
  29. }
  30. app.Commands = []cli.Command{
  31. {
  32. Name: "add",
  33. Aliases: []string{"a"},
  34. Usage: "add a task to the list",
  35. Action: func(c *cli.Context) {
  36. fmt.Println("added task: ", c.Args().First())
  37. },
  38. },
  39. {
  40. Name: "complete",
  41. Aliases: []string{"c"},
  42. Usage: "complete a task on the list",
  43. Action: func(c *cli.Context) {
  44. fmt.Println("completed task: ", c.Args().First())
  45. },
  46. },
  47. }
  48. app.RunAndExitOnError()
  49. }