Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for a way to auto-generate documentation for commands #84

Closed
dominikbraun opened this issue May 26, 2021 · 4 comments
Closed

Look for a way to auto-generate documentation for commands #84

dominikbraun opened this issue May 26, 2021 · 4 comments
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested

Comments

@dominikbraun
Copy link
Owner

I'd love to auto-generate the documentation for commands from the cobra.Command.Long help text and the command's options. Maybe there's some cool way to do this?

@dominikbraun dominikbraun added documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested labels May 26, 2021
@retronav
Copy link
Contributor

Cobra includes command generation. Here are the docs:
https://github.com/spf13/cobra/blob/master/doc/README.md
https://github.com/spf13/cobra/blob/master/doc/md_docs.md

I will definitely play with that a bit and get back here!

@retronav
Copy link
Contributor

I tried generating some docs, and it works:

Generated docs:

  1. Index documentation
    image

  2. Document of a command
    image

  3. Documentation of a subcommand
    image

Steps to generate documentation

  1. Create a script file (in a separate directory)
  2. Paste the main function from main.go, but with some modifications
package main

import (
	"log"

	"github.com/dominikbraun/timetrace/cli"
	"github.com/dominikbraun/timetrace/config"
	"github.com/dominikbraun/timetrace/core"
	"github.com/dominikbraun/timetrace/fs"
	"github.com/dominikbraun/timetrace/out"
	"github.com/spf13/cobra/doc"
)

var version = "UNDEFINED"

func main() {
	c, err := config.FromFile()
	if err != nil {
		out.Warn("%s", err.Error())
	}

	filesystem := fs.New(c)
	timetrace := core.New(c, filesystem)

	cmd := cli.RootCommand(timetrace, version)
	err = doc.GenMarkdownTree(cmd, "./docs")
	if err != nil {
		log.Fatal(err)
	}
}
  1. Running that script with go run places docs in the docs directory in cwd

This was the process. We can incorporate it in CI with a bit of refining. @dominikbraun Any suggestions?

@dominikbraun
Copy link
Owner Author

Wow, thanks! This looks exactly like what I've been looking for. I'm going to open some issues for implementing this 😄

@dominikbraun
Copy link
Owner Author

I'm going to close this since we found a way to auto-generate docs. The subsequent issue is #87. Thanks again for figuring this out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants