~samwhited/cli

ref: fcf5af920f9ad23ecd9426a942ea0a272650f133 cli/example_test.go -rw-r--r-- 1.6 KiB
fcf5af92 — Sam Whited Make vet shut up 4 years 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
// Copyright 2017 The Mellium Authors.
// 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)
	help := commitFlags.Bool("h", false, "Print this commands help output…")
	interactive := commitFlags.Bool("interactive", false, "Run commit in interactive mode.")

	return &cli.Command{
		Usage: `commit [-h] …`,
		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(os.Stdout)
			}
			return nil
		},
	}
}

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

	// TODO: os.Args[1:]
	globalFlags.Parse([]string{"-config", "mygit.config", "commit", "-interactive", "-h"})

	cmds := &cli.CommandSet{
		Name: "git",
		Commands: []*cli.Command{
			commitCmd(*cfg),
		},
	}
	cmds.Run(globalFlags.Args()...)

	// Output:
	// Using config file: mygit.config
	// Interactive mode enabled.
	// Usage: commit [-h] …
	//
	// 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…
}