Skip to content

Cobra exporter emits invalid Usage defaults for true and negative integers #1594

Closed

Description

@slikts

The Cobra integration emits some flag defaults as bare KDL values, but Usage expects string defaults. Reproduced with the adapter pinned to Usage v7.1.3 (f357a6c096be219a3166346dbb199ab36d5be73a, Go module pseudo-version v0.0.0-20261026100501-f357a6c096be) and usage 7.1.3.

Minimal example:

package main

import (
    "fmt"

    cobrausage "github.com/jdx/usage/integrations/cobra"
    "github.com/spf13/cobra"
)

func main() {
    cmd := &cobra.Command{Use: "example"}
    cmd.Flags().Bool("enabled", true, "Enable the feature")
    // Test independently too:
    // cmd.Flags().Int("parallel", -1, "Maximum parallelism")
    fmt.Print(cobrausage.Generate(cmd))
}

For the boolean, the output includes:

flag --enabled help="Enable the feature" default=true

Passing the generated document to usage lint fails with “Expected identifier string”. When the boolean flag is replaced with the commented integer flag, the output is:

flag --parallel help="Maximum parallelism" default=-1 {
    arg <PARALLEL>
}

That fails with “Invalid usage config” / “expected string”.

Expected defaults: default="true" and default="-1".

In needsQuoting at the pinned commit, leading digits and some punctuation trigger quoting, but reserved words and signed numeric strings do not. Quoting all serialized string defaults, or extending that check, would avoid the problem. A round trip through Usage's parser would cover both cases.

AI-assisted report; both examples were run against the pinned adapter and CLI.

Activity

  1. github-actions commented on Oct 30, 2026

    @github-actions
    Contributor

    Fixed in v7.1.4 by #1595.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions