From 1da00919bcb4c7d52cabec135607271619737ad5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 14 Jul 2024 03:53:25 -0700 Subject: [PATCH] docs: add example to README (#3) Co-authored-by: Eng Zer Jun --- README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 817f481..e2a74de 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,81 @@ [![Go Reference](https://pkg.go.dev/badge/github.com/urfave/cli-docs/v3.svg)](https://pkg.go.dev/github.com/urfave/cli-docs/v3) [![Go Report Card](https://goreportcard.com/badge/github.com/urfave/cli-docs/v3)](https://goreportcard.com/report/github.com/urfave/cli-docs/v3) -urfave/cli-docs/v3 is an extended documentation library for use -with urfave/cli/v3. +urfave/cli-docs/v3 is an extended documentation library for use with urfave/cli/v3. + +## Start using + +1. Add the dependency to your project + +```sh + go get github.com/urfave/cli-docs/v3@latest + ``` + +2. Add it as import + +```diff + import ( ++ docs "github.com/urfave/cli-docs/v3" + ) +``` + +3. Now use it e.g. to generate markdown document from a command + +```go +package main + +import ( + "context" + "fmt" + "os" + + docs "github.com/urfave/cli-docs/v3" + cli "github.com/urfave/cli/v3" +) + +func main() { + app := &cli.Command{ + Name: "greet", + Usage: "say a greeting", + Action: func(ctx context.Context, c *cli.Command) error { + fmt.Println("Greetings") + return nil + }, + } + + md, err := docs.ToMarkdown(app) + if err != nil { + panic(err) + } + + fi, err := os.Create("cli-docs.md") + if err != nil { + panic(err) + } + defer fi.Close() + if _, err := fi.WriteString("# CLI\n\n" + md); err != nil { + panic(err) + } +} +``` + +This will create a file `cli-docs.md` with content: + +````md +# CLI + +# NAME + +greet - say a greeting + +# SYNOPSIS + +greet + +**Usage**: + +``` +greet [GLOBAL OPTIONS] [command [COMMAND OPTIONS]] [ARGUMENTS...] +``` + +````