Golang flag Parse into parameter

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First an example:

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, let's Analyze the code (below the split line we don't see for the moment):

The first line corresponds to the parsing rules of the Data_path

The value corresponding to the-D option is a string of type

The default value is "/home/manu/sample",

The DB data path hint or help message or description is.

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 

We see, resolved the Data_path,log_file no matter how the order of-l-d appear, as long as the normal appearance, it can be normal analysis.

But there is also a cloud in the clear sky, start is not this type of-key=alue or-option, flag is unable to parse. We call this parameter the Non-flag parameter, and the flag parse stops when it encounters the Non-flag parameter:

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

So if we put the Non-flag parameter at the front, flag will not parse anything because flag will stop parsing when it encounters this.

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 is 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 provides ARG (i), Args () to get the Non-flag parameter, Narg () to get the number of Non-flag. As we see in the sample code.

Fmt. Printf ("There is%d Non-flag input param\n", flag. Narg ()) for I,param: = Range flag. Args () {fmt. Printf ("#%d:%s\n", I,param)}

Flag also provides Nflag () to get the number of parameters on those matches.

From the example, the flag package is useful, but not strong to the extent of parsing everything.

If you have a parameter like-option or-key =value, you might want to try flag. If your entry parsing is complex, flag may be stretched.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.