exec cue cmd -t prod -t name=bar -t enable=true -t count=5 -t mult=1.5 tag tags.cue tags_tool.cue
cmp stdout expect-stdout

# Check that the flags can be used after any arguments;
# such "interspersed" flags are supported by cobra by default.
exec cue cmd tag tags.cue tags_tool.cue -t prod -t name=bar -t enable=true -t count=5 -t mult=1.5
cmp stdout expect-stdout

# Verify that the global -t flag works with commands like "cmd"
# or "eval", but not "fmt".
exec cue -t prod -t name=bar -t enable=true -t count=5 -t mult=1.5 cmd tag tags.cue tags_tool.cue
cmp stdout expect-stdout
exec cue eval -t name=bar tags.cue
stdout 'name: *"bar"'
exec cue -t name=bar eval tags.cue
stdout 'name: *"bar"'
! exec cue fmt -t name=bar tags.cue
stderr 'unknown shorthand flag'
! exec cue -t name=bar fmt tags.cue
stderr 'unknown shorthand flag'

# Duplicate tags.
exec cue cmd -t who=foo -t who=foo hello ./hello
stdout 'Hello foo!'
! exec cue cmd -t who=foo -t who=bar hello ./hello
stderr 'who: invalid interpolation: conflicting values'

# Invalid tag injections.
! exec cue cmd -t nosuchshort tag
stderr 'tag "nosuchshort" not used in any file'
! exec cue cmd -t nosuchname=bar tag
stderr 'no tag for "nosuchname"'
! exec cue cmd -t env=notadisjunct tag
stderr '^data\.env: 2 errors in empty disjunction'
! exec cue cmd -t enable=notabool tag
stderr 'invalid boolean "notabool" for injection tag "enable"'
! exec cue cmd -t count=notanint tag
stderr 'invalid number for injection tag "count": illegal number start "notanint"'
! exec cue cmd -t count=12.34 tag
stderr 'invalid int "12.34" for injection tag "count"'
! exec cue cmd -t toolenable=notabool tag
stderr 'invalid boolean "notabool" for injection tag "toolenable"'

-- expect-stdout --
data.json: {"env":"prod","name":"bar","enable":true,"count":5,"mult":1.5}
tooldata.json: {"enable":true}
-- tags.cue --
package tags

data: {
    env: "prod" | "staging" @tag(env,short=prod|staging)
    name: string @tag(name)
    enable: _ @tag(enable,type=bool)
    count: _ @tag(count,type=int)
    mult: _ @tag(mult,type=number)
}

-- tags_tool.cue --
package tags

import (
    "encoding/json"
    "tool/cli"
)

tooldata: {
    enable: _ | *true @tag(toolenable,type=bool)
}

command: tag: cli.Print & {
    text: """
        data.json: \(json.Marshal(data))
        tooldata.json: \(json.Marshal(tooldata))
        """
}
-- hello/hello_tool.cue --
package hello

import "tool/cli"

who: string @tag(who)

command: hello: cli.Print & {
  text: "Hello \(who)!"
}
