Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions documentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (c *documentationCommand) writeIndex(w io.Writer) error {
}
// TODO: handle subcommands ??
}
_, err = fmt.Fprintf(w, "---\n\n")
_, err = fmt.Fprintf(w, "\n---\n\n")
return err
}

Expand Down Expand Up @@ -380,7 +380,7 @@ func (c *documentationCommand) formatCommand(ref commandReference, title bool, c
return fmt.Sprintf("%s%s", prefix, target)
},
LinkForSubcommand: func(s string) string {
return c.linkForCommand(strings.Join(append(commandSeq[1:], s), "_"))
return c.linkForCommand(strings.Join(append(commandSeq[1:], s), "-"))
},
})
return buf.String()
Expand Down
16 changes: 7 additions & 9 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ func PrintMarkdown(w io.Writer, cmd InfoCommand, opts MarkdownOptions) error {
fmt.Fprintln(&doc)

// Usage
if strings.TrimSpace(info.Args) != "" {
fmt.Fprintln(&doc, "## Usage")
fmt.Fprintf(&doc, "```")
fmt.Fprint(&doc, opts.UsagePrefix)
fmt.Fprintf(&doc, "%s [%ss] %s", info.Name, getFlagsName(info.FlagKnownAs), info.Args)
fmt.Fprintf(&doc, "```")
fmt.Fprintln(&doc)
fmt.Fprintln(&doc)
}
fmt.Fprintln(&doc, "## Usage")
fmt.Fprintf(&doc, "```")
fmt.Fprint(&doc, opts.UsagePrefix)
fmt.Fprintf(&doc, "%s [%ss] %s", info.Name, getFlagsName(info.FlagKnownAs), info.Args)
fmt.Fprintf(&doc, "```")
fmt.Fprintln(&doc)
fmt.Fprintln(&doc)

// Options
printFlags(&doc, cmd)
Expand Down
56 changes: 56 additions & 0 deletions markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,59 @@ func (*markdownSuite) TestOutput(c *gc.C) {
c.Assert(err, jc.ErrorIsNil)
c.Check(buf.String(), gc.Equals, string(expected))
}

// TestOutputWithoutArgs tests that the output of the PrintMarkdown function is
// correct when a command does not need arguments, e.g. list commands.
func (*markdownSuite) TestOutputWithoutArgs(c *gc.C) {
seeAlso := []string{"add-cloud", "update-cloud", "remove-cloud", "update-credential"}
subcommands := map[string]string{
"foo": "foo the bar baz",
"bar": "bar the baz foo",
"baz": "baz the foo bar",
}

command := &docTestCommand{
info: &cmd.Info{
Name: "clouds",
Args: "", //Empty args should still result in a usage field.
Purpose: "List clouds.",
Doc: "details for clouds...",
Examples: "examples for clouds...",
SeeAlso: seeAlso,
Aliases: []string{"list-clouds"},
Subcommands: subcommands,
},
}

// These functions verify the provided argument is in the expected set.
linkForCommand := func(s string) string {
for _, cmd := range seeAlso {
if cmd == s {
return "https://docs.com/" + cmd
}
}
c.Fatalf("linkForCommand called with unexpected command %q", s)
return ""
}

linkForSubcommand := func(s string) string {
_, ok := subcommands[s]
if !ok {
c.Fatalf("linkForSubcommand called with unexpected subcommand %q", s)
}
return "https://docs.com/clouds/" + s
}

expected, err := os.ReadFile("testdata/list-clouds.md")
c.Assert(err, jc.ErrorIsNil)

var buf bytes.Buffer
err = cmd.PrintMarkdown(&buf, command, cmd.MarkdownOptions{
Title: `Command "juju clouds"`,
UsagePrefix: "juju ",
LinkForCommand: linkForCommand,
LinkForSubcommand: linkForSubcommand,
})
c.Assert(err, jc.ErrorIsNil)
c.Check(buf.String(), gc.Equals, string(expected))
}
23 changes: 23 additions & 0 deletions testdata/list-clouds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Command "juju clouds"

> See also: [add-cloud](https://docs.com/add-cloud), [update-cloud](https://docs.com/update-cloud), [remove-cloud](https://docs.com/remove-cloud), [update-credential](https://docs.com/update-credential)

**Aliases:** list-clouds

## Summary
List clouds.

## Usage
```juju clouds [options] ```

## Examples
examples for clouds...

## Details
details for clouds...

## Subcommands
- [bar](https://docs.com/clouds/bar)
- [baz](https://docs.com/clouds/baz)
- [foo](https://docs.com/clouds/foo)