# Overview

cmdr is a golang library to interpret or parse the command-line input with POSIX-compliant style (the getopt_long (opens new window) command line UI).

cmdr is a better replacement to the standard library flag.

There is a full Options Store (configurations) for your hierarchy configuration data too.

# Features

  • Friendly API styles: Fluent, flag-like, or old-style (struct)

  • POSIX-compliant flags for CLI: short, long and aliases

  • Nested command and sub-commands

  • Strong data types. Such as: bool, int, uint, string, string slice, ...

  • Groupable (and sortable) commands and flags

  • Smart suggestions for wrong command and flags

    since v1.1.3, using Jaro-Winkler distance (opens new window) instead of soundex.

  • Environment Variables overriding

  • Mergeable external configuration files

  • Automatic help screen generation

  • Riched debugging tools: ~~tree, --debug, ....

  • Integration with Options Store which provides riched read/write for hierarchical application configurations.

# Examples

package main

import (
	"fmt"
	"github.com/hedzr/cmdr"
	"github.com/hedzr/cmdr/tool"
)

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

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
	root := cmdr.
		Root("test-app", "1.1").
		Copyright("test-app is powered by cmdr", "hedzr").
		Description("desc", "longDesc").
		Examples("examples")
		//.Action(func(cmd *cmdr.Command, args []string) (err error) { return; )
	rootCmd = root.RootCommand()

	cmdr.NewBool(false).
		Titles("enable-ueh", "ueh").
		EnvKeys("ENABLE_UEH").
		Description("Enables the unhandled exception handler?").
		AttachTo(root)

	cmdr.NewCmd().
		Titles("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, tool.Soundex(s))
			}
			return
		}).
		AttachTo(root)
	return
}
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

Play on Go Playground (opens new window)

The screen looks like:

image-20210920154132341

# More Examples

There are lots of small apps to show you what and how to apply a feature of cmdr. They lies at: https://github.com/hedzr/cmdr-examples (opens new window).

# License

MIT and feel free.

🔚