如何在if语句中使用命令行参数?

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

How do I use arguments from the command line in an if statement?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是编程的新手,golang是我学习的第一门语言,我在尝试从命令行访问变量时遇到了一些问题。我想通过用户在命令行中输入的标志来获取输入,并在if语句中使用这些变量。我希望for循环能够遍历用户输入的所有参数,然后在if语句中逐个比较,并查看是否与特定的标志匹配。所有的println语句都是为了测试目的。请问我应该如何正确编写这个for循环和if语句?

var Args []string

for i := range os.Args {
    fmt.Println("数组项", i, "是", os.Args[i])
    
    if os.Args[i] == "-target" {
        fmt.Println("GREAT SUCCESS")
        targetHostNew := targetHost
    }
}

fmt.Println("-------------------------", targetHostNew)

我还应该澄清一下,程序需要能够读取标志参数输入,但不修改这些参数变量。

英文:

I am brand new to programming, golang being my first language, and I am having some issues figuring out how to access variables from the command line. I am trying to take flags input in the command line by a user, and use those variables in an if statement. I want the for loop to run through all arguments input by the user, then compare each one in an if statement and see if any match up with specific flags. All println statements are for testing purposes. How would I correctly write this for loop/if statement?

var Args []string

for i := range os.Args {
	fmt.Println("array item", i, "is", os.Args[i])
	
	if i := os.Args("-target") {
		fmt.Println("GREAT SUCCESS")
		targetHostNew := targetHost
	}
}

fmt.Println("-------------------------", targetHostNew)

I should have clarified as well. The program needs to be able to read flag arguments input but not alter those arguments variables

答案1

得分: 3

在调用flag.Parse()之后,您需要通过os.Args访问变量,或者使用flag包将它们分配给变量。

请参考以下示例:

package main

import (
	"fmt"
	"os"
	"flag"
)

func main() {
	// 声明变量
	var targetHost string
	
	// 告诉flag将命令行(字符串)参数放入变量中
	flag.StringVar(&targetHost, "target", "defaultValue", "")

	// 解析命令行变量
	flag.Parse()

	// 遍历所有参数的索引和值
	for i, arg := range os.Args {
		// 打印索引和值
		fmt.Println("item", i, "is", arg)
	}

	// 如果targetHost等于defaultValue,则表示它未在命令行上设置
	if targetHost == "defaultValue" {
		fmt.Println("target not set")
	} else {
		fmt.Println("target set to", targetHost)
	}
}
英文:

You need to access the variables through os.Args after calling flag.Parse() or assign them to variables using the flag package.

Check out this example on both:

package main

import (
	"fmt"
	"os"
	"flag"
)

func main() {
	// declare variable
	var targetHost string
	
	// tell flags to put cmd line (string) arg into variable
	flag.StringVar(&targetHost, "target", "defaultValue", "")
	
	// do the actual parsing of command line variables
	flag.Parse()

	// loop over all arguments by index and value
	for i, arg := range os.Args {
		// print index and value
		fmt.Println("item", i, "is", arg)
	}

	// is targetHost defaultValue - then it wasn't set on the command line
	if targetHost == "defaultValue" {
		fmt.Println("target not set")
	} else {
		fmt.Println("target set to", targetHost);
	}
}

答案2

得分: 2

你可以使用flaghttp://golang.org/pkg/flag/

package main

import (
    "flag"
    "fmt"
)

var ip = flag.String("target", "localhost:3000", "目标的帮助信息")

func main() {
    flag.Parse()
    fmt.Println(*ip)
}
英文:

You can use the flag package http://golang.org/pkg/flag/

package main

import (
	"flag"
	"fmt"
)

var ip = flag.String("target", "localhost:3000", "help message for target")

func main() {
	flag.Parse()
	fmt.Println(*ip)
}

huangapple
  • 本文由 发表于 2014年6月14日 02:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/24211482.html
匿名

发表评论

匿名网友

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

确定