繁体   English   中英

在 windows 上使用 exec.Command 进行更新

[英]Go use of exec.Command on windows for noverify

我想使用 VKCOM/ noverify来分析代码。 使用此命令从命令行(windows dos shell)调用它有效

 noverify.exe -exclude-checks arraySyntax,phpdocLint 
              -output result.txt 
              C:\Dev\PHP\ResourceSpace_9_0_13357\include

问题是我无法将参数传递给cmnd := exec.Command("noverify.exe", args)

options := " -exclude-checks arraySyntax, PHPDoc"
pathToCode := "C:\\Dev\\PHP\\ResourceSpace_9_0_13357\\include"

// this works
cmnd := exec.Command("noverify.exe", pathToCode)


args := []string{options, pathToCode}
arg := strings.Join(args, "")
// passing options does not work
// cmnd := exec.Command("noverify.exe", arg)    

b, err := cmnd.CombinedOutput()

我试过什么

你可以在这个 gist 中找到我的源代码尽管上面的分隔符是空的,但 args 似乎被连接为一个由 分隔的字符串。

问题

  1. 如何将多个参数传递给exec.Comman("yourFoo.exe", cmdArgs...)
  2. 为什么我的尝试在 Windows 上不起作用?

有多个选项可以将参数传递给 exec.Command:

您可以使用多个字符串作为参数:

cmd := exec.Command("your-command", "arg1", "arg2")

如果你有一个参数切片,你可以使用扩展运算符

args := []string{"-exclude-checks", "arraySyntax,phpdocLint", "-output", "result.txt", "your-path"}
cmd := exec.Command("your-command", args...)

问题二:在你的代码中

options := " -exclude-checks arraySyntax, PHPDoc"
pathToCode := "C:\\Dev\\PHP\\ResourceSpace_9_0_13357\\include"
    
args := []string{options, pathToCode}

您将两个选项传递给外部程序。 如果你在命令行上写了同样的,你就通过了

your-command.exe " -exclude-checks arraySyntax, PHPDoc" "your-path"

这不起作用,这也是您的程序不起作用的原因。

简而言之,无论您在命令中的任何位置放置空格,都需要为exec.Command提供一个单独的参数。 示例也执行此操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM