all repos

clerk @ f2dd916162e462998dbd1b2fa9d0cc8ce9529226

missing tooling for ledger/hledger

clerk/internal/cli/cli.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
cli: add version command; disable `h` help alias, 1 month ago
1
package cli
2
3
import (
4
	"context"
5
	"fmt"
6
	"os"
7
8
	"github.com/urfave/cli/v3"
9
)
10
11
type Cli struct {
12
	version string
13
}
14
15
func New(version string) *Cli {
16
	return &Cli{
17
		version: version,
18
	}
19
}
20
21
func (c *Cli) Run(ctx context.Context, args []string) error {
22
	cmd := &cli.Command{
23
		Name:                  "clerk",
24
		Usage:                 "missing pta tooling",
25
		Version:               c.version,
26
		HideVersion:           true,
27
		EnableShellCompletion: true,
28
		Flags: []cli.Flag{
29
			&cli.StringFlag{
30
				Name:  "config",
31
				Usage: "path to config file (defaults to clerk.toml in the current directory)",
32
			},
33
		},
34
		Commands: []*cli.Command{
35
			{
36
				Name:   "lsp",
37
				Usage:  "Run LSP server, to be used by editors",
38
				Action: c.lspAction,
39
			},
40
			{
41
				Name:                   "format",
42
				Usage:                  "Format journal files",
43
				Action:                 c.formatAction,
44
				UseShortOptionHandling: true,
45
				Arguments: []cli.Argument{&cli.StringArgs{
46
					Name:      "journals",
47
					UsageText: "(path to journal files/directories (stdin if empty))",
48
					Min:       0,
49
					Max:       -1,
50
				}},
51
				Flags: []cli.Flag{
52
					&cli.BoolFlag{
53
						Name:    "write",
54
						Aliases: []string{"w"},
55
						Usage:   "write result back to file instead of stdout",
56
					},
57
					&cli.BoolFlag{
58
						Name:    "check",
59
						Aliases: []string{"c"},
60
						Usage:   "exit code 0 if already formatted, 1 otherwise",
61
					},
62
					&cli.BoolFlag{
63
						Name:    "diff",
64
						Aliases: []string{"d"},
65
						Usage:   "display diffs instead of rewriting files",
66
					},
67
					&cli.BoolFlag{
68
						Name:    "list",
69
						Aliases: []string{"l"},
70
						Usage:   "list files whose formatting differs",
71
					},
72
				},
73
			},
74
			{
75
				Name:   "lint",
76
				Usage:  "Lint journal files",
77
				Action: c.lintAction,
78
				Flags: []cli.Flag{
79
					&cli.StringFlag{
80
						Name:    "format",
81
						Aliases: []string{"f"},
82
						Usage:   "output format: text, json",
83
						Value:   "text",
84
					},
85
					&cli.StringFlag{
86
						Name:  "path-style",
87
						Usage: "file path style: basename, relative, absolute",
88
						Value: "relative",
89
					},
90
				},
91
				Arguments: []cli.Argument{&cli.StringArgs{
92
					Name:      "journals",
93
					UsageText: "(path to journal files/directories (stdin if empty))",
94
					Min:       0,
95
					Max:       -1,
96
				}},
97
			},
98
			{
99
				Name:   "help",
100
				Usage:  "Show help",
101
				Action: func(ctx context.Context, cmd *cli.Command) error { return cli.ShowAppHelp(cmd) },
102
			},
103
			{
104
				Name:  "version",
105
				Usage: "Print clerk version",
106
				Action: func(ctx context.Context, cmd *cli.Command) error {
107
					fmt.Fprintf(os.Stdout, "clerk version %s", c.version)
108
					return nil
109
				},
110
			},
111
		},
112
	}
113
	return cmd.Run(ctx, args)
114
}