将命令行参数中添加 * 号会添加额外的参数。

huangapple go评论70阅读模式
英文:

Adding * to command line arguments adds additional arguments

问题

我正在尝试创建一个简单的命令行计算器,它接受3个参数:operator int1 int2,其中operator可以是+、-、、/。当我将作为参数时,它会添加额外的参数。我该如何读取参数中的*?

操作系统:Ubuntu x64
构建命令:go build main.go

使用+作为命令行参数执行:
运行命令:./main + 2 2
输出:[./main + 2 2]

使用*作为命令行参数执行:
运行命令:./main * 2 2
输出:[./main build main main.go main.go.bak 2 2]

示例代码:

package main

import (
  "os"
  "fmt"
)

func main() {
  args := os.Args
  fmt.Println(args)
}
英文:

I'm trying to create a simple command line calculator which takes in 3 arguments operator int1 int2 where operator is can be either +,-,*,/ whenever I add * to the arguments it adds additional arguments. How would I go about reading * from arguments.

OS: Ubuntu x64
Build Command: go build main.go

Executed with + as a command line argument
Run Command: ./main + 2 2
Output: [./main + 2 2]

Executed with * as a command line argument
Run Command: ./main * 2 2
Output: [./main build main main.go main.go.bak 2 2]

Example Code

package main

import (
  "os"
  "fmt"
)

func main() {
  args := os.Args
  fmt.Println(args)
}

答案1

得分: 1

在命令行中,*会在实际运行命令之前被替换为当前目录中的所有文件和目录。为了避免这种情况,可以使用.\main ""*" 2 2"来代替。

英文:

On the command line, * is replaced with all files and directories in the current directory before the command is actually ran. In order to avoid this, instead do .\main "*" 2 2

huangapple
  • 本文由 发表于 2022年8月26日 07:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73494629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定