Golang Command line Usage--flag package

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

2013-12-08 WCDJ


The Go language handles command-line arguments by using the flag package in the standard library.

Package flag implements command-line flag parsing.

http://golang.org/pkg/flag/

http://golang.org/pkg/


A few things to note:

1, through flag. String (), Bool (), Int () to define the flag to be used in the command line.

2, after the flag is defined, by calling flag. Parse () to parse the command-line arguments.

3, the format of the command-line arguments can be:

-flag xxx (using spaces, one-symbol)

--flag xxx (using spaces, two-symbols)

-FLAG=XXX (use equal sign, one-symbol)

--FLAG=XXX (use equal sign, two-symbol)

Where the Boolean parameter prevents the two semantics of parsing, it should be specified using the equals sign.


Test Case:

Package Mainimport (     "flag"      "FMT") var input_pstrname = flag. String ("Name", "Gerry", "Input ur name") var input_piage = flag. Int ("Age", and "input ur Age") var input_flagvar intfunc Init () {    flag. Intvar (&input_flagvar, "FlagName", 1234, "help message for FlagName")}func main () {    init ()  & nbsp  flag. Parse ()     //after parsing, the arguments after the flag is available as the slice flag. Args () or individually as flag. ARG (i). The arguments is indexed from 0 through flag. Narg () -1    //Args returns the Non-flag command-line arguments    //NArg is the number of Arguments remaining after flags has been processed    fmt. Printf ("args=%s, num=%d\n", flag.) Args (), flag. Narg ())     for I: = 0; I! = flag. Narg (); i++ {        fmt. Printf ("arg[%d]=%s\n", I, flag. ARG (i))     }    fmt. PRintln ("Name=", *input_pstrname)     fmt. Println ("age=", *input_piage)     fmt. Println ("Flagname=", Input_flagvar)}/*output:mba:filter gerryyang$./test_flag--name "AAA"-age=123-flagname=0x11 Para1 para2 para3args=[para1 para2 para3], num=3arg[0]=para1arg[1]=para2arg[2]=para3name= aaaage= 123flagname= 17*/

Reference:

Standard library-command line parameter parsing flag




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.