Files
devvm/internal/cli/flags.go
T
w3cjandClaude Opus 5.5 0b03f334fb feat(cli): help in groups, with short texts, examples and each command's docs page
devvm --help lists the commands in groups: Your VM, Change your VM,
Images, Disk space and This machine, with help under Cobra's Additional
Commands. A group whose commands don't exist yet, such as Links between
VMs, stays out of the help.

Each command's Long is one to three sentences, and the rules it held are
on the command's docs page. Every visible command has an Example, with
the most common invocations first, each with a comment. Flag
descriptions are shorter. Each help ends with the address of the
command's docs page, and the root's also ends with the issue tracker.

- internal/cli/help.go: commandGroups, addGroups, addDocs (Cobra's usage
  template with Docs and Issues sections), examples
- internal/msg/docs.go: commandDocs, CommandDoc, CommandDocURL,
  IssuesURL; TestDocLinks checks commandDocs (user pages only)
- docgen: each reference page has a Docs section before See also
- docs: commands.md in the help's groups, with new sections on
  restarting and stopping and on deleting a VM; install.md has doctor's
  engine, feature values, Docker and CA notes; Contributing describes a
  command's help
- tests: TestExamples, TestHelpGroupsAndDocs, TestAddGroups,
  TestWithDocs, TestCheckCommandDocs

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-01 04:39:39 -08:00

85 lines
2.6 KiB
Go

package cli
import (
"os"
"slices"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/w3cj/devvm/internal/from"
"github.com/w3cj/devvm/internal/msg"
"github.com/w3cj/devvm/internal/ui"
)
// addBackendFlag adds --backend, the backend a command uses: a new VM's,
// or the one it builds, deletes or checks on.
func addBackendFlag(f *pflag.FlagSet, p *string) {
f.StringVar(p, "backend", "", "the backend `NAME` to use (default: devvm setup's, else lima)")
}
// addBackendFilterFlag adds --backend as a filter: a command that acts on
// every backend acts on that one alone.
func addBackendFilterFlag(f *pflag.FlagSet, p *string) {
f.StringVar(p, "backend", "", "act only on the backend `NAME` (default: every backend)")
}
// addFromFlag adds --from, the container image a base starts from.
func addFromFlag(f *pflag.FlagSet, p *string) {
f.StringVar(p, "from", "", "the container `IMAGE` base starts from, such as node:24, instead of Ubuntu's cloud image (default: $DEVVM_FROM)")
}
// addForceFlag adds -f/--force, which only skips a confirmation.
func addForceFlag(f *pflag.FlagSet, p *bool) {
f.BoolVarP(p, "force", "f", false, "don't ask to confirm, which devvm needs without a terminal")
}
// addSSHConfigFlag adds --ssh-config, which answers the question whether
// to add devvm's Include line to ~/.ssh/config.
func addSSHConfigFlag(f *pflag.FlagSet, p *bool) {
f.BoolVar(p, "ssh-config", false, "add devvm's Include line to ~/.ssh/config without asking")
}
// confirmed is whether a destructive command (command, as typed) goes
// ahead: with --force without asking; in a terminal, when ask says yes;
// without one, never, refused with force_needed.
func confirmed(u *ui.UI, force bool, command string, ask func() bool) (bool, error) {
switch {
case force:
return true, nil
case !u.StdinIsTerminal():
return false, msg.ForceNeeded(command)
}
return ask(), nil
}
// fromRef is --from as given, else its default from the environment.
func fromRef(flag string) string {
if flag != "" {
return flag
}
return os.Getenv(from.Env)
}
// noBlanks refuses a flag given an empty value, and an empty argument: no
// flag or argument takes a blank to mean none or its default. A command
// that passes its arguments on as they are (limactl) is left alone.
func noBlanks(cmd *cobra.Command, args []string) error {
if cmd.DisableFlagParsing {
return nil
}
var err error
cmd.Flags().Visit(func(f *pflag.Flag) {
if err == nil && f.Value.Type() == "string" && f.Value.String() == "" {
err = msg.FlagBlank(f.Name)
}
})
if err != nil {
return err
}
if slices.Contains(args, "") {
return msg.ArgBlank(cmd.CommandPath())
}
return nil
}