Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Site
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software/vole.git

Self-hosted forge that lives in your Git repo.

Generate the CLI reference and help from one command table

f380ab70420ddf24f68931fa26e1288aff4acca6 · Keenan Nemetz · 23d ago · parent c284cb2c

Generate the CLI reference and help from one command table

The CLI documented itself four ways: a usage string per command group, inline
"usage: vole ..." strings in error paths, a per-group accepted-flag list, and
docs/cli.md, which was hand-written and had already drifted. `vole help` never
listed `run list`, `run show` or `run logs`, though all three exist and
`vole run -h` documented them; the reference listed them.

Each group now declares its subcommands, its flags (with argument and a one-line
description), its notes and its links in one table, in the file that parses it.
Three things render from that table:

  - `vole help` and `vole <group> -h`, which gain an Options section and now list
    every command and every flag;
  - `vole help --markdown`, which writes docs/cli.md;
  - `checkAllowed`, which refuses a flag the subcommand does not document — the
    per-group flag lists are gone, so the parser and the docs are one list.

verify regenerates docs/cli.md and fails on any difference, so the page cannot go
stale. cmd/vole/help_test.go checks that every flag has a description, that every
parsed flag is documented by some subcommand, that each subcommand accepts exactly
the flags it lists, and that the committed reference matches the binary.

Closes: 01M7359KQ6Z8W91N8WNWVV120B

cmd/vole/pages.go +37−28

@@ -22,22 +22,31 @@ import (
22 22 "rodent.software/vole/internal/format"
23 23 )
24 24
25 func pagesUsage() string {
26 return `usage: vole pages deploy [<dir>] [--branch <name>] [-m <text> | -F <file>] [--dry-run] [--remote <name>]
27 or: vole pages status [--branch <name>] [--remote <name>]
28 or: vole pages unpublish [--branch <name>] [--remote <name>]
29
30 A site is a branch: deploy replaces its tree with <dir> (default the current
31 directory), status reports what is published, and unpublish deletes the branch so
32 the site answers 404.
33
34 The site is served at <repo>.<pages_domain>, which the instance configures; if the
35 operator changed pages_ref, pass the same branch name here.`
25 var pagesGroup = &group{
26 Name: "pages",
27 Summary: "publish a branch as a static website",
28 Flags: []flagSpec{
29 {long: "branch", arg: "<name>", desc: "the branch that holds the site (default pages, or the instance's pages_ref)"},
30 {long: "remote", arg: "<name>", desc: "the remote naming the instance (default origin)"},
31 {long: "message", short: "m", arg: "<text>", desc: "commit message for the publish"},
32 {long: "file", short: "F", arg: "<file>", desc: "read the commit message from a file"},
33 {long: "dry-run", boolean: true, desc: "report what deploy would publish, and publish nothing"},
34 },
35 Subs: []subcommand{
36 {Name: "deploy", Synopsis: "pages deploy [<dir>] [--branch <name>] [-m <text> | -F <file>] [--dry-run] [--remote <name>]", Flags: []string{"branch", "remote", "message", "file", "dry-run"}, Notes: []string{"Replaces the branch's tree with <dir> (default the current directory), so a page deleted from the build stops being served."}},
37 {Name: "status|show", Synopsis: "pages status [--branch <name>] [--remote <name>]", Flags: []string{"branch", "remote"}, Notes: []string{"Reports what is published on the branch."}},
38 {Name: "unpublish|rm|remove", Synopsis: "pages unpublish [--branch <name>] [--remote <name>]", Flags: []string{"branch", "remote"}, Notes: []string{"Deletes the branch, so the site answers 404. `rm` and `remove` are accepted for the same command."}},
39 },
40 Notes: []string{
41 "The site is served at <repo>.<pages_domain>, which the instance configures;\n" +
42 "if the operator changed `pages_ref`, pass the same branch name here.",
43 },
44 See: []seeAlso{{"Sites", "sites.md"}},
36 45 }
37 46
38 47 func cmdPages(args []string) error {
39 48 if len(args) == 0 {
40 fmt.Fprintln(os.Stderr, pagesUsage())
49 fmt.Fprint(os.Stderr, renderHelpText(pagesGroup))
41 50 return usageErr{msg: "no pages subcommand given"}
42 51 }
43 52 switch args[0] {
@@ -48,20 +57,16 @@ func cmdPages(args []string) error {
48 57 case "unpublish", "rm", "remove":
49 58 return cmdPagesUnpublish(args[1:])
50 59 case "-h", "--help", "help":
51 fmt.Println(pagesUsage())
60 fmt.Print(renderHelpText(pagesGroup))
52 61 return nil
53 62 }
54 fmt.Fprintln(os.Stderr, pagesUsage())
63 fmt.Fprint(os.Stderr, renderHelpText(pagesGroup))
55 64 return usagef("unknown pages subcommand %q", args[0])
56 65 }
57 66
58 67 // siteFlags are the options all three subcommands share.
59 68 func siteFlags(args []string) (*flags, error) {
60 return parseFlags(args,
61 flagSpec{long: "branch"},
62 flagSpec{long: "remote"},
63 flagSpec{long: "help", short: "h", boolean: true},
64 )
69 return parseFlags(args, pagesGroup.Flags...)
65 70 }
66 71
67 72 // checkBranch rejects a name git would refuse, before it becomes a ref update.
@@ -97,20 +102,18 @@ func siteCLI(f *flags) (*client.Branch, string, error) {
97 102
98 103 func cmdPagesDeploy(args []string) error {
99 104 f, err := parseFlags(args,
100 flagSpec{long: "branch"},
101 flagSpec{long: "remote"},
102 flagSpec{long: "message", short: "m"},
103 flagSpec{long: "file", short: "F"},
104 flagSpec{long: "dry-run", boolean: true},
105 flagSpec{long: "help", short: "h", boolean: true},
105 pagesGroup.Flags...,
106 106 )
107 107 if err != nil {
108 108 return err
109 109 }
110 110 if f.help {
111 fmt.Println(pagesUsage())
111 fmt.Print(renderHelpText(pagesGroup))
112 112 return nil
113 113 }
114 if err := f.checkAllowed("vole pages deploy"); err != nil {
115 return err
116 }
114 117 if len(f.rest) > 1 {
115 118 return usageErr{msg: "usage: vole pages deploy [<dir>] [--branch <name>] [-m <text> | -F <file>] [--dry-run]"}
116 119 }
@@ -149,9 +152,12 @@ func cmdPagesStatus(args []string) error {
149 152 return err
150 153 }
151 154 if f.help {
152 fmt.Println(pagesUsage())
155 fmt.Print(renderHelpText(pagesGroup))
153 156 return nil
154 157 }
158 if err := f.checkAllowed("vole pages status"); err != nil {
159 return err
160 }
155 161 if len(f.rest) != 0 {
156 162 return usageErr{msg: "usage: vole pages status [--branch <name>] [--remote <name>]"}
157 163 }
@@ -204,9 +210,12 @@ func cmdPagesUnpublish(args []string) error {
204 210 return err
205 211 }
206 212 if f.help {
207 fmt.Println(pagesUsage())
213 fmt.Print(renderHelpText(pagesGroup))
208 214 return nil
209 215 }
216 if err := f.checkAllowed("vole pages unpublish"); err != nil {
217 return err
218 }
210 219 if len(f.rest) != 0 {
211 220 return usageErr{msg: "usage: vole pages unpublish [--branch <name>] [--remote <name>]"}
212 221 }