Skip to content

Commit 7b66a5b

Browse files
authored
Merge pull request #115 from juju/v3
chore: Merge v3 to v4
2 parents 48cf985 + cc2574f commit 7b66a5b

File tree

2 files changed

+60
-33
lines changed

2 files changed

+60
-33
lines changed

documentation.go

+38-25
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ This command generates a markdown formatted document with all the commands, thei
2727

2828
var documentationExamples = `
2929
juju documentation
30-
juju documentation --split
31-
juju documentation --split --no-index --out /tmp/docs
32-
33-
To render markdown documentation using a list of existing
34-
commands, you can use a file with the following syntax
35-
36-
command1: id1
37-
command2: id2
38-
commandN: idN
39-
40-
For example:
41-
42-
add-cloud: 1183
43-
add-secret: 1284
44-
remove-cloud: 4344
45-
46-
Then, the urls will be populated using the ids indicated
47-
in the file above.
48-
49-
juju documentation --split --no-index --out /tmp/docs --discourse-ids /tmp/docs/myids
30+
juju documentation --split
31+
juju documentation --split --no-index --out /tmp/docs
32+
33+
To render markdown documentation using a list of existing
34+
commands, you can use a file with the following syntax
35+
36+
command1: id1
37+
command2: id2
38+
commandN: idN
39+
40+
For example:
41+
42+
add-cloud: 1183
43+
add-secret: 1284
44+
remove-cloud: 4344
45+
46+
Then, the urls will be populated using the ids indicated
47+
in the file above.
48+
49+
juju documentation --split --no-index --out /tmp/docs --discourse-ids /tmp/docs/myids
5050
`
5151

5252
type documentationCommand struct {
@@ -473,7 +473,10 @@ func (d *documentationCommand) formatFlags(c Command, info *Info) string {
473473
c.SetFlags(f)
474474
}
475475

476-
// group together all flags for a given value
476+
// group together all flags for a given value, meaning that flag which sets the same value are
477+
// grouped together and displayed with the same description, as below:
478+
//
479+
// -s, --short, --alternate-string | default value | some description.
477480
flags := make(map[interface{}]flagsByLength)
478481
f.VisitAll(func(f *gnuflag.Flag) {
479482
flags[f.Value] = append(flags[f.Value], f)
@@ -483,6 +486,9 @@ func (d *documentationCommand) formatFlags(c Command, info *Info) string {
483486
}
484487

485488
// sort the output flags by shortest name for each group.
489+
// Caution: this mean that description/default value displayed in documentation will
490+
// be the one of the shortest alias. Other will be discarded. Be careful to have the same default
491+
// values between each alias, and put the description on the shortest alias.
486492
var byName flagsByName
487493
for _, fl := range flags {
488494
sort.Sort(fl)
@@ -493,14 +499,21 @@ func (d *documentationCommand) formatFlags(c Command, info *Info) string {
493499
formatted := "| Flag | Default | Usage |\n"
494500
formatted += "| --- | --- | --- |\n"
495501
for _, fs := range byName {
496-
theFlags := ""
502+
// Collect all flag aliases (usually a short one and a plain one, like -v / --verbose)
503+
formattedFlags := ""
497504
for i, f := range fs {
498505
if i > 0 {
499-
theFlags += ", "
506+
formattedFlags += ", "
507+
}
508+
if len(f.Name) == 1 {
509+
formattedFlags += fmt.Sprintf("`-%s`", f.Name)
510+
} else {
511+
formattedFlags += fmt.Sprintf("`--%s`", f.Name)
500512
}
501-
theFlags += fmt.Sprintf("`--%s`", f.Name)
502513
}
503-
formatted += fmt.Sprintf("| %s | %s | %s |\n", theFlags,
514+
// display all the flags aliases and the default value and description of the shortest one.
515+
// Escape Markdown in description in order to display it cleanly in the final documentation.
516+
formatted += fmt.Sprintf("| %s | %s | %s |\n", formattedFlags,
504517
EscapeMarkdown(fs[0].DefValue),
505518
strings.ReplaceAll(EscapeMarkdown(fs[0].Usage), "\n", " "),
506519
)

documentation_test.go

+22-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (s *documentationSuite) TestFormatCommand(c *gc.C) {
3030
SeeAlso: []string{"clouds", "update-cloud", "remove-cloud", "update-credential"},
3131
Aliases: []string{"cloud-add", "import-cloud"},
3232
},
33-
flags: []string{"force", "format", "output"},
33+
flags: []testFlag{{name: "force", short: "f"}, {name: "format" /* no short flag */}, {name: "output", short: "o"}},
3434
},
3535
title: false,
3636
expected: (`
@@ -45,9 +45,9 @@ summary for add-cloud...
4545
### Options
4646
| Flag | Default | Usage |
4747
| --- | --- | --- |
48-
| ` + "`" + `--force` + "`" + ` | default value for "force" flag | description for "force" flag |
48+
| ` + "`" + `-f` + "`" + `, ` + "`" + `--force` + "`" + ` | default value for "force" flag | description for "force" flag |
4949
| ` + "`" + `--format` + "`" + ` | default value for "format" flag | description for "format" flag |
50-
| ` + "`" + `--output` + "`" + ` | default value for "output" flag | description for "output" flag |
50+
| ` + "`" + `-o` + "`" + `, ` + "`" + `--output` + "`" + ` | default value for "output" flag | description for "output" flag |
5151
5252
## Examples
5353
examples for add-cloud...
@@ -68,7 +68,7 @@ details for add-cloud...
6868
Doc: "insert details here...",
6969
Examples: "insert examples here...",
7070
},
71-
flags: []string{},
71+
flags: []testFlag{},
7272
},
7373
title: false,
7474
expected: (`
@@ -105,7 +105,13 @@ insert details here...
105105
// documentation output.
106106
type docTestCommand struct {
107107
info *cmd.Info
108-
flags []string
108+
flags []testFlag
109+
}
110+
111+
// testFlag is a definition for flag in test. It allows to provide an optional short name for the flag
112+
type testFlag struct {
113+
name string
114+
short string // optional
109115
}
110116

111117
func (c *docTestCommand) Info() *cmd.Info {
@@ -114,9 +120,17 @@ func (c *docTestCommand) Info() *cmd.Info {
114120

115121
func (c *docTestCommand) SetFlags(f *gnuflag.FlagSet) {
116122
for _, flag := range c.flags {
117-
f.String(flag,
118-
fmt.Sprintf("default value for %q flag", flag),
119-
fmt.Sprintf("description for %q flag", flag))
123+
var fakeValue string
124+
defaultValue := fmt.Sprintf("default value for %q flag", flag.name)
125+
description := fmt.Sprintf("description for %q flag", flag.name)
126+
f.StringVar(&fakeValue, flag.name,
127+
defaultValue,
128+
description)
129+
if flag.short != "" {
130+
f.StringVar(&fakeValue, flag.short,
131+
defaultValue,
132+
description)
133+
}
120134
}
121135
}
122136

0 commit comments

Comments
 (0)