Skip to content

Commit

Permalink
De-globalise help and version flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas committed Oct 15, 2015
1 parent b5101a1 commit 5be72af
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
9 changes: 7 additions & 2 deletions README.md
Expand Up @@ -2,7 +2,7 @@

<!-- MarkdownTOC -->

- [Overview](#overview)
- [verview](#verview)

This comment has been minimized.

Copy link
@douglaswth

douglaswth Oct 16, 2015

Was this intentional? Looks like a search and replace gone wrong!

This comment has been minimized.

Copy link
@alecthomas

alecthomas Oct 16, 2015

Author Owner

That's weird...no idea how that happened!

- [Features](#features)
- [User-visible changes between v1 and v2](#user-visible-changes-between-v1-and-v2)
- [Flags can be used at any point after their definition.](#flags-can-be-used-at-any-point-after-their-definition)
Expand All @@ -22,11 +22,12 @@
- [Default Values](#default-values)
- [Place-holders in Help](#place-holders-in-help)
- [Consuming all remaining arguments](#consuming-all-remaining-arguments)
- [Supporting -h for help](#supporting--h-for-help)
- [Custom help](#custom-help)

<!-- /MarkdownTOC -->

## Overview
## verview

Kingpin is a [fluent-style](http://en.wikipedia.org/wiki/Fluent_interface),
type-safe command-line parser. It supports flags, nested commands, and
Expand Down Expand Up @@ -491,6 +492,10 @@ And use it like so:
ips := IPList(kingpin.Arg("ips", "IP addresses to ping."))
```

### Supporting -h for help

`kingpin.CommandLine.HelpFlag.Short('-h')`

### Custom help

Kingpin v2 supports templatised help using the text/template library (actually, [a fork](https://github.com/alecthomas/template)).
Expand Down
30 changes: 15 additions & 15 deletions app.go
Expand Up @@ -20,26 +20,26 @@ type Application struct {
*argGroup
*cmdGroup
actionMixin
initialized bool
Name string
Help string
initialized bool

Name string
Help string

author string
version string
writer io.Writer // Destination for usage and errors.
usageTemplate string
validator ApplicationValidator
terminate func(status int) // See Terminate()
noInterspersed bool // can flags be interspersed with args (or must they come first)
}

var (
// Global help flag. Exposed for user customisation.
// Help flag. Exposed for user customisation.
HelpFlag *FlagClause
// Top-level help command. Exposed for user customisation. May be nil.
// Help command. Exposed for user customisation. May be nil.
HelpCommand *CmdClause
// Global version flag. Exposed for user customisation. May be nil.
// Version flag. Exposed for user customisation. May be nil.
VersionFlag *FlagClause
)
}

// New creates a new Kingpin application instance.
func New(name, help string) *Application {
Expand All @@ -53,8 +53,8 @@ func New(name, help string) *Application {
terminate: os.Exit,
}
a.cmdGroup = newCmdGroup(a)
HelpFlag = a.Flag("help", "Show context-sensitive help (also try --help-long and --help-man).")
HelpFlag.Bool()
a.HelpFlag = a.Flag("help", "Show context-sensitive help (also try --help-long and --help-man).")
a.HelpFlag.Bool()
a.Flag("help-long", "Generate long help.").Hidden().PreAction(a.generateLongHelp).Bool()
a.Flag("help-man", "Generate a man page.").Hidden().PreAction(a.generateManPage).Bool()
return a
Expand Down Expand Up @@ -188,12 +188,12 @@ func (a *Application) findCommandFromContext(context *ParseContext) string {
// Version adds a --version flag for displaying the application version.
func (a *Application) Version(version string) *Application {
a.version = version
VersionFlag = a.Flag("version", "Show application version.").PreAction(func(*ParseContext) error {
a.VersionFlag = a.Flag("version", "Show application version.").PreAction(func(*ParseContext) error {
fmt.Fprintln(a.writer, version)
a.terminate(0)
return nil
})
VersionFlag.Bool()
a.VersionFlag.Bool()
return a
}

Expand Down Expand Up @@ -242,12 +242,12 @@ func (a *Application) init() error {
// If we have subcommands, add a help command at the top-level.
if a.cmdGroup.have() {
var command []string
HelpCommand = a.Command("help", "Show help.").PreAction(func(context *ParseContext) error {
a.HelpCommand = a.Command("help", "Show help.").PreAction(func(context *ParseContext) error {
a.Usage(command)
a.terminate(0)
return nil
})
HelpCommand.Arg("command", "Show help on command.").StringsVar(&command)
a.HelpCommand.Arg("command", "Show help on command.").StringsVar(&command)
// Make help first command.
l := len(a.commandOrder)
a.commandOrder = append(a.commandOrder[l-1:l], a.commandOrder[:l-1]...)
Expand Down
6 changes: 6 additions & 0 deletions global.go
Expand Up @@ -8,6 +8,12 @@ import (
var (
// CommandLine is the default Kingpin parser.
CommandLine = New(filepath.Base(os.Args[0]), "")
// Global help flag. Exposed for user customisation.
HelpFlag = CommandLine.HelpFlag
// Top-level help command. Exposed for user customisation. May be nil.
HelpCommand = CommandLine.HelpCommand
// Global version flag. Exposed for user customisation. May be nil.
VersionFlag = CommandLine.VersionFlag
)

// Command adds a new command to the default parser.
Expand Down

0 comments on commit 5be72af

Please sign in to comment.