spf13 / cobra

A Commander for modern Go CLI interactions
https://cobra.dev
Apache License 2.0
38.17k stars 2.85k forks source link

Support double quotes `"` for input flags delimiter with --help flag #1445

Closed GuillaumeFalourd closed 2 years ago

GuillaumeFalourd commented 3 years ago

Context

We are using cobra in Ritchie CLI implementation in Golang.

What happened

We use the default command Helper to get flag options based on the command line informed.

However, returned flags always appear with single quote ' and we would like to standardise the return to double quote " as its the nomenclature we use on all OS (single quotes don't work on Windows OS).

More information in this ISSUE: https://github.com/ZupIT/ritchie-cli/issues/871

Example

108770836-95199700-7539-11eb-9e7e-7b00d05120d8

In the example above, the --help flag returns ' for command line input flags, and we would like it to return " instead.

Questions

When debugging, we understood this output is generated by the HelpFunc() function called in our project using the cmd.Help() function.

How should/could we solve this problem? Should we customize this display on the cobra repository to support what we are expecting (it may be useful for others) or should we implement this behaviour on our project? In that case, what would be the recommended way to implement this?

github-actions[bot] commented 3 years ago

This issue is being marked as stale due to a long period of inactivity

johnSchnake commented 2 years ago

Is this still an issue? Seems like a small enough issue and reasonable to promote since you state there is an OS difference in the quotes.

Did you define your own templates? I just want to be able to repro and happen to use the default templates and so I don't end up getting the single or double quote options like you do. Not sure the difference that causes yours to suggest the values.

sonobuoy gen help
...
...
Flags:
      --aggregator-node-selector nodeSelectors   Node selectors to add to the aggregator. Values can be given multiple times and are in the form key:value (default map[])
      --aggregator-permissions string            Type of aggregator permission to use in the cluster. Allowable values are [namespaced, clusterAdmin] (default "clusterAdmin")
      --config Sonobuoy config                   Path to a sonobuoy configuration JSON file.
      --context string                           Context in the kubeconfig to use.
      --dns-namespace string                     The namespace to check for DNS pods during preflight checks. (default "kube-system")
      --dns-pod-labels strings                   The label selectors to use for locating DNS pods during preflight checks. Can be specified multiple times or as a comma-separated list. (default [k8s-app=kube-dns,k8s-app=coredns])
      --e2e-focus envModifier                    Specify the E2E_FOCUS value for the e2e plugin, specifying which tests to run. Shorthand for --plugin-env=e2e.E2E_FOCUS=<string> (default \[Conformance\])
      --e2e-repo envModifier                     Specify a registry to use as the default for pulling Kubernetes test images. Same as providing --e2e-repo-config but specifying the same repo repeatedly.
      --e2e-repo-config yaml-filepath            Specify a yaml file acting as KUBE_TEST_REPO_LIST, overriding registries for test images.
      --e2e-skip envModifier                     Specify the E2E_SKIP value for the e2e plugin, specifying which tests to skip. Shorthand for --plugin-env=e2e.E2E_SKIP=<string> (default \[Disruptive\]|NoExecuteTaintManager)
  -f, --file -                                   If set, loads the file as if it were the output from sonobuoy gen. Set to - to read from stdin.
  -h, --help                                     help for gen
      --image-pull-policy string
...
...
GuillaumeFalourd commented 2 years ago

Hi @johnSchnake , thank you for commenting.

The CLI builds the command helper based on a json file. This json file is specific for each command and contains inputs that corresponds to the command flags with eventually default values.

Here is the issue json file as example.

Independently of the input being required or not, with a default value or not, the output is always between single quote when calling the HelpFunc() function in the project.

johnSchnake commented 2 years ago

Whew! So after tracking this down it doesn't seem to be a cobra issue; cobra doesn't show the default values in the usage method like that. However, I tracked down where this occurs: https://github.com/kubernetes/kubectl/blob/master/pkg/util/templates/templater.go#L231

This is invoked via: https://github.com/ZupIT/ritchie-cli/blob/main/pkg/commands/builder.go#L327

So if you want to get double quotes you can put an issue/PR with k8s, or more simply, copy the code from there that you need for the templating into your own fork/directory.

marckhouzam commented 2 years ago

Nice investigation @johnSchnake !