Skip to content

Commit b851044

Browse files
committed
chore: optimize code and doc
1 parent aba9252 commit b851044

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

lib/launcher/flags/flags.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Package flags ...
22
package flags
33

4+
import "strings"
5+
46
// Flag name of a command line argument of the browser, also known as command line flag or switch.
57
// List of available flags: https://peter.sh/experiments/chromium-command-line-switches
68
type Flag string
@@ -48,3 +50,15 @@ const (
4850
// The "http://a.com" and "http://b.com" are the arguments
4951
Arguments Flag = ""
5052
)
53+
54+
// Check if the flag name is valid
55+
func (f Flag) Check() {
56+
if strings.Contains(string(f), "=") {
57+
panic("flag name should not contain '='")
58+
}
59+
}
60+
61+
// NormalizeFlag normalize the flag name, remove the leading dash
62+
func (f Flag) NormalizeFlag() Flag {
63+
return Flag(strings.TrimLeft(string(f), "-"))
64+
}

lib/launcher/launcher.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ func (l *Launcher) Context(ctx context.Context) *Launcher {
158158
return l
159159
}
160160

161-
// Set a command line argument to launch the browser.
161+
// Set a command line argument when launching the browser. Be careful the first argument is a flag name,
162+
// it shouldn't contain values. The values the will be joined with comma.
163+
// You can use the [Launcher.FormatArgs] to debug the final CLI arguments.
162164
func (l *Launcher) Set(name flags.Flag, values ...string) *Launcher {
163-
if strings.Contains(string(name), "=") {
164-
panic("flag name should not contain '='")
165-
}
166-
l.Flags[l.normalizeFlag(name)] = values
165+
name.Check()
166+
l.Flags[name.NormalizeFlag()] = values
167167
return l
168168
}
169169

@@ -183,7 +183,7 @@ func (l *Launcher) Has(name flags.Flag) bool {
183183

184184
// GetFlags from settings
185185
func (l *Launcher) GetFlags(name flags.Flag) ([]string, bool) {
186-
flag, has := l.Flags[l.normalizeFlag(name)]
186+
flag, has := l.Flags[name.NormalizeFlag()]
187187
return flag, has
188188
}
189189

@@ -198,7 +198,7 @@ func (l *Launcher) Append(name flags.Flag, values ...string) *Launcher {
198198

199199
// Delete a flag
200200
func (l *Launcher) Delete(name flags.Flag) *Launcher {
201-
delete(l.Flags, l.normalizeFlag(name))
201+
delete(l.Flags, name.NormalizeFlag())
202202
return l
203203
}
204204

@@ -488,7 +488,3 @@ func (l *Launcher) Cleanup() {
488488
dir := l.Get(flags.UserDataDir)
489489
_ = os.RemoveAll(dir)
490490
}
491-
492-
func (l *Launcher) normalizeFlag(name flags.Flag) flags.Flag {
493-
return flags.Flag(strings.TrimLeft(string(name), "-"))
494-
}

0 commit comments

Comments
 (0)