Skip to content

Commit

Permalink
feat(core):handle display oneof
Browse files Browse the repository at this point in the history
  • Loading branch information
jremy42 committed Mar 28, 2024
1 parent 127f1ab commit 47f2278
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/core/cobra_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command, group
if len(cmd.Aliases) > 0 {
cobraCmd.Annotations["Aliases"] = buildUsageAliases(b.ctx, cmd)
}

if cmd.ArgsType != nil {
cobraCmd.Annotations["UsageArgs"] = BuildUsageArgs(b.ctx, cmd, false)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/core/cobra_usage_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,28 @@ func BuildUsageArgs(ctx context.Context, cmd *Command, deprecated bool) string {
// _buildUsageArgs builds the arg usage list.
// This should not be called directly.
func _buildUsageArgs(ctx context.Context, w io.Writer, argSpecs ArgSpecs) error {
inOneOfGroup := false
lastOneOfGroup := ""

for _, argSpec := range argSpecs {

argSpecUsageLeftPart := argSpec.Name
argSpecUsageRightPart := _buildArgShort(argSpec)

if argSpec.OneOfGroup != "" {
if argSpec.OneOfGroup != lastOneOfGroup {
inOneOfGroup = true
lastOneOfGroup = argSpec.OneOfGroup
_, err := fmt.Fprintf(w, " %s (one of):\n", argSpec.OneOfGroup)
if err != nil {
return err
}
}
} else {
inOneOfGroup = false
lastOneOfGroup = ""
}

if argSpec.Default != nil {
_, doc := argSpec.Default(ctx)
argSpecUsageLeftPart = fmt.Sprintf("%s=%s", argSpecUsageLeftPart, doc)
Expand All @@ -78,6 +97,9 @@ func _buildUsageArgs(ctx context.Context, w io.Writer, argSpecs ArgSpecs) error
if argSpec.CanLoadFile {
argSpecUsageRightPart += " (Support file loading with @/path/to/file)"
}
if inOneOfGroup {
argSpecUsageLeftPart = " " + argSpecUsageLeftPart
}

_, err := fmt.Fprintf(w, " %s\t%s\n", argSpecUsageLeftPart, argSpecUsageRightPart)
if err != nil {
Expand Down

0 comments on commit 47f2278

Please sign in to comment.