all repos

clerk @ a65ac055446d1d21fbdbbd2b848cf8d988403514

missing tooling for ledger/hledger

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

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
change cli frameworks, 1 month ago
1
package cli
2
3
import (
4
	"context"
5
6
	"github.com/urfave/cli/v3"
7
)
8
9
type Cli struct {
10
	version string
11
}
12
13
func New(version string) *Cli {
14
	return &Cli{
15
		version: version,
16
	}
17
}
18
19
func (c *Cli) Run(ctx context.Context, args []string) error {
20
	cmd := &cli.Command{
21
		Name:                   "clerk",
22
		Usage:                  "missing pta tooling",
23
		Version:                c.version,
24
		EnableShellCompletion:  true,
25
		UseShortOptionHandling: true,
26
		Commands: []*cli.Command{
27
			{
28
				Name:   "format",
29
				Usage:  "reformat journal files",
30
				Action: c.formatAction,
31
				Arguments: []cli.Argument{&cli.StringArgs{
32
					Name:      "journals",
33
					UsageText: "(path to journal files/directories (stdin if empty))",
34
					Min:       0,
35
					Max:       -1,
36
				}},
37
				Flags: []cli.Flag{
38
					&cli.BoolFlag{
39
						Name:    "write",
40
						Aliases: []string{"w"},
41
						Usage:   "write result back to file instead of stdout",
42
					},
43
					&cli.BoolFlag{
44
						Name:    "check",
45
						Aliases: []string{"c"},
46
						Usage:   "exit code 0 if already formatted, 1 otherwise",
47
					},
48
					&cli.BoolFlag{
49
						Name:    "diff",
50
						Aliases: []string{"d"},
51
						Usage:   "display diffs instead of rewriting files",
52
					},
53
					&cli.BoolFlag{
54
						Name:    "list",
55
						Aliases: []string{"l"},
56
						Usage:   "list files whose formatting differs",
57
					},
58
				},
59
			},
60
			{
61
				Name:   "tags",
62
				Usage:  "generate a tags file for journal entries",
63
				Action: c.tagsAction,
64
				Flags: []cli.Flag{&cli.StringFlag{
65
					Name:    "out",
66
					Aliases: []string{"o"},
67
					Usage:   "output file, set to - for stdout",
68
					Value:   "tags",
69
				}},
70
				Arguments: []cli.Argument{&cli.StringArgs{
71
					Name:      "journals",
72
					UsageText: "(path to journal files/directories)",
73
					Min:       0,
74
					Max:       -1,
75
				}},
76
			},
77
			{
78
				Name:   "lint",
79
				Usage:  "lint journal files for common mistakes",
80
				Action: c.lintAction,
81
				Flags: []cli.Flag{
82
					&cli.StringFlag{
83
						Name:    "format",
84
						Aliases: []string{"f"},
85
						Usage:   "output format: text, json",
86
						Value:   "text",
87
					},
88
					&cli.StringFlag{
89
						Name:  "path-style",
90
						Usage: "file path style: basename, relative, absolute",
91
						Value: "relative",
92
					},
93
				},
94
				Arguments: []cli.Argument{&cli.StringArgs{
95
					Name:      "journals",
96
					UsageText: "(path to journal files/directories (stdin if empty))",
97
					Min:       0,
98
					Max:       -1,
99
				}},
100
			},
101
		},
102
	}
103
	return cmd.Run(ctx, args)
104
}