Skip to content

Commit ce04765

Browse files
committed
allow "=true" and "=false" for all boolean flags
1 parent 58e2734 commit ce04765

File tree

2 files changed

+153
-91
lines changed

2 files changed

+153
-91
lines changed

internal/logger/logger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ func OutputOptionsForArgs(osArgs []string) OutputOptions {
622622
switch arg {
623623
case "--color=false":
624624
options.Color = ColorNever
625-
case "--color=true":
625+
case "--color=true", "--color":
626626
options.Color = ColorAlways
627627
case "--log-level=info":
628628
options.LogLevel = LevelInfo

pkg/cli/cli_impl.go

+152-90
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,32 @@ type parseOptionsExtras struct {
4646
mangleCache *string
4747
}
4848

49+
func isBoolFlag(arg string, flag string) bool {
50+
if strings.HasPrefix(arg, flag) {
51+
remainder := arg[len(flag):]
52+
return len(remainder) == 0 || remainder[0] == '='
53+
}
54+
return false
55+
}
56+
57+
func parseBoolFlag(arg string, defaultValue bool) (bool, *cli_helpers.ErrorWithNote) {
58+
equals := strings.IndexByte(arg, '=')
59+
if equals == -1 {
60+
return defaultValue, nil
61+
}
62+
value := arg[equals+1:]
63+
switch value {
64+
case "false":
65+
return false, nil
66+
case "true":
67+
return true, nil
68+
}
69+
return false, cli_helpers.MakeErrorWithNote(
70+
fmt.Sprintf("Invalid value %q in %q", value, arg),
71+
"Valid values are \"true\" or \"false\".",
72+
)
73+
}
74+
4975
func parseOptionsImpl(
5076
osArgs []string,
5177
buildOpts *api.BuildOptions,
@@ -57,51 +83,81 @@ func parseOptionsImpl(
5783
// Parse the arguments now that we know what we're parsing
5884
for _, arg := range osArgs {
5985
switch {
60-
case arg == "--bundle" && buildOpts != nil:
61-
buildOpts.Bundle = true
86+
case isBoolFlag(arg, "--bundle") && buildOpts != nil:
87+
if value, err := parseBoolFlag(arg, true); err != nil {
88+
return parseOptionsExtras{}, err
89+
} else {
90+
buildOpts.Bundle = value
91+
}
6292

63-
case arg == "--preserve-symlinks" && buildOpts != nil:
64-
buildOpts.PreserveSymlinks = true
93+
case isBoolFlag(arg, "--preserve-symlinks") && buildOpts != nil:
94+
if value, err := parseBoolFlag(arg, true); err != nil {
95+
return parseOptionsExtras{}, err
96+
} else {
97+
buildOpts.PreserveSymlinks = value
98+
}
6599

66-
case arg == "--splitting" && buildOpts != nil:
67-
buildOpts.Splitting = true
100+
case isBoolFlag(arg, "--splitting") && buildOpts != nil:
101+
if value, err := parseBoolFlag(arg, true); err != nil {
102+
return parseOptionsExtras{}, err
103+
} else {
104+
buildOpts.Splitting = value
105+
}
68106

69-
case arg == "--allow-overwrite" && buildOpts != nil:
70-
buildOpts.AllowOverwrite = true
107+
case isBoolFlag(arg, "--allow-overwrite") && buildOpts != nil:
108+
if value, err := parseBoolFlag(arg, true); err != nil {
109+
return parseOptionsExtras{}, err
110+
} else {
111+
buildOpts.AllowOverwrite = value
112+
}
71113

72-
case arg == "--watch" && buildOpts != nil:
73-
buildOpts.Watch = &api.WatchMode{}
114+
case isBoolFlag(arg, "--watch") && buildOpts != nil:
115+
if value, err := parseBoolFlag(arg, true); err != nil {
116+
return parseOptionsExtras{}, err
117+
} else if value {
118+
buildOpts.Watch = &api.WatchMode{}
119+
} else {
120+
buildOpts.Watch = nil
121+
}
74122

75-
case arg == "--minify":
76-
if buildOpts != nil {
77-
buildOpts.MinifySyntax = true
78-
buildOpts.MinifyWhitespace = true
79-
buildOpts.MinifyIdentifiers = true
123+
case isBoolFlag(arg, "--minify"):
124+
if value, err := parseBoolFlag(arg, true); err != nil {
125+
return parseOptionsExtras{}, err
126+
} else if buildOpts != nil {
127+
buildOpts.MinifySyntax = value
128+
buildOpts.MinifyWhitespace = value
129+
buildOpts.MinifyIdentifiers = value
80130
} else {
81-
transformOpts.MinifySyntax = true
82-
transformOpts.MinifyWhitespace = true
83-
transformOpts.MinifyIdentifiers = true
131+
transformOpts.MinifySyntax = value
132+
transformOpts.MinifyWhitespace = value
133+
transformOpts.MinifyIdentifiers = value
84134
}
85135

86-
case arg == "--minify-syntax":
87-
if buildOpts != nil {
88-
buildOpts.MinifySyntax = true
136+
case isBoolFlag(arg, "--minify-syntax"):
137+
if value, err := parseBoolFlag(arg, true); err != nil {
138+
return parseOptionsExtras{}, err
139+
} else if buildOpts != nil {
140+
buildOpts.MinifySyntax = value
89141
} else {
90-
transformOpts.MinifySyntax = true
142+
transformOpts.MinifySyntax = value
91143
}
92144

93-
case arg == "--minify-whitespace":
94-
if buildOpts != nil {
95-
buildOpts.MinifyWhitespace = true
145+
case isBoolFlag(arg, "--minify-whitespace"):
146+
if value, err := parseBoolFlag(arg, true); err != nil {
147+
return parseOptionsExtras{}, err
148+
} else if buildOpts != nil {
149+
buildOpts.MinifyWhitespace = value
96150
} else {
97-
transformOpts.MinifyWhitespace = true
151+
transformOpts.MinifyWhitespace = value
98152
}
99153

100-
case arg == "--minify-identifiers":
101-
if buildOpts != nil {
102-
buildOpts.MinifyIdentifiers = true
154+
case isBoolFlag(arg, "--minify-identifiers"):
155+
if value, err := parseBoolFlag(arg, true); err != nil {
156+
return parseOptionsExtras{}, err
157+
} else if buildOpts != nil {
158+
buildOpts.MinifyIdentifiers = value
103159
} else {
104-
transformOpts.MinifyIdentifiers = true
160+
transformOpts.MinifyIdentifiers = value
105161
}
106162

107163
case strings.HasPrefix(arg, "--mangle-props="):
@@ -192,38 +248,39 @@ func parseOptionsImpl(
192248
)
193249
}
194250

195-
case strings.HasPrefix(arg, "--tree-shaking="):
196-
var value *api.TreeShaking
197-
if buildOpts != nil {
198-
value = &buildOpts.TreeShaking
251+
case isBoolFlag(arg, "--tree-shaking"):
252+
if value, err := parseBoolFlag(arg, true); err != nil {
253+
return parseOptionsExtras{}, err
199254
} else {
200-
value = &transformOpts.TreeShaking
201-
}
202-
name := arg[len("--tree-shaking="):]
203-
switch name {
204-
case "false":
205-
*value = api.TreeShakingFalse
206-
case "true":
207-
*value = api.TreeShakingTrue
208-
default:
209-
return parseOptionsExtras{}, cli_helpers.MakeErrorWithNote(
210-
fmt.Sprintf("Invalid value %q in %q", name, arg),
211-
"Valid values are \"true\" or \"false\".",
212-
)
255+
var treeShaking *api.TreeShaking
256+
if buildOpts != nil {
257+
treeShaking = &buildOpts.TreeShaking
258+
} else {
259+
treeShaking = &transformOpts.TreeShaking
260+
}
261+
if value {
262+
*treeShaking = api.TreeShakingTrue
263+
} else {
264+
*treeShaking = api.TreeShakingFalse
265+
}
213266
}
214267

215-
case arg == "--ignore-annotations":
216-
if buildOpts != nil {
217-
buildOpts.IgnoreAnnotations = true
268+
case isBoolFlag(arg, "--ignore-annotations"):
269+
if value, err := parseBoolFlag(arg, true); err != nil {
270+
return parseOptionsExtras{}, err
271+
} else if buildOpts != nil {
272+
buildOpts.IgnoreAnnotations = value
218273
} else {
219-
transformOpts.IgnoreAnnotations = true
274+
transformOpts.IgnoreAnnotations = value
220275
}
221276

222-
case arg == "--keep-names":
223-
if buildOpts != nil {
224-
buildOpts.KeepNames = true
277+
case isBoolFlag(arg, "--keep-names"):
278+
if value, err := parseBoolFlag(arg, true); err != nil {
279+
return parseOptionsExtras{}, err
280+
} else if buildOpts != nil {
281+
buildOpts.KeepNames = value
225282
} else {
226-
transformOpts.KeepNames = true
283+
transformOpts.KeepNames = value
227284
}
228285

229286
case arg == "--sourcemap":
@@ -267,24 +324,21 @@ func parseOptionsImpl(
267324
transformOpts.SourceRoot = sourceRoot
268325
}
269326

270-
case strings.HasPrefix(arg, "--sources-content="):
271-
value := arg[len("--sources-content="):]
272-
var sourcesContent api.SourcesContent
273-
switch value {
274-
case "false":
275-
sourcesContent = api.SourcesContentExclude
276-
case "true":
277-
sourcesContent = api.SourcesContentInclude
278-
default:
279-
return parseOptionsExtras{}, cli_helpers.MakeErrorWithNote(
280-
fmt.Sprintf("Invalid value %q in %q", value, arg),
281-
"Valid values are \"true\" or \"false\".",
282-
)
283-
}
284-
if buildOpts != nil {
285-
buildOpts.SourcesContent = sourcesContent
327+
case isBoolFlag(arg, "--sources-content"):
328+
if value, err := parseBoolFlag(arg, true); err != nil {
329+
return parseOptionsExtras{}, err
286330
} else {
287-
transformOpts.SourcesContent = sourcesContent
331+
var sourcesContent *api.SourcesContent
332+
if buildOpts != nil {
333+
sourcesContent = &buildOpts.SourcesContent
334+
} else {
335+
sourcesContent = &transformOpts.SourcesContent
336+
}
337+
if value {
338+
*sourcesContent = api.SourcesContentInclude
339+
} else {
340+
*sourcesContent = api.SourcesContentExclude
341+
}
288342
}
289343

290344
case strings.HasPrefix(arg, "--sourcefile="):
@@ -569,24 +623,21 @@ func parseOptionsImpl(
569623
}
570624

571625
// Make sure this stays in sync with "PrintErrorToStderr"
572-
case strings.HasPrefix(arg, "--color="):
573-
value := arg[len("--color="):]
574-
var color api.StderrColor
575-
switch value {
576-
case "false":
577-
color = api.ColorNever
578-
case "true":
579-
color = api.ColorAlways
580-
default:
581-
return parseOptionsExtras{}, cli_helpers.MakeErrorWithNote(
582-
fmt.Sprintf("Invalid value %q in %q", value, arg),
583-
"Valid values are \"true\" or \"false\".",
584-
)
585-
}
586-
if buildOpts != nil {
587-
buildOpts.Color = color
626+
case isBoolFlag(arg, "--color"):
627+
if value, err := parseBoolFlag(arg, true); err != nil {
628+
return parseOptionsExtras{}, err
588629
} else {
589-
transformOpts.Color = color
630+
var color *api.StderrColor
631+
if buildOpts != nil {
632+
color = &buildOpts.Color
633+
} else {
634+
color = &transformOpts.Color
635+
}
636+
if value {
637+
*color = api.ColorAlways
638+
} else {
639+
*color = api.ColorNever
640+
}
590641
}
591642

592643
// Make sure this stays in sync with "PrintErrorToStderr"
@@ -641,7 +692,6 @@ func parseOptionsImpl(
641692
"bundle": true,
642693
"ignore-annotations": true,
643694
"keep-names": true,
644-
"metafile": true,
645695
"minify-identifiers": true,
646696
"minify-syntax": true,
647697
"minify-whitespace": true,
@@ -653,8 +703,10 @@ func parseOptionsImpl(
653703
}
654704

655705
equals := map[string]bool{
706+
"allow-overwrite": true,
656707
"asset-names": true,
657708
"banner": true,
709+
"bundle": true,
658710
"charset": true,
659711
"chunk-names": true,
660712
"color": true,
@@ -663,31 +715,41 @@ func parseOptionsImpl(
663715
"footer": true,
664716
"format": true,
665717
"global-name": true,
718+
"ignore-annotations": true,
666719
"jsx-factory": true,
667720
"jsx-fragment": true,
668721
"jsx": true,
722+
"keep-names": true,
669723
"legal-comments": true,
670724
"loader": true,
671725
"log-level": true,
672726
"log-limit": true,
673727
"main-fields": true,
674728
"mangle-cache": true,
675729
"mangle-props": true,
730+
"metafile": true,
731+
"minify-identifiers": true,
732+
"minify-syntax": true,
733+
"minify-whitespace": true,
734+
"minify": true,
676735
"outbase": true,
677736
"outdir": true,
678737
"outfile": true,
679738
"platform": true,
739+
"preserve-symlinks": true,
680740
"public-path": true,
681741
"reserve-props": true,
682742
"resolve-extensions": true,
683743
"source-root": true,
684744
"sourcefile": true,
685745
"sourcemap": true,
686746
"sources-content": true,
747+
"splitting": true,
687748
"target": true,
688749
"tree-shaking": true,
689750
"tsconfig-raw": true,
690751
"tsconfig": true,
752+
"watch": true,
691753
}
692754

693755
colon := map[string]bool{

0 commit comments

Comments
 (0)