What does this mean? I do not have time to fix issues myself. The only way fixes or new features will be added is by people submitting PRs. If you are interested in taking over maintenance and have a history of contributions to Kingpin, please let me know.
Current status. Kingpin is largely feature stable. There hasn't been a need to add new features in a while, but there are some bugs that should be fixed.
Why? I no longer use Kingpin personally (I now use kong). Rather than leave the project in a limbo of people filing issues and wondering why they're not being worked on, I believe this notice will more clearly set expectations.
Kingpin is a fluent-style, type-safe command-line parser. It supports flags, nested commands, and positional arguments.
Install it with:
$ go get github.com/alecthomas/kingpin/v2
It looks like this:
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)
}
More examples are available.
Second to parsing, providing the user with useful help is probably the most
important thing a command-line parser does. Kingpin tries to provide detailed
contextual help if --help is encountered at any point in the command line
(excluding after --).
kingpin.Flag("f", "help").Int())kingpin.Arg("a", "help").Int()).kingpin.Command("c", "help")).kingpin.Flag("f", "").Required().Int()).command.Default()).kingpin.Command("c", "").Action(myAction)).-a -b -> -ab).-a parm -> -aparm).@<file>).--help-man).Flags can be specified at any point after their definition, not just immediately after their associated command. From the chat example below, the following used to be required:
$ chat --server=chat.server.com:8080 post --image=~/Downloads/owls.jpg pics
But the following will now work:
$ chat post --server=chat.server.com:8080 --image=~/Downloads/owls.jpg pics
Previously, if a short flag was used, any argument to that flag would have to be separated by a space. That is no longer the case.
ParseWithFileExpansion() is gone. The new parser directly supports expanding @<file>.FatalUsage() and FatalUsageContext() for displaying an error + usage and terminating.Dispatch() renamed to Action().ParseContext() for parsing a command line into its intermediate context form without executing.Terminate() function to override the termination function.UsageForContextWithTemplate() for printing usage via a custom template.UsageTemplate() for overriding the default template to use. Two templates are included:DefaultUsageTemplate - default template.CompactUsageTemplate - compact command template for larger applications.The current stable version is github.com/alecthomas/kingpin/v2. The previous version, gopkg.in/alecthomas/kingpin.v1, is deprecated and in maintenance mode.
Installation:
$ go get github.com/alecthomas/kingpin/v2
Installation:
$ go get gopkg.in/alecthomas/kingpin.v1
2015-09-19 -- Stable v2.1.0 release.
command.Default() to specify a default command to use if no other
command matches. This allows for convenient user shortcuts.HelpFlag and VersionFlag for further customisation.Action() and PreAction() added and both now support an arbitrary
number of callbacks.kingpin.SeparateOptionalFlagsUsageTemplate.--help-long and --help-man (hidden by default) flags.app.Interspersed(false).app.Writer(os.Writer) to specify the default writer for all output functions.os.Writer prefix from all printf-like functions.2015-05-22 -- Stable v2.0.0 release.
go generate to generate repeated flags.2015-01-23 -- Stable v1.3.4 release.
2014-07-08 -- Stable v1.2.0 release.
Strings() when final argument.
Allows for values that look like flags to be processed.--help to be used with commands.Hidden() flags.--ram=512MB or --ram=1GB.Enum() value, allowing only one of a set of values
to be selected. eg. Flag(...).Enum("debug", "info", "warning").2014-06-27 -- Stable v1.1.0 release.
OpenFile(flag, perm) value type added, for finer control over opening files.2014-06-19 -- Stable v1.0.0 release.
2014-06-10 -- Place-holder streamlining.
MetaVar to PlaceHolder.MetaVarFromDefault. Kingpin now uses heuristics
to determine what to display.Kingpin can be used for simple flag+arg applications like so:
$ ping --help
usage: ping [<flags>] <ip> [<count>]
Flags:
--debug Enable debug mode.
--help Show help.
-t, --timeout=5s Timeout waiting for ping.
Args:
<ip> IP address to ping.
[<count>] Number of packets to send
$ ping 1.2.3.4 5
Would ping: 1.2.3.4 with timeout 5s and count 5
From the following source:
package main
import (
"fmt"
"github.com/alecthomas/kingpin/v2"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode.").Bool()
timeout = kingpin.Flag("timeout", "Timeout waiting for ping.").Default("5s").Envar("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)
}
Kingpin supports reading arguments from a file. Create a file with the corresponding arguments:
echo -t=5\n > args
And now supply it:
$ ping @args
Kingpin can also produce complex command-line applications with global flags, subcommands, and per-subcommand flags, like this:
$ chat --help
usage: chat [<flags>] <command> [<flags>] [<args> ...]
A command-line chat application.
Flags:
--help Show help.
--debug Enable debug mode.
--server=127.0.0.1 Server address.
Commands:
help [<command>]
Show help for a command.
register <nick> <name>
Register a new user.
post [<flags>] <channel> [<text>]
Post a message to a channel.
$ chat help post
usage: chat [<flags>] post [<flags>] <channel> [<text>]
Post a message to a channel.
Flags:
--image=IMAGE Image to post.
Args:
<channel> Channel to post to.
[<text>] Text to post.
$ chat post --image=~/Downloads/owls.jpg pics
...
From this code:
package main
import (
"os"
"strings"
"github.com/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)
}
}
Kingpin exports a set of functions to provide consistent errors and usage information to the user.
Error messages look something like this:
<app>: error: <message>
The functions on Application are:
| Function | Purpose |
|---|---|
Errorf(format, args) |
Display a printf formatted error to the user. |
Fatalf(format, args) |
As with Errorf, but also call the termination handler. |
FatalUsage(format, args) |
As with Fatalf, but also print contextual usage information. |
FatalUsageContext(context, format, args) |
As with Fatalf, but also print contextual usage information from a ParseContext. |
FatalIfError(err, format, args) |
Conditionally print an error prefixed with format+args, then call the termination handler |
There are equivalent global functions in the kingpin namespace for the default
kingpin.CommandLine instance.
Kingpin supports nested sub-commands, with separate flag and positional arguments per sub-command. Note that positional arguments may only occur after sub-commands.
For example:
```go var ( deleteCommand = kingpin.Command("delete", "Delete an object.") deleteUserCommand = deleteCommand.Command("user", "Delete a user.") deleteUserUIDFlag =
$ claude mcp add kingpin \
-- python -m otcore.mcp_server <graph>