Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334138
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-26 11:04:32

使用flag 解析命令行参数用法

点击(此处)折叠或打开

  1. // flag_mytest project main.go
  2. package main

  3. import (
  4.     //    "errors"
  5.     "flag"
  6.     "fmt"
  7.     "os"
  8.     //    "strings"
  9.     //    "time"
  10. )

  11. func main() {
  12.     fmt.Println(os.Args)

  13.     // Example 1: A single string flag called "species" with default value "gopher".
  14.     var species = flag.String("species", "gopher", "the species we are studying")

  15.     ok := flag.Bool("ok", false, "is ok")
  16.     id := flag.Int("id", 0, "id")
  17.     port := flag.String("port", ":8080", "http listen port")
  18.     var name string
  19.     flag.StringVar(&name, "name", "123", "name")

  20.     flag.Parse()

  21.     fmt.Println("species:", *species)
  22.     fmt.Println("ok:", *ok)
  23.     fmt.Println("id:", *id)
  24.     fmt.Println("port:", *port)
  25.     fmt.Println("name:", name)
  26. }


-----------------------------------------------------------------

点击(此处)折叠或打开

  1. // flag_mytest project main.go
  2. package main

  3. import (
  4.     //    "errors"
  5.     "flag"
  6.     "fmt"
  7.     "os"
  8.     //    "strings"
  9.     //    "time"
  10. )

  11. var gopherType string

  12. // Example 2: Two flags sharing a variable, so we can have a shorthand.
  13. // The order of initialization is undefined, so make sure both use the
  14. // same default value. They must be set up with an init function.

  15. func init() {
  16.     const (
  17.         defaultGopher = "pocket"
  18.         usage = "the variety of gopher"
  19.     )
  20.     flag.StringVar(&gopherType, "gopher_type", defaultGopher, usage)
  21.     flag.StringVar(&gopherType, "g", defaultGopher, usage+" (shorthand)")
  22. }

  23. func main() {
  24.     fmt.Println(os.Args)
  25.     flag.Parse()
  26.     fmt.Println("gopherType:", gopherType)
  27. }


---------------------------------------------------------------------------

点击(此处)折叠或打开

  1. package main // Example 3: A user-defined flag type, a slice of durations.

  2. import (
  3.     "errors"
  4.     "flag"
  5.     "fmt"
  6.     "os"
  7.     "strings"
  8.     //    "time"
  9. )

  10. //type interval []time.Duration
  11. type interval []string

  12. // String is the method to format the flag's value, part of the flag.Value interface.
  13. // The String method's output will be used in diagnostics.
  14. func (i *interval) String() string {
  15.     return fmt.Sprint(*i)
  16. }

  17. // Set is the method to set the flag value, part of the flag.Value interface.
  18. // Set's argument is a string to be parsed to set the flag.
  19. // It's a comma-separated list, so we split it.
  20. func (i *interval) Set(value string) error {
  21.     // If we wanted to allow the flag to be set multiple times,
  22.     // accumulating values, we would delete this if statement.
  23.     // That would permit usages such as
  24.     //    -deltaT 10s -deltaT 15s
  25.     // and other combinations.
  26.     if len(*i) > 0 {
  27.         return errors.New("interval flag already set")
  28.     }
  29.     fmt.Println("in Set")
  30.     for _, dt := range strings.Split(value, ",") {
  31.         //        duration, err := time.ParseDuration(dt)

  32.         /*
  33.             duration, err := dt
  34.             if err != nil {
  35.                 return err
  36.             }
  37.         */
  38.         duration := dt
  39.         *i = append(*i, duration)
  40.     }
  41.     return nil
  42. }

  43. // Define a flag to accumulate durations. Because it has a special type,
  44. // we need to use the Var function and therefore create the flag during
  45. // init.

  46. var intervalFlag interval

  47. func init() {
  48.     // Tie the command-line flag to the intervalFlag variable and
  49.     // set a usage message.
  50.     flag.Var(&intervalFlag, "deltaT", "comma-separated list of intervals to use between events")
  51. }

  52. func main() {
  53.     fmt.Println(os.Args)
  54.     flag.Parse()
  55.     fmt.Println("intervalFlag:", intervalFlag)
  56. }


阅读(531) | 评论(0) | 转发(0) |
0

上一篇:文件复制

下一篇:io操作

给主人留下些什么吧!~~