英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论