~samwhited/cli

ref: e61e7fcaa94afc5f08431923c74acc39a79fecbc cli/example_test.go -rw-r--r-- 1.8 KiB
e61e7fcaSam Whited cli: fix typo in docs 1 year, 20 days ago
                                                                                
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2017 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package cli_test

import (
	"flag"
	"fmt"
	"os"

	"mellium.im/cli"
)

func commitCmd(cfg *string) *cli.Command {
	commitFlags := flag.NewFlagSet("commit", flag.ExitOnError)
	commitFlags.SetOutput(os.Stdout)
	help := commitFlags.Bool("h", false, "Print this commands help output…")
	interactive := commitFlags.Bool("interactive", false, "Run commit in interactive mode.")
	if cfg == nil {
		empty := ""
		cfg = &empty
	}

	return &cli.Command{
		Usage: `commit [-h] [-interactive] …`,
		Description: `Records changes to the repository.

Stores the current contents of the index in a new commit…`,
		Flags: commitFlags,
		Run: func(c *cli.Command, args ...string) error {
			_ = commitFlags.Parse(args)

			fmt.Printf("Using config file: %s\n", *cfg)
			if *interactive {
				fmt.Println("Interactive mode enabled.")
			}
			if *help {
				c.Help()
			}
			return nil
		},
	}
}

func Example() {
	globalFlags := flag.NewFlagSet("git", flag.ExitOnError)
	cfg := globalFlags.String("config", "gitconfig", "A custom config file to load")

	cmds := &cli.Command{
		Usage: "git",
		Flags: globalFlags,
		Commands: []*cli.Command{
			commitCmd(cfg),
		},
	}

	// In a real main function, this would probably be os.Args[1:]
	cmds.Exec("-config", "mygit.config", "commit", "-interactive", "-h")

	// Output:
	// Using config file: mygit.config
	// Interactive mode enabled.
	// Usage: commit [-h] [-interactive] …
	//
	// Options:
	//
	//   -h	Print this commands help output…
	//   -interactive
	//     	Run commit in interactive mode.
	//
	// Records changes to the repository.
	//
	// Stores the current contents of the index in a new commit…
}