Skip to content

Commit b3ecfe1

Browse files
committed
annotation to override default value
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
1 parent 8ced391 commit b3ecfe1

6 files changed

Lines changed: 30 additions & 8 deletions

File tree

annotation/annotation.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ const (
2020
// CodeDelimiter specifies the char that will be converted as code backtick.
2121
// Can be used on cmd for inheritance or a specific flag.
2222
CodeDelimiter = "docs.code-delimiter"
23+
// DefaultValue specifies the default value for a flag.
24+
DefaultValue = "docs.default-value"
2325
)

clidocstool_md.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,14 @@ func mdCmdOutput(cmd *cobra.Command, old string) (string, error) {
202202
}
203203

204204
var defval string
205-
if f.DefValue != "" && (f.Value.Type() != "bool" && f.DefValue != "true") && f.DefValue != "[]" {
205+
if v, ok := f.Annotations[annotation.DefaultValue]; ok && len(v) > 0 {
206+
defval = v[0]
207+
if cd, ok := f.Annotations[annotation.CodeDelimiter]; ok {
208+
defval = strings.ReplaceAll(defval, cd[0], "`")
209+
} else if cd, ok := cmd.Annotations[annotation.CodeDelimiter]; ok {
210+
defval = strings.ReplaceAll(defval, cd, "`")
211+
}
212+
} else if f.DefValue != "" && (f.Value.Type() != "bool" && f.DefValue != "true") && f.DefValue != "[]" {
206213
defval = "`" + f.DefValue + "`"
207214
}
208215

clidocstool_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func init() {
106106
buildxBuildFlags.StringArrayP("output", "o", []string{}, `Output destination (format: "type=local,dest=path")`)
107107

108108
buildxBuildFlags.StringArray("platform", []string{}, "Set target platform for build")
109+
buildxBuildFlags.SetAnnotation("platform", annotation.DefaultValue, []string{"local"})
109110

110111
buildxBuildFlags.Bool("push", false, `Shorthand for "--output=type=registry"`)
111112

clidocstool_yaml.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,25 @@ func genFlagResult(cmd *cobra.Command, flags *pflag.FlagSet, anchors map[string]
269269

270270
flags.VisitAll(func(flag *pflag.Flag) {
271271
opt = cmdOption{
272-
Option: flag.Name,
273-
ValueType: flag.Value.Type(),
274-
DefaultValue: forceMultiLine(flag.DefValue, defaultValueMaxWidth),
275-
Deprecated: len(flag.Deprecated) > 0,
276-
Hidden: flag.Hidden,
272+
Option: flag.Name,
273+
ValueType: flag.Value.Type(),
274+
Deprecated: len(flag.Deprecated) > 0,
275+
Hidden: flag.Hidden,
277276
}
278277

278+
var defval string
279+
if v, ok := flag.Annotations[annotation.DefaultValue]; ok && len(v) > 0 {
280+
defval = v[0]
281+
if cd, ok := flag.Annotations[annotation.CodeDelimiter]; ok {
282+
defval = strings.ReplaceAll(defval, cd[0], "`")
283+
} else if cd, ok := cmd.Annotations[annotation.CodeDelimiter]; ok {
284+
defval = strings.ReplaceAll(defval, cd, "`")
285+
}
286+
} else {
287+
defval = flag.DefValue
288+
}
289+
opt.DefaultValue = forceMultiLine(defval, defaultValueMaxWidth)
290+
279291
usage := flag.Usage
280292
if cd, ok := flag.Annotations[annotation.CodeDelimiter]; ok {
281293
usage = strings.ReplaceAll(usage, cd[0], "`")

fixtures/buildx_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Start a build
2424
| `--load` | | | Shorthand for `--output=type=docker` |
2525
| `--network` | `string` | `default` | Set the networking mode for the `RUN` instructions during build |
2626
| `-o`, `--output` | `stringArray` | | Output destination (format: `type=local,dest=path`) |
27-
| `--platform` | `stringArray` | | Set target platform for build |
27+
| `--platform` | `stringArray` | local | Set target platform for build |
2828
| `--push` | | | Shorthand for `--output=type=registry` |
2929
| `-q`, `--quiet` | | | Suppress the build output and print image ID on success |
3030
| `--secret` | `stringArray` | | Secret file to expose to the build (format: `id=mysecret,src=/local/secret`) |

fixtures/docker_buildx_build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ options:
232232
swarm: false
233233
- option: platform
234234
value_type: stringArray
235-
default_value: '[]'
235+
default_value: local
236236
description: Set target platform for build
237237
deprecated: false
238238
hidden: false

0 commit comments

Comments
 (0)