golang flag 解析入參

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

首先一個例子:

package mainimport (    "fmt"    "flag")func main(){    data_path := flag.String("D","/home/manu/sample/","DB data path")    log_file := flag.String("l","/home/manu/sample.log","log file")    nowait_flag :=flag.Bool("W",false,"do not wait until operation completes")    flag.Parse()    var cmd string = flag.Arg(0);    fmt.Printf("action   : %s\n",cmd)    fmt.Printf("data path: %s\n",*data_path)    fmt.Printf("log file : %s\n",*log_file)    fmt.Printf("nowait     : %v\n",*nowait_flag)    fmt.Printf("-------------------------------------------------------\n")    fmt.Printf("there are %d non-flag input param\n",flag.NArg())    for i,param := range flag.Args(){        fmt.Printf("#%d    :%s\n",i,param)    }}

 OK,我們分析下代碼(分割線下面的我們暫時不看):

    第一行對應的是data_path的解析規則

    -D選項對應的值是字串類型字串,

    預設值是“/home/manu/sample”,

    DB data path提示資訊或者help資訊或者說明是。

manu@manu-hacks:~/code/go/self$ go run pg_ctl_parse.go  -D /home/manu/DB_data/ -l /home/manu/DB_data/postgres_manu.log -W startaction   : startdata path: /home/manu/DB_data/log file : /home/manu/DB_data/postgres_manu.lognowait: true-------------------------------------------------------there are 1 non-flag input param#0:startmanu@manu-hacks:~/code/go/self$ go run pg_ctl_parse.go   -l=/home/manu/DB_data/postgres_manu.log -W -D /home/manu/DB_data/  startaction   : startdata path: /home/manu/DB_data/log file : /home/manu/DB_data/postgres_manu.lognowait: true-------------------------------------------------------there are 1 non-flag input param#0:start

    我們看到了,解析出了data_path,log_file無論 -l -D出現的順序如何,只要正常的出現了,就能正常的解析。

    但是晴朗的天空中也有一片烏雲,start不是這種 -key=alue 或則-option的類型,flag是解析不了的。我們稱這種參數為non-flag參數,flag解析遇到non-flag參數就停止了:

s := f.args[0]if len(s) == 0 || s[0] != '-' || len(s) == 1 {    return false, nil}

  所以如果我們將non-flag參數放在最前面,flag什麼也不會解析,因為flag遇到了這個就停止解析了。

manu@manu-hacks:~/code/go/self$ go run pg_ctl_parse.go  start -l=/home/manu/DB_data/postgres_manu.log -W -D /home/manu/DB_data/  action   : startdata path: /home/manu/samplelog file : /home/manu/sample.lognowait   : false-------------------------------------------------------there are 5 non-flag input param#0:start#1:-l=/home/manu/DB_data/postgres_manu.log#2:-W#3:-D#4:/home/manu/DB_data/

  OK,flag提供了Arg(i),Args()來擷取non-flag參數,NArg()來擷取non-flag的個數。正如我們們sample 代碼看到的。

fmt.Printf("there are %d non-flag input param\n",flag.NArg())    for i,param := range flag.Args(){        fmt.Printf("#%d :%s\n",i,param)    }

    flag還提供了NFlag()擷取那些匹配上的參數的個數。

    從例子上看,flag package很有用,但是並沒有強大到解析一切的程度。

    如果你有類似-option或者-key =value這種參數,不妨試試 flag。如果你的入參解析非常複雜,flag可能捉襟見肘。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.