summaryrefslogtreecommitdiff
path: root/usage.go
diff options
context:
space:
mode:
authorAlex Flint <[email protected]>2015-11-01 00:13:23 -0700
committerAlex Flint <[email protected]>2015-11-01 00:13:23 -0700
commit30befae91a472e767b021961620ac0d93e6ff11d (patch)
tree9bf1176403b433bcca4442c52a7f4be6cb4cb671 /usage.go
parentf427e9f317714fc985c39ca24990852e73be5ef0 (diff)
move more stuff over to the parser struct
Diffstat (limited to 'usage.go')
-rw-r--r--usage.go30
1 files changed, 5 insertions, 25 deletions
diff --git a/usage.go b/usage.go
index fdc2248..689da73 100644
--- a/usage.go
+++ b/usage.go
@@ -9,37 +9,17 @@ import (
"strings"
)
-// Usage prints usage information to stdout and exits with status zero
-func Usage(dest ...interface{}) {
- if err := WriteUsage(os.Stdout, dest...); err != nil {
- fmt.Println(err)
- }
- os.Exit(0)
-}
-
// Fail prints usage information to stdout and exits with non-zero status
-func Fail(msg string, dest ...interface{}) {
+func (p *Parser) Fail(msg string) {
fmt.Println(msg)
- if err := WriteUsage(os.Stdout, dest...); err != nil {
- fmt.Println(err)
- }
+ p.WriteUsage(os.Stdout)
os.Exit(1)
}
// WriteUsage writes usage information to the given writer
-func WriteUsage(w io.Writer, dest ...interface{}) error {
- spec, err := extractSpec(dest...)
- if err != nil {
- return err
- }
- writeUsage(w, filepath.Base(os.Args[0]), spec)
- return nil
-}
-
-// writeUsage writes usage information to the given writer
-func writeUsage(w io.Writer, cmd string, specs []*spec) {
+func (p *Parser) WriteUsage(w io.Writer) {
var positionals, options []*spec
- for _, spec := range specs {
+ for _, spec := range p.spec {
if spec.positional {
positionals = append(positionals, spec)
} else {
@@ -47,7 +27,7 @@ func writeUsage(w io.Writer, cmd string, specs []*spec) {
}
}
- fmt.Fprint(w, "usage: %s ", cmd)
+ fmt.Fprintf(w, "usage: %s ", filepath.Base(os.Args[0]))
// write the option component of the one-line usage message
for _, spec := range options {