|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + docs "github.com/urfave/cli-docs/v3" |
| 9 | + "github.com/urfave/cli/v3" |
| 10 | +) |
| 11 | + |
| 12 | +//go:embed markdown_tabular.md.gotmpl |
| 13 | +var markdownTabularDocTemplate string |
| 14 | + |
| 15 | +// We have a copy of this template taken from |
| 16 | +// https://github.com/urfave/cli-docs where we remove column |
| 17 | +// "Environment variables" if it has no values. |
| 18 | +// TODO: remove this when https://github.com/urfave/cli-docs/pull/15 |
| 19 | +// is merged. |
| 20 | +func init() { |
| 21 | + docs.MarkdownTabularDocTemplate = markdownTabularDocTemplate |
| 22 | +} |
| 23 | + |
| 24 | +var printManCommand = &cli.Command{ |
| 25 | + Name: "man", |
| 26 | + Usage: "prints man file", |
| 27 | + Description: "Prints documentation of loop CLI in man format", |
| 28 | + Action: printMan, |
| 29 | + Hidden: true, |
| 30 | +} |
| 31 | + |
| 32 | +func printMan(_ context.Context, cmd *cli.Command) error { |
| 33 | + root := filterNestedHelpCommands(cmd.Root()) |
| 34 | + |
| 35 | + const userCommandsSection = 1 |
| 36 | + man, err := docs.ToManWithSection(root, userCommandsSection) |
| 37 | + if err != nil { |
| 38 | + return fmt.Errorf("failed to produce man: %w", err) |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Println(man) |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +var printMarkdownCommand = &cli.Command{ |
| 47 | + Name: "markdown", |
| 48 | + Usage: "prints markdown file", |
| 49 | + Description: "Prints documentation of loop CLI in markdown format", |
| 50 | + Action: printMarkdown, |
| 51 | + Hidden: true, |
| 52 | +} |
| 53 | + |
| 54 | +func printMarkdown(_ context.Context, cmd *cli.Command) error { |
| 55 | + root := filterNestedHelpCommands(cmd.Root()) |
| 56 | + |
| 57 | + md, err := docs.ToTabularMarkdown(root, "loop") |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to produce man: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + fmt.Println(md) |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// filterNestedHelpCommands returns a copy of cmd with nested "help" |
| 68 | +// sub-commands removed. |
| 69 | +// TODO: remove when https://github.com/urfave/cli-docs/pull/16 |
| 70 | +func filterNestedHelpCommands(cmd *cli.Command) *cli.Command { |
| 71 | + return cloneCommand(cmd, 0) |
| 72 | +} |
| 73 | + |
| 74 | +// cloneCommand clones the command, filtering out nested "help" subcommands. |
| 75 | +// TODO: remove when https://github.com/urfave/cli-docs/pull/16 |
| 76 | +func cloneCommand(cmd *cli.Command, depth int) *cli.Command { |
| 77 | + if cmd == nil { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + cloned := *cmd |
| 82 | + if len(cmd.Commands) == 0 { |
| 83 | + return &cloned |
| 84 | + } |
| 85 | + |
| 86 | + filtered := make([]*cli.Command, 0, len(cmd.Commands)) |
| 87 | + for _, sub := range cmd.Commands { |
| 88 | + if sub == nil { |
| 89 | + continue |
| 90 | + } |
| 91 | + childDepth := depth + 1 |
| 92 | + if childDepth > 0 && sub.Name == "help" { |
| 93 | + continue |
| 94 | + } |
| 95 | + filtered = append(filtered, cloneCommand(sub, childDepth)) |
| 96 | + } |
| 97 | + |
| 98 | + cloned.Commands = filtered |
| 99 | + return &cloned |
| 100 | +} |
0 commit comments