# Getting Started

# Installation

go get -v github.com/hedzr/cmdr

cmdr can support go v1.11 and later.

To have a full view about our compatibilities to golang version, take a look go.yml (opens new window).

# Base Usage

Simple app here:

package main

import (
	"fmt"
	"github.com/hedzr/cmdr"
	cmdrexamples "github.com/hedzr/cmdr-examples"
	"log"
)

func main() {
	if err := cmdr.Exec(buildRootCmd()); err != nil {
		log.Printf("error: %+v\n", err)
	}
}

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
	root := cmdr.
		Root(appName, cmdrexamples.Version).
		Copyright(copyright, "hedzr").
		Description(desc, longDesc).
		Examples(examples)
	rootCmd = root.RootCommand()
	soundex(root)
	return
}

func soundex(root cmdr.OptCmd) {
	root.NewSubCommand("soundex", "snd", "sndx", "sound").
		Description("soundex test").
		Group("Test").
		TailPlaceholder("[text1, text2, ...]").
		Action(func(cmd *cmdr.Command, args []string) (err error) {
			for ix, s := range args {
				fmt.Printf("%5d. %s => %s\n", ix, s, cmdr.Soundex(s))
			}
			return
		})
}

const (
	appName   = "getting-start"
	copyright = "getting-start is an effective devops tool"
	desc      = "getting-start is an effective devops tool. It make an demo application for `cmdr`."
	longDesc  = "getting-start is an effective devops tool. It make an demo application for `cmdr`."
	examples  = `
$ {{.AppName}} gen shell [--bash|--zsh|--auto]
  generate bash/shell completion scripts
$ {{.AppName}} gen man
  generate linux man page 1
$ {{.AppName}} --help
  show help screen.
`
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

You can get its full source codes at https://github.com/hedzr/cmdr-examples/blob/master/examples/getting-start (opens new window).

# Display the Help Screen

Run it and show the help screen:

go run ./examples/getting-start
1

The results should be:

image-20200713140513668

# Run a Sub-command

Now we could run the sub-command soundex to test:

$ go run ./examples/getting-start sndx fish bird
    0. fish => f12
    1. bird => b163
1
2
3

# All folks

Congrates, you took the shot!

🔚