### Install Kingpin Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Install Kingpin using the go get command. ```bash $ go get gopkg.in/alecthomas/kingpin.v2 ``` -------------------------------- ### Simple Kingpin Example Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md A basic example demonstrating how to define and parse a boolean flag and a required string argument. ```go var ( verbose = kingpin.Flag("verbose", "Verbose mode.").Short('v').Bool() name = kingpin.Arg("name", "Name of user.").Required().String() ) func main() { kingpin.Parse() fmt.Printf("%v, %s\n", *verbose, *name) } ``` -------------------------------- ### Install Kingpin v1 Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Use 'go get' to install the older stable version of Kingpin. ```sh $ go get gopkg.in/alecthomas/kingpin.v1 ``` -------------------------------- ### Example CLI Help Output Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Demonstrates the default help message format generated by Kingpin for a sample CLI application, showing usage, flags, and commands. ```bash $ go run ./examples/curl/curl.go --help usage: curl [] [ ...] An example implementation of curl. Flags: --help Show help. -t, --timeout=5s Set connection timeout. -H, --headers=HEADER=VALUE Add HTTP headers to the request. Commands: help [...] Show help. get url Retrieve a URL. get file Retrieve a file. post [] POST a resource. ``` -------------------------------- ### Install CLI Tool Completion via Bash Profile Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Instructs users to add a statement to their bash_profile to enable shell completion for the CLI tool. ```bash eval "$(your-cli-tool --completion-script-bash)" ``` ```bash eval "$(your-cli-tool --completion-script-zsh)" ``` -------------------------------- ### Compile BOS CMD Source: https://github.com/baidubce/bce-cmd/blob/master/README.md Compiles the BOS CMD tool. Ensure Go (version 1.11+) is installed and GOROOT is configured. ```shell sh build.sh ``` -------------------------------- ### Simple Flag and Argument Parsing Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Demonstrates basic command-line application structure with flags and arguments using Kingpin. Useful for simple utilities. ```go package main import ( "fmt" "gopkg.in/alecthomas/kingpin.v2" ) var ( debug = kingpin.Flag("debug", "Enable debug mode.").Bool() timeout = kingpin.Flag("timeout", "Timeout waiting for ping.").Default("5s").OverrideDefaultFromEnvar("PING_TIMEOUT").Short('t').Duration() ip = kingpin.Arg("ip", "IP address to ping.").Required().IP() count = kingpin.Arg("count", "Number of packets to send").Int() ) func main() { kingpin.Version("0.0.1") kingpin.Parse() fmt.Printf("Would ping: %s with timeout %s and count %d\n", *ip, *timeout, *count) } ``` -------------------------------- ### Complex Command-Line Application with Sub-commands Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Illustrates building a complex CLI application with global flags, sub-commands, and per-subcommand flags. Suitable for multi-feature CLIs. ```go package main import ( "os" "strings" "gopkg.in/alecthomas/kingpin.v2" ) var ( app = kingpin.New("chat", "A command-line chat application.") debug = app.Flag("debug", "Enable debug mode.").Bool() serverIP = app.Flag("server", "Server address.").Default("127.0.0.1").IP() register = app.Command("register", "Register a new user.") registerNick = register.Arg("nick", "Nickname for user.").Required().String() registerName = register.Arg("name", "Name of user.").Required().String() post = app.Command("post", "Post a message to a channel.") postImage = post.Flag("image", "Image to post.").File() postChannel = post.Arg("channel", "Channel to post to.").Required().String() postText = post.Arg("text", "Text to post.").Strings() ) func main() { switch kingpin.MustParse(app.Parse(os.Args[1:])) { // Register user case register.FullCommand(): println(*registerNick) // Post message case post.FullCommand(): if *postImage != nil { } text := strings.Join(*postText, " ") println("Post:", text) } } ``` -------------------------------- ### Using Custom IP Address List Argument Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Demonstrates how to use the custom IPList function to define an argument that accepts a list of IP addresses. ```go ips := IPList(kingpin.Arg("ips", "IP addresses to ping.")) ``` -------------------------------- ### Provide Static Options for Flag Completion Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Uses HintOptions to provide a predefined list of static options for a flag, allowing users to choose from these or provide their own. ```go app := kingpin.New("completion", "My application with bash completion.") app.Flag("port", "Provide a port to connect to"). Required(). HintOptions("80", "443", "8080"). IntVar(&c.port) ``` -------------------------------- ### Using Custom HTTP Header Flag Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Demonstrates how to use the custom HTTPHeader function to define a flag for adding HTTP headers. ```go headers = HTTPHeader(kingpin.Flag("header", "Add a HTTP header to the request.").Short('H')) ``` -------------------------------- ### Sub-command Definition and Parsing Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Shows how to define nested sub-commands with their own flags and arguments, and how to parse them in the main function. Essential for structured CLIs. ```go var ( deleteCommand = kingpin.Command("delete", "Delete an object.") deleteUserCommand = deleteCommand.Command("user", "Delete a user.") deleteUserUIDFlag = deleteUserCommand.Flag("uid", "Delete user by UID rather than username.") deleteUserUsername = deleteUserCommand.Arg("username", "Username to delete.") deletePostCommand = deleteCommand.Command("post", "Delete a post.") ) func main() { switch kingpin.Parse() { case "delete user": case "delete post": } } ``` -------------------------------- ### Set Short Help Flag Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Configures the command-line application to use '-h' as a short flag for displaying help information. ```go kingpin.CommandLine.HelpFlag.Short('h') ``` -------------------------------- ### Provide Dynamic Options for Flag Completion Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Uses HintAction with a function to dynamically generate completion options for a flag, useful for reading from files or databases. ```go func listHosts() []string { // Provide a dynamic list of hosts from a hosts file or otherwise // for bash completion. In this example we simply return static slice. // You could use this functionality to reach into a hosts file to provide // completion for a list of known hosts. return []string{"sshhost.example", "webhost.example", "ftphost.example"} } app := kingpin.New("completion", "My application with bash completion.") app.Flag("flag-1", "").HintAction(listHosts).String() ``` -------------------------------- ### Run Tests and Generate Coverage Report Source: https://github.com/baidubce/bce-cmd/blob/master/README.md Executes tests for specified files or directories and generates an HTML coverage report. Replace $1 with the target file or directory. ```shell go test -v $1 -coverprofile=c.out go tool cover -html=c.out -o cover.html ``` -------------------------------- ### Convenience Function for HTTP Header Parser Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Provides a convenience function to create and set an HTTP header parser, simplifying its usage with Kingpin flags. ```go func HTTPHeader(s Settings) (target *http.Header) { target = &http.Header{} s.SetValue((*HTTPHeaderValue)(target)) return } ``` -------------------------------- ### Convenience Function for IP Address List Parser Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Provides a convenience function to create and set an IP address list parser, simplifying its usage with Kingpin arguments. ```go func IPList(s Settings) (target *[]net.IP) { target = new([]net.IP) s.SetValue((*ipList)(target)) return } ``` -------------------------------- ### Custom IP Address List Parser Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Implements a custom parser for accumulating a list of IP addresses, conforming to Go's flag.Value interface and supporting cumulative values. ```go type ipList []net.IP func (i *ipList) Set(value string) error { if ip := net.ParseIP(value); ip == nil { return fmt.Errorf("'%s' is not an IP address", value) } else { *i = append(*i, ip) return nil } } func (i *ipList) String() string { return "" } func (i *ipList) IsCumulative() bool { return true } ``` -------------------------------- ### Custom HTTP Header Parser Source: https://github.com/baidubce/bce-cmd/blob/master/src/vendor/github.com/alecthomas/kingpin/README.md Defines a custom parser for accumulating HTTP header values, conforming to Go's flag.Value interface. It splits the input string into a header name and value. ```go type HTTPHeaderValue http.Header func (h *HTTPHeaderValue) Set(value string) error { parts := strings.SplitN(value, ":", 2) if len(parts) != 2 { return fmt.Errorf("expected HEADER:VALUE got '%s'", value) } (*http.Header)(h).Add(parts[0], parts[1]) return nil } func (h *HTTPHeaderValue) String() string { return "" } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.