Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mastering CLIs

Mastering CLIs

Johannes Pichler

April 24, 2019
Tweet

More Decks by Johannes Pichler

Other Decks in Technology

Transcript

  1. args := os.Args[1:] if len(args) == 0 { fmt.Println("you need

    to provide a command") return } command := args[0] fmt.Printf("Command: %s\n", command)
  2. switch args[0] { case "list": fmt.Println("List todos") break case "new":

    fmt.Println("New Todo") break case "complete": fmt.Println("Complete Todo") break default: fmt.Println("Invalid command specified") break }
  3. !

  4. args := os.Args[1:] if len(args) == 0 { fmt.Println("you need

    to provide a command") return } command := args[0] switch command { case "list": // ... break case "new": newCommand := flag.NewFlagSet("new", flag.ExitOnError) text := newCommand.String("text", "", "text for new todo") newCommand.Parse(args[1:]) // ... break case "complete": completeCommand := flag.NewFlagSet("complete", flag.ExitOnError) id := completeCommand.Int("id", 0, "id to complete") completeCommand.Parse(args[1:]) // ... break }
  5. COBRA Many of the most widely used Go projects are

    built using Cobra, such as: Kubernetes, Hugo, rkt, etcd, Moby (former Docker), Docker (distribution), OpenShift, Delve, GopherJS, CockroachDB, Bleve, ProjectAtomic (enterprise), Giant Swarm's gsctl, Nanobox/Nanopack, rclone, nehm, Pouch, Istio, Prototool, mattermost-server, etc.