From b8510445877030d6b2da4190cb0492079454751e Mon Sep 17 00:00:00 2001 From: Yad Smood Date: Mon, 20 Feb 2023 11:34:33 +0800 Subject: [PATCH] chore: optimize code and doc --- lib/launcher/flags/flags.go | 14 ++++++++++++++ lib/launcher/launcher.go | 18 +++++++----------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/launcher/flags/flags.go b/lib/launcher/flags/flags.go index b8c7da61..9fc7cabb 100644 --- a/lib/launcher/flags/flags.go +++ b/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 @@ -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), "-")) +} diff --git a/lib/launcher/launcher.go b/lib/launcher/launcher.go index 08a5af13..bf2fee49 100644 --- a/lib/launcher/launcher.go +++ b/lib/launcher/launcher.go @@ -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 } @@ -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 } @@ -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 } @@ -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), "-")) -}