使用flag 解析命令行参数用法
-
// flag_mytest project main.go
-
package main
-
-
import (
-
// "errors"
-
"flag"
-
"fmt"
-
"os"
-
// "strings"
-
// "time"
-
)
-
-
func main() {
-
fmt.Println(os.Args)
-
-
// Example 1: A single string flag called "species" with default value "gopher".
-
var species = flag.String("species", "gopher", "the species we are studying")
-
-
ok := flag.Bool("ok", false, "is ok")
-
id := flag.Int("id", 0, "id")
-
port := flag.String("port", ":8080", "http listen port")
-
var name string
-
flag.StringVar(&name, "name", "123", "name")
-
-
flag.Parse()
-
-
fmt.Println("species:", *species)
-
fmt.Println("ok:", *ok)
-
fmt.Println("id:", *id)
-
fmt.Println("port:", *port)
-
fmt.Println("name:", name)
-
}
-----------------------------------------------------------------
-
// flag_mytest project main.go
-
package main
-
-
import (
-
// "errors"
-
"flag"
-
"fmt"
-
"os"
-
// "strings"
-
// "time"
-
)
-
-
var gopherType string
-
-
// Example 2: Two flags sharing a variable, so we can have a shorthand.
-
// The order of initialization is undefined, so make sure both use the
-
// same default value. They must be set up with an init function.
-
-
func init() {
-
const (
-
defaultGopher = "pocket"
-
usage = "the variety of gopher"
-
)
-
flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
-
flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
-
}
-
-
func main() {
-
fmt.Println(os.Args)
-
-
flag.Parse()
-
fmt.Println("gopherType:", gopherType)
-
}
---------------------------------------------------------------------------
-
package main // Example 3: A user-defined flag type, a slice of durations.
-
-
import (
-
"errors"
-
"flag"
-
"fmt"
-
"os"
-
"strings"
-
// "time"
-
)
-
-
//type interval []time.Duration
-
type interval []string
-
-
// String is the method to format the flag's value, part of the flag.Value interface.
-
// The String method's output will be used in diagnostics.
-
func (i *interval) String() string {
-
return fmt.Sprint(*i)
-
}
-
-
// Set is the method to set the flag value, part of the flag.Value interface.
-
// Set's argument is a string to be parsed to set the flag.
-
// It's a comma-separated list, so we split it.
-
func (i *interval) Set(value string) error {
-
// If we wanted to allow the flag to be set multiple times,
-
// accumulating values, we would delete this if statement.
-
// That would permit usages such as
-
// -deltaT 10s -deltaT 15s
-
// and other combinations.
-
if len(*i) > 0 {
-
return errors.New("interval flag already set")
-
}
-
fmt.Println("in Set")
-
for _, dt := range strings.Split(value, ",") {
-
// duration, err := time.ParseDuration(dt)
-
-
/*
-
duration, err := dt
-
if err != nil {
-
return err
-
}
-
*/
-
duration := dt
-
*i = append(*i, duration)
-
}
-
return nil
-
}
-
-
// Define a flag to accumulate durations. Because it has a special type,
-
// we need to use the Var function and therefore create the flag during
-
// init.
-
-
var intervalFlag interval
-
-
func init() {
-
// Tie the command-line flag to the intervalFlag variable and
-
// set a usage message.
-
flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
-
}
-
-
func main() {
-
fmt.Println(os.Args)
-
flag.Parse()
-
fmt.Println("intervalFlag:", intervalFlag)
-
}
阅读(531) | 评论(0) | 转发(0) |