Skip to content

Commit

Permalink
chore: optimize code and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Feb 20, 2023
1 parent aba9252 commit b851044
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
14 changes: 14 additions & 0 deletions lib/launcher/flags/flags.go
@@ -1,6 +1,8 @@
// Package flags ...
package flags

import "strings"

// Flag name of a command line argument of the browser, also known as command line flag or switch.
// List of available flags: https://peter.sh/experiments/chromium-command-line-switches
type Flag string
Expand Down Expand Up @@ -48,3 +50,15 @@ const (
// The "http://a.com" and "http://b.com" are the arguments
Arguments Flag = ""
)

// Check if the flag name is valid
func (f Flag) Check() {
if strings.Contains(string(f), "=") {
panic("flag name should not contain '='")
}
}

// NormalizeFlag normalize the flag name, remove the leading dash
func (f Flag) NormalizeFlag() Flag {
return Flag(strings.TrimLeft(string(f), "-"))
}
18 changes: 7 additions & 11 deletions lib/launcher/launcher.go
Expand Up @@ -158,12 +158,12 @@ func (l *Launcher) Context(ctx context.Context) *Launcher {
return l
}

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

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

// GetFlags from settings
func (l *Launcher) GetFlags(name flags.Flag) ([]string, bool) {
flag, has := l.Flags[l.normalizeFlag(name)]
flag, has := l.Flags[name.NormalizeFlag()]
return flag, has
}

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

// Delete a flag
func (l *Launcher) Delete(name flags.Flag) *Launcher {
delete(l.Flags, l.normalizeFlag(name))
delete(l.Flags, name.NormalizeFlag())
return l
}

Expand Down Expand Up @@ -488,7 +488,3 @@ func (l *Launcher) Cleanup() {
dir := l.Get(flags.UserDataDir)
_ = os.RemoveAll(dir)
}

func (l *Launcher) normalizeFlag(name flags.Flag) flags.Flag {
return flags.Flag(strings.TrimLeft(string(name), "-"))
}

0 comments on commit b851044

Please sign in to comment.