|
| 1 | +// Copyright The KCL Authors. All rights reserved. |
| 2 | + |
| 3 | +package cmd |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "kcl-lang.io/kcl-go/pkg/tools/gen" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + docDesc = ` |
| 14 | +This command shows documentation for KCL package or symbol. |
| 15 | +` |
| 16 | + docExample = ` # Generate document for current package |
| 17 | + kcl doc generate |
| 18 | + ` |
| 19 | + docGenDesc = ` # Generate Markdown document for current package |
| 20 | + kcl doc generate |
| 21 | +
|
| 22 | + # Generate Html document for current package |
| 23 | + kcl doc generate --format html |
| 24 | +
|
| 25 | + # Generate Markdown document for specific package |
| 26 | + kcl doc generate --file-path <package path> |
| 27 | +
|
| 28 | + # Generate Markdown document for specific package to a <target directory> |
| 29 | + kcl doc generate --file-path <package path> --target <target directory> |
| 30 | +` |
| 31 | + docGenExample = ` # Generate Markdown document for current package |
| 32 | + kcl doc generate |
| 33 | +
|
| 34 | + # Generate Html document for current package |
| 35 | + kcl doc generate --format html |
| 36 | +
|
| 37 | + # Generate Markdown document for specific package |
| 38 | + kcl doc generate --file-path <package path> |
| 39 | +
|
| 40 | + # Generate Markdown document for specific package to a <target directory> |
| 41 | + kcl doc generate --file-path <package path> --target <target directory> |
| 42 | +` |
| 43 | +) |
| 44 | + |
| 45 | +// NewDocCmd returns the doc command. |
| 46 | +func NewDocCmd() *cobra.Command { |
| 47 | + cmd := &cobra.Command{ |
| 48 | + Use: "doc", |
| 49 | + Short: "KCL document tool", |
| 50 | + Long: docDesc, |
| 51 | + Example: docExample, |
| 52 | + SilenceUsage: true, |
| 53 | + } |
| 54 | + |
| 55 | + cmd.AddCommand(NewDocGenerateCmd()) |
| 56 | + |
| 57 | + return cmd |
| 58 | +} |
| 59 | + |
| 60 | +// NewDocGenerateCmd returns the doc generate command. |
| 61 | +func NewDocGenerateCmd() *cobra.Command { |
| 62 | + o := gen.GenOpts{} |
| 63 | + cmd := &cobra.Command{ |
| 64 | + Use: "generate", |
| 65 | + Short: "Generates documents from code and examples", |
| 66 | + Long: docGenDesc, |
| 67 | + Example: docGenExample, |
| 68 | + RunE: func(*cobra.Command, []string) error { |
| 69 | + genContext, err := o.ValidateComplete() |
| 70 | + if err != nil { |
| 71 | + fmt.Println(fmt.Errorf("generate failed: %s", err)) |
| 72 | + return err |
| 73 | + } |
| 74 | + |
| 75 | + err = genContext.GenDoc() |
| 76 | + if err != nil { |
| 77 | + fmt.Println(fmt.Errorf("generate failed: %s", err)) |
| 78 | + return err |
| 79 | + } else { |
| 80 | + fmt.Printf("Generate Complete! Check generated docs in %s\n", genContext.Target) |
| 81 | + return nil |
| 82 | + } |
| 83 | + }, |
| 84 | + SilenceUsage: true, |
| 85 | + } |
| 86 | + |
| 87 | + cmd.Flags().StringVar(&o.Path, "file-path", "", |
| 88 | + `Relative or absolute path to the KCL package root when running kcl-doc command from |
| 89 | +outside of the KCL package root directory. |
| 90 | +If not specified, the current work directory will be used as the KCL package root.`) |
| 91 | + cmd.Flags().StringVar(&o.Format, "format", string(gen.Markdown), |
| 92 | + `The document format to generate. Supported values: markdown, html, openapi.`) |
| 93 | + cmd.Flags().StringVar(&o.Target, "target", "", |
| 94 | + `If not specified, the current work directory will be used. A docs/ folder will be created under the target directory.`) |
| 95 | + cmd.Flags().StringVar(&o.TemplateDir, "template", "", |
| 96 | + `The template directory based on the KCL package root. If not specified, the built-in templates will be used.`) |
| 97 | + cmd.Flags().BoolVar(&o.IgnoreDeprecated, "ignore-deprecated", false, |
| 98 | + `Do not generate documentation for deprecated schemas.`) |
| 99 | + cmd.Flags().BoolVar(&o.EscapeHtml, "escape-html", false, |
| 100 | + `Whether to escape html symbols when the output format is markdown. Always scape when the output format is html. Default to false.`) |
| 101 | + |
| 102 | + return cmd |
| 103 | +} |
0 commit comments