Created
December 7, 2017 00:29
-
-
Save wolfeidau/dab463a94ed7b0ab2d6b390bae36bd05 to your computer and use it in GitHub Desktop.
Example of a more complex kingpin setup.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/alecthomas/kingpin" | |
) | |
var ( | |
app = kingpin.New("chat", "A command-line chat application.") | |
verbose = app.Flag("verbose", "Verbose mode.").Short('v').Bool() | |
username = app.Flag("username", "Username used to authenticate to service.").Required().String() | |
password = app.Flag("password", "Password used to authenticate to service.").Required().String() | |
register = app.Command("register", "Register a new user.") | |
post = app.Command("post", "Post a message to a channel.") | |
) | |
type RegisterFlags struct { | |
RegisterNick string | |
} | |
type PostFlags struct { | |
PostNick string | |
} | |
func registerSetup() *RegisterFlags { | |
rf := &RegisterFlags{} | |
register.Flag("nick", "The nick name you want to register.").Short('n').StringVar(&rf.RegisterNick) | |
return rf | |
} | |
func postSetup() *PostFlags { | |
rf := &PostFlags{} | |
post.Flag("nick", "The nick name you want to register.").Short('n').StringVar(&rf.PostNick) | |
return rf | |
} | |
func main() { | |
rf := registerSetup() | |
pf := postSetup() | |
switch kingpin.MustParse(app.Parse(os.Args[1:])) { | |
// Register user | |
case register.FullCommand(): | |
fmt.Println("Register", rf) | |
// Post message | |
case post.FullCommand(): | |
fmt.Println("Post", pf) | |
} | |
} |
Author
wolfeidau
commented
Dec 7, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment