Skip to content

Commit

Permalink
Merge pull request #91 from juju/JUJU-3275_remove_examples_wrapup
Browse files Browse the repository at this point in the history
[JUJU-3275] Examples field in documentation command is now a string block.
  • Loading branch information
juanmanuel-tirado authored Mar 16, 2023
2 parents 1f8fbf7 + c8713ca commit 48edee6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type Info struct {
Doc string

// Examples is a collection of running examples.
Examples []string
Examples string

// SeeAlso is a collection of additional commands to be checked.
SeeAlso []string
Expand Down
49 changes: 31 additions & 18 deletions documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,21 @@ func (c *documentationCommand) formatCommand(ref commandReference, title bool) s
formatted = "# " + strings.ToUpper(ref.name) + "\n"
}

var info *Info
if ref.name == "documentation" {
info = c.Info()
} else {
info = ref.command.Info()
}

// See Also
if len(ref.command.Info().SeeAlso) > 0 {
if len(info.SeeAlso) > 0 {
formatted += "## See Also\n"
prefix := "#"
if c.url != "" {
prefix = c.url + "/"
}
for _, s := range ref.command.Info().SeeAlso {
for _, s := range info.SeeAlso {
formatted += fmt.Sprintf("[%s](%s%s)\n", s, prefix, s)
}
formatted += "\n"
Expand All @@ -222,30 +229,27 @@ func (c *documentationCommand) formatCommand(ref commandReference, title bool) s
formatted += "\n"

// Summary
formatted += "## Summary\n" + ref.command.Info().Purpose + "\n\n"
formatted += "## Summary\n" + info.Purpose + "\n\n"

// Usage
if ref.command.Info().Args != "" {
formatted += "## Usage\n```" + ref.command.Info().Args + "```\n\n"
if strings.TrimSpace(info.Args) != "" {
formatted += "## Usage\n```" + info.Args + "```\n\n"
}

// Description
doc := ref.command.Info().Doc
if doc != "" {
formatted += "## Description\n" + ref.command.Info().Doc + "\n\n"
doc := info.Doc
if strings.TrimSpace(doc) != "" {
formatted += "## Description\n" + doc + "\n\n"
}

// Examples
if len(ref.command.Info().Examples) > 0 {
formatted += "## Examples\n"
for _, e := range ref.command.Info().Examples {
formatted += "`" + e + "`\n"
}
formatted += "\n"
examples := info.Examples
if strings.TrimSpace(examples) != "" {
formatted += "## Examples\n" + examples + "\n\n"
}

// Options
formattedFlags := c.formatFlags(ref.command)
formattedFlags := c.formatFlags(ref.command, info)
if len(formattedFlags) > 0 {
formatted += "## Options\n" + formattedFlags + "\n"
}
Expand All @@ -260,14 +264,23 @@ func (c *documentationCommand) formatCommand(ref commandReference, title bool) s
// the gnuflag.PrintDefaults. The code is extended here
// to permit additional formatting without modifying the
// gnuflag package.
func (d *documentationCommand) formatFlags(c Command) string {
func (d *documentationCommand) formatFlags(c Command, info *Info) string {
flagsAlias := FlagAlias(c, "")
if flagsAlias == "" {
// For backward compatibility, the default is 'flag'.
flagsAlias = "flag"
}
f := gnuflag.NewFlagSetWithFlagKnownAs(c.Info().Name, gnuflag.ContinueOnError, flagsAlias)
c.SetFlags(f)
f := gnuflag.NewFlagSetWithFlagKnownAs(info.Name, gnuflag.ContinueOnError, flagsAlias)

// if we are working with the documentation command,
// we have to set flags on a new instance, otherwise
// we will overwrite the current flag values
if info.Name != "documentation" {
c.SetFlags(f)
} else {
c = newDocumentationCommand(d.super)
c.SetFlags(f)
}

// group together all flags for a given value
flags := make(map[interface{}]flagsByLength)
Expand Down
2 changes: 1 addition & 1 deletion supercommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (c *SuperCommand) init() {
}
c.subcmds = map[string]commandReference{
"help": {command: c.help},
"documentation": {command: newDocumentationCommand(c),
"documentation": {command: c.documentation,
name: "documentation"},
}

Expand Down

0 comments on commit 48edee6

Please sign in to comment.