this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: alter flag behavior based on the command name

Rather than having each "add flags" take boolean arguments,
have the functions take the whole command and alter behavior
based on what is the command they're adding the flags to.

Either mechanism works OK so far, although boolean values without
context don't really say much about what they're meant for.
However, we want to add more variety in which flags are added
or with what default values for each flag,
so adding more boolean knobs will start scaling poorly.

One minor advantage of this change is that we centralize
the presence and default values of shared flags in flags.go.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: If334743be3bc7897d0d7efe558d57b518fa3b4d4
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1230086
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>

+33 -22
+1 -1
cmd/cue/cmd/cmd.go
··· 158 158 }), 159 159 } 160 160 161 - addInjectionFlags(cmd.Flags(), true) 161 + addInjectionFlags(cmd) 162 162 163 163 return cmd 164 164 }
+3 -3
cmd/cue/cmd/def.go
··· 35 35 RunE: mkRunE(c, runDef), 36 36 } 37 37 38 - addOutFlags(cmd.Flags(), true) 39 - addOrphanFlags(cmd.Flags()) 40 - addInjectionFlags(cmd.Flags(), false) 38 + addOutFlags(cmd) 39 + addOrphanFlags(cmd) 40 + addInjectionFlags(cmd) 41 41 42 42 cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "evaluate this expression only") 43 43
+3 -3
cmd/cue/cmd/eval.go
··· 52 52 RunE: mkRunE(c, runEval), 53 53 } 54 54 55 - addOutFlags(cmd.Flags(), true) 56 - addOrphanFlags(cmd.Flags()) 57 - addInjectionFlags(cmd.Flags(), false) 55 + addOutFlags(cmd) 56 + addOrphanFlags(cmd) 57 + addInjectionFlags(cmd) 58 58 59 59 cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "evaluate this expression only") 60 60
+3 -3
cmd/cue/cmd/export.go
··· 98 98 RunE: mkRunE(c, runExport), 99 99 } 100 100 101 - addOutFlags(cmd.Flags(), true) 102 - addOrphanFlags(cmd.Flags()) 103 - addInjectionFlags(cmd.Flags(), false) 101 + addOutFlags(cmd) 102 + addOrphanFlags(cmd) 103 + addInjectionFlags(cmd) 104 104 105 105 cmd.Flags().Bool(string(flagEscape), false, "use HTML escaping") 106 106 cmd.Flags().StringArrayP(string(flagExpression), "e", nil, "export this expression only")
+17 -6
cmd/cue/cmd/flags.go
··· 17 17 import ( 18 18 "fmt" 19 19 20 + "github.com/spf13/cobra" 20 21 "github.com/spf13/pflag" 21 22 ) 22 23 ··· 71 72 flagMemProfile flagName = "memprofile" 72 73 ) 73 74 74 - func addOutFlags(f *pflag.FlagSet, allowNonCUE bool) { 75 - if allowNonCUE { 75 + func addOutFlags(cmd *cobra.Command) { 76 + f := cmd.Flags() 77 + 78 + switch cmd.Name() { 79 + case "def", "eval", "export": 76 80 f.String(string(flagOut), "", 77 81 `output format (run 'cue help filetypes' for more info)`) 78 82 } ··· 81 85 f.BoolP(string(flagForce), "f", false, "force overwriting existing files") 82 86 } 83 87 84 - func addGlobalFlags(f *pflag.FlagSet) { 88 + func addGlobalFlags(cmd *cobra.Command) { 89 + f := cmd.PersistentFlags() 90 + 85 91 f.BoolP(string(flagSimplify), "s", false, 86 92 "simplify output") 87 93 f.BoolP(string(flagIgnore), "i", false, ··· 94 100 f.MarkHidden(string(flagMemProfile)) 95 101 } 96 102 97 - func addOrphanFlags(f *pflag.FlagSet) { 103 + func addOrphanFlags(cmd *cobra.Command) { 104 + f := cmd.Flags() 105 + 98 106 f.StringP(string(flagPackage), "p", "", "package name for non-CUE files") 99 107 f.StringP(string(flagSchema), "d", "", 100 108 "expression to select schema for evaluating values in non-CUE files") ··· 107 115 f.Bool(string(flagMerge), true, "merge non-CUE files") 108 116 } 109 117 110 - func addInjectionFlags(f *pflag.FlagSet, auto bool) { 118 + func addInjectionFlags(cmd *cobra.Command) { 119 + f := cmd.Flags() 120 + 111 121 f.StringArrayP(string(flagInject), "t", nil, 112 122 "set the value of a tagged field") 113 - f.BoolP(string(flagInjectVars), "T", auto, 123 + autoInject := cmd.Name() == "cmd" 124 + f.BoolP(string(flagInjectVars), "T", autoInject, 114 125 "inject system variables in tags") 115 126 } 116 127
+2 -2
cmd/cue/cmd/import.go
··· 254 254 RunE: mkRunE(c, runImport), 255 255 } 256 256 257 - addOutFlags(cmd.Flags(), false) 258 - addOrphanFlags(cmd.Flags()) 257 + addOutFlags(cmd) 258 + addOrphanFlags(cmd) 259 259 260 260 cmd.Flags().Bool(string(flagFiles), false, "split multiple entries into different files") 261 261 cmd.Flags().Bool(string(flagDryRun), false, "show what files would be created")
+1 -1
cmd/cue/cmd/root.go
··· 264 264 } 265 265 c.cmdCmd = newCmdCmd(c) 266 266 267 - addGlobalFlags(cmd.PersistentFlags()) 267 + addGlobalFlags(cmd) 268 268 269 269 // Cobra's --help flag shows up in help text by default, which is unnecessary. 270 270 cmd.InitDefaultHelpFlag()
+1 -1
cmd/cue/cmd/trim.go
··· 86 86 RunE: mkRunE(c, runTrim), 87 87 } 88 88 89 - addOutFlags(cmd.Flags(), false) 89 + addOutFlags(cmd) 90 90 cmd.Flags().Bool(string(flagTrace), false, "trace computation") 91 91 cmd.Flags().BoolP(string(flagDryRun), "n", false, "only run simulation") 92 92
+2 -2
cmd/cue/cmd/vet.go
··· 65 65 RunE: mkRunE(c, doVet), 66 66 } 67 67 68 - addOrphanFlags(cmd.Flags()) 69 - addInjectionFlags(cmd.Flags(), false) 68 + addOrphanFlags(cmd) 69 + addInjectionFlags(cmd) 70 70 71 71 cmd.Flags().BoolP(string(flagConcrete), "c", false, 72 72 "require the evaluation to be concrete, or set -c=false to allow incomplete values")