简体   繁体   English

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

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

I want to use VKCOM/ noverify to analyse code.我想使用 VKCOM/ noverify来分析代码。 Calling it from the command-line (windows dos shell) using this command works使用此命令从命令行(windows dos shell)调用它有效

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

The trouble is that i am unable to pass arguments to cmnd := exec.Command("noverify.exe", args)问题是我无法将参数传递给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()

What have i tried我试过什么

You can find my source code in this gist It seems that args are joined as a string seperated by , despite that the separator is empty above.你可以在这个 gist 中找到我的源代码尽管上面的分隔符是空的,但 args 似乎被连接为一个由 分隔的字符串。

Questions问题

  1. How to pass multiple arguments to exec.Comman("yourFoo.exe", cmdArgs...)如何将多个参数传递给exec.Comman("yourFoo.exe", cmdArgs...)
  2. Why is my attempt not working on windows?为什么我的尝试在 Windows 上不起作用?

There are multiple options to pass arguments to exec.Command:有多个选项可以将参数传递给 exec.Command:

You can use multiple strings as arguments:您可以使用多个字符串作为参数:

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

If you have a slice of arguments, you can use the spread operator如果你有一个参数切片,你可以使用扩展运算符

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

To question two: In your code问题二:在你的代码中

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

you're passing two options to the external program.您将两个选项传递给外部程序。 If you wrote the same on the command line, you pass如果你在命令行上写了同样的,你就通过了

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

This doesn't work, and is also the reason your program doesn't work.这不起作用,这也是您的程序不起作用的原因。

In short, wherever you put a space between in a command, you need to have a separate argument to exec.Command .简而言之,无论您在命令中的任何位置放置空格,都需要为exec.Command提供一个单独的参数。 The example also does this.示例也执行此操作。

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

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