summaryrefslogtreecommitdiff
path: root/parse.go
diff options
context:
space:
mode:
authorEmmanouil "Manolis" Maragkakis <[email protected]>2017-01-23 20:41:12 -0500
committerEmmanouil "Manolis" Maragkakis <[email protected]>2017-01-23 20:41:12 -0500
commitdb274311536204025bd248faaa74347707be9d76 (patch)
tree9fb9264188c1c93a7340885797956e86dad97075 /parse.go
parent7c77c70f8528a7b3310820aeec46d56a5be1ba70 (diff)
add support for description string
Diffstat (limited to 'parse.go')
-rw-r--r--parse.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/parse.go b/parse.go
index 26b530a..3cd00aa 100644
--- a/parse.go
+++ b/parse.go
@@ -67,9 +67,10 @@ type Config struct {
// Parser represents a set of command line options with destination values
type Parser struct {
- spec []*spec
- config Config
- version string
+ spec []*spec
+ config Config
+ version string
+ description string
}
// Versioned is the interface that the destination struct should implement to
@@ -80,6 +81,14 @@ type Versioned interface {
Version() string
}
+// Described is the interface that the destination struct should implement to
+// make a description string appear at the top of the help message.
+type Described interface {
+ // Description returns the string that will be printed on a line by itself
+ // at the top of the help message.
+ Description() string
+}
+
// walkFields calls a function for each field of a struct, recursively expanding struct fields.
func walkFields(v reflect.Value, visit func(field reflect.StructField, val reflect.Value, owner reflect.Type) bool) {
t := v.Type()
@@ -102,6 +111,9 @@ func NewParser(config Config, dests ...interface{}) (*Parser, error) {
if dest, ok := dest.(Versioned); ok {
p.version = dest.Version()
}
+ if dest, ok := dest.(Described); ok {
+ p.description = dest.Description()
+ }
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr {
panic(fmt.Sprintf("%s is not a pointer (did you forget an ampersand?)", v.Type()))