Implementation of command line parameters like Go get–u

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

We may need something like go get–u .... This way to implement our application, we cannot simply use flag. Parse, but to use Flagset, use the following example:

Package Main

Import (
"Flag"
"Log"
"OS"
)

var ()

Func Main () {

Read the command parameters, note that there are different parameters depending on the execution of the task.
Technical Reference:
Http://stackoverflow.com/questions/24504024/defining-independent-flagsets-in-golang
Http://play.golang.org/p/eaEEx_EReX
If Len (OS. Args) <= 1 {
Log. Println ("Please enter commands, supported commands: ESB, Web, host! ")
Os. Exit (-1)
}
Log. Println (OS. ARGS[1])
CMD: = os. ARGS[1]

ESB Client Tools
FESB: = flag. Newflagset ("Fesb", flag. ContinueOnError)
Consumer: = Fesb. Int ("C", 0, "The consumer's number is filtered in the ESB relationship, and the total is not lost.") ")
Provider: = Fesb. Int ("P", 0, "the number of the producer is filtered in the ESB relationship, and the output represents all.") ")
Dependtype: = Fesb. String ("T", "Cache", "type of ESB relationship dependency, default from cache on line, Optional: config (database configuration), cache (cached on-line call)")

Web site
Fweb: = flag. Newflagset ("Fweb", flag. ContinueOnError)
WebPort: = Fweb. Int ("P", "Bayi," the port number used by the WEB site. ")

Modify Host File Mode
Fhost: = flag. Newflagset ("Fhost", flag. ContinueOnError)
AAA: = Fhost. Bool ("B", False, "Test parameter!") ")

    switch cmd {
    case "ESB":
        ERR: = Fesb. Parse (OS. Args[2:])
        if err! = Nil {
             log. PRINTLN ("ESB parameter type error,", err)
            os. Exit ( -1)
       }
        log. Println ("Dependtype:", *dependtype)
        log. Println ("Consumer:", *consumer)
        log. Println ("Provider:", *provider)

Case "Web":
ERR: = Fweb. Parse (OS. Args[2:])
If err! = Nil {
Log. PRINTLN ("Web parameter type error,", err)
Os. Exit (-1)
}
Log. Println ("WebPort:", *webport)

Case "Host":
ERR: = Fhost. Parse (OS. Args[2:])
If err! = Nil {
Log. PRINTLN ("host parameter type error,", err)
Os. Exit (-1)
}
Log. Println ("AAA:", *AAA)

}

}

Resources:

Http://play.golang.org/p/eaEEx_EReX

Http://stackoverflow.com/questions/24504024/defining-independent-flagsets-in-golang

https://golang.org/pkg/flag/#NewFlagSet

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.