cue icon indicating copy to clipboard operation
cue copied to clipboard

cue cmd does not accept flags, interprets them as package

Open cueckoo opened this issue 3 years ago • 4 comments

Originally opened by @ah-edg in https://github.com/cuelang/cue/issues/925

What version of CUE are you using (cue version)?

$ cue version
cue version 0.3.2 linux/amd64
# but also
cue version 0.4.0-alpha.2 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

Ran setup_tool.cue with contents below like this: cue cmd greet --all-errors --ignore --verbose

file contents:

package example

import (
	"tool/cli"
)

command: greet: {
    task: {
        askname: cli.Ask & {
            prompt: "Enter your name"
        }
        hello: cli.Print & {
            "$after": askname
            text: "Hello \(task.askname.response)"
        }
    }
}

What did you expect to see?

A prompt for my name and a Hello X output

What did you see instead?

cannot find package "--all-errors" allthough it should work:

$ cue help cmd greet
Usage:
  cue cmd greet [flags]

Flags:
  -h, --help   help for greet

Global Flags:
  -E, --all-errors   print all available errors
  -i, --ignore       proceed in the presence of errors
  -s, --simplify     simplify output
      --strict       report errors for lossy mappings
      --trace        trace computation
  -v, --verbose      print information about progress
$

cueckoo avatar Jul 03 '21 10:07 cueckoo

Original reply by @mpvl in https://github.com/cuelang/cue/issues/925#issuecomment-828189153

This seems to be a bug in Cobra. Cobra should have generated the documentation:

cue cmd [flags] greet

The flags need to be specified before the "subcommand" greet. This behavior is enabled by Cobra's SetInterspersed(false) feature to allow future compatibility in case we want to support using flags for commands.

cueckoo avatar Jul 03 '21 11:07 cueckoo

Original reply by @mpvl in https://github.com/cuelang/cue/issues/925#issuecomment-828189892

The solution would be for someone to file/track an issue in Cobra or submit a fix to Cobra. It could be that a later version of Cobra already fixes this.

cueckoo avatar Jul 03 '21 11:07 cueckoo

--all-errors is defined as a persistent/global flag defined on "root" command. This means that it may be used on cue and on any of its subcommands including cue cmd greeting.

In the presence of subcommands Cobra allows two options:

  1. Command with flags local to cmd: cue cmd -i -T
  2. Subcommand with flags local to subcommand: cue cmd greet -i --greet-flag

And global flags can be everywhere.

Command with both local flags and subcommand is not allowed. Not sure if this is a bug in Cobra, but obviously it is a limitation and the reason why CUE needs to abuse Cobra.

Adding cmd.SetInterspersed(false) prevents from mixing flags and arguments in cue cmd. But in the presence of subcommands it does not have any effect. Command can't have both subcommans and arguments, so there is nothing to be interspersed.

Moreover, Cobra is not able to generate help message such as cue cmd [flags] greet because in general case it must be something like cue [cue-global-flags] cmd [cmd-flags | cue-global-flags] greet [cue-global-flags].


It is possible to set usage template in addCustom to something like this:

    sub.SetUsageTemplate(
        fmt.Sprintf(`Usage:
    cue cmd [flags] %v [inputs]

    Flags:
    {{.Parent.LocalFlags.FlagUsages | trimTrailingWhitespaces}}`,
        name))

This will fix cue help cmd hello:

Say hello!

Usage:
  cue cmd [flags] hello [inputs]

Flags:
  -t, --inject stringArray   set the value of a tagged field
  -T, --inject-vars          inject system variables in tags (default
true)

jorpic avatar Jul 01 '22 21:07 jorpic

Actually, I came here because of some other inconsistencies in the help messages.

$ cue help cmd

Usage:
  cue cmd <name> [inputs] [flags]
  cue cmd [command]

Available Commands:
  hello       Say hello!

Flags:
  -h, --help                 help for cmd
  -t, --inject stringArray   set the value of a tagged field
  -T, --inject-vars          inject system variables in tags (default true)

Global Flags:
  -E, --all-errors   print all available errors
  -i, --ignore       proceed in the presence of errors
  -s, --simplify     simplify output
      --strict       report errors for lossy mappings
      --trace        trace computation
  -v, --verbose      print information about progress

Use "cue cmd [command] --help" for more information about a command.
  1. cue cmd <name> [inputs] [flags] is not correct and can easily be fixed by changing Use here to
Use: "cue cmd [flags] <command> [inputs]"

Cobra notices [flags] and does not add them at the end.

  1. cue cmd [command] assumes that command is optional, but this is not the case:
$ cue cmd
cmd must be run as one of its subcommands
Run 'cue help cmd' for known subcommands.
exit status 1
  1. Use "cue cmd [command] --help" for more information about a command. is confusing as it produces error instead of more information:
$ cue cmd hello --help
cannot find package "--help"
exit status 1

jorpic avatar Jul 01 '22 21:07 jorpic