瀏覽代碼

style(parser): simplify option creation

jguer 4 年之前
父節點
當前提交
de92f24ce5
共有 2 個文件被更改,包括 12 次插入11 次删除
  1. 1 7
      install.go
  2. 11 4
      pkg/settings/parser.go

+ 1 - 7
install.go

@@ -143,13 +143,7 @@ func install(cmdArgs *settings.Arguments, alpmHandle *alpm.Handle, ignoreProvide
 		}
 
 		if len(ignore) > 0 {
-			if arguments.Options["ignore"] == nil {
-				arguments.Options["ignore"] = &settings.Option{
-					Args: ignore.ToSlice(),
-				}
-			} else {
-				arguments.Options["ignore"].Add(ignore.ToSlice()...)
-			}
+			arguments.CreateOrAppendOption("ignore", ignore.ToSlice()...)
 		}
 	}
 

+ 11 - 4
pkg/settings/parser.go

@@ -52,6 +52,16 @@ func (a *Arguments) String() string {
 	return fmt.Sprintf("Op:%v Options:%+v Targets: %v", a.Op, a.Options, a.Targets)
 }
 
+func (a *Arguments) CreateOrAppendOption(option string, values ...string) {
+	if a.Options[option] == nil {
+		a.Options[option] = &Option{
+			Args: values,
+		}
+	} else {
+		a.Options[option].Add(values...)
+	}
+}
+
 func MakeArguments() *Arguments {
 	return &Arguments{
 		"",
@@ -166,10 +176,7 @@ func (a *Arguments) addParam(option, arg string) error {
 		return a.addOP(option)
 	}
 
-	if a.Options[option] == nil {
-		a.Options[option] = &Option{}
-	}
-	a.Options[option].Add(strings.Split(arg, ",")...)
+	a.CreateOrAppendOption(option, strings.Split(arg, ",")...)
 
 	if isGlobal(option) {
 		a.Options[option].Global = true