goopt: github.com/droundy/goopt Index | Files | Directories

package goopt

import "github.com/droundy/goopt"

Index

Package Files

goopt.go slice.go

Variables

var Args = make([]string, 0, 4)

This is the list of non-flag arguments after processing

var Author = ""

Redefine this to change the author of your program (used in the default man page)

var Description = func() string {
    return "" /* 134 byte string literal not displayed */

}

Set the description used in the man page for your program. If you want paragraphs, use two newlines in a row (e.g. LaTeX)

var ExtraUsage = ""

Redefine this to change the additional usage of your program (used in the default Usage() and man page)

var Help = func() string {
    h0 := new(bytes.Buffer)
    h := tabwriter.NewWriter(h0, 0, 8, 2, ' ', 0)
    if len(opts) > 1 {
        fmt.Fprintln(h, "Options:")
    }
    for _, o := range opts {
        fmt.Fprint(h, "  ")
        if len(o.shortnames) > 0 {
            for _, sn := range o.shortnames[0 : len(o.shortnames)-1] {
                fmt.Fprintf(h, "-%c, ", sn)
            }
            fmt.Fprintf(h, "-%c", o.shortnames[len(o.shortnames)-1])
            if o.allowsArg != nil && len(o.names) == 0 {
                fmt.Fprintf(h, " %s", *o.allowsArg)
            }
        }
        if len(o.names) > 0 {
            if len(o.shortnames) > 0 {
                fmt.Fprint(h, ", ")
            }
            for _, n := range o.names[0 : len(o.names)-1] {
                fmt.Fprintf(h, "%s, ", n)
            }
            fmt.Fprint(h, o.names[len(o.names)-1])
            if o.allowsArg != nil {
                fmt.Fprintf(h, "=%s", *o.allowsArg)
            }
        }
        fmt.Fprintf(h, "\t%v\n", Expand(o.help))
    }
    h.Flush()
    return h0.String()
}

Override the way help is displayed (not recommended)

var RequireOrder = false

Redefine this to force flags to come before all options or be treated as if they were options

var Suite = ""

Redefine this to change the suite of your program (e.g. the application name) (used in the default manpage())

var Summary = ""

Redefine this to change the summary of your program (used in the default Usage() and man page)

var Synopsis = func() string {
    h := new(bytes.Buffer)
    for _, o := range opts {
        fmt.Fprint(h, " [")
        switch {
        case len(o.shortnames) == 0:
            for _, n := range o.names[0 : len(o.names)-1] {
                fmt.Fprintf(h, "\\-\\-%s|", n[2:])
            }
            fmt.Fprintf(h, "\\-\\-%s", o.names[len(o.names)-1][2:])
            if o.allowsArg != nil {
                fmt.Fprintf(h, " %s", *o.allowsArg)
            }
        case len(o.names) == 0:
            for _, c := range o.shortnames[0 : len(o.shortnames)-1] {
                fmt.Fprintf(h, "\\-%c|", c)
            }
            fmt.Fprintf(h, "\\-%c", o.shortnames[len(o.shortnames)-1])
            if o.allowsArg != nil {
                fmt.Fprintf(h, " %s", *o.allowsArg)
            }
        default:
            for _, c := range o.shortnames {
                fmt.Fprintf(h, "\\-%c|", c)
            }
            for _, n := range o.names[0 : len(o.names)-1] {
                fmt.Fprintf(h, "\\-\\-%s|", n[2:])
            }
            fmt.Fprintf(h, "\\-\\-%s", o.names[len(o.names)-1][2:])
            if o.allowsArg != nil {
                fmt.Fprintf(h, " %s", *o.allowsArg)
            }
        }
        fmt.Fprint(h, "]")
    }
    return h.String()
}

Override the shortened help for your program (not recommended)

var Usage = func() string {
    programName := os.Args[0][strings.LastIndex(os.Args[0], "/")+1:]
    usage := fmt.Sprintf("Usage of %s:\n", programName)
    if Summary != "" {
        usage += fmt.Sprintf("\t%s", Summary)
    }
    usage += fmt.Sprintf("\n%s", Help())
    if ExtraUsage != "" {
        usage += fmt.Sprintf("%s\n", ExtraUsage)
    }
    return usage
}

Redefine this function to change the way usage is printed

var Vars = make(map[string]string)

Variables for expansion using Expand(), which is automatically called on help text for flags

var Version = ""

Redefine this to change the displayed version of your program (used in the default man page)

func Alternatives Uses

func Alternatives(names, vs []string, help string) *string

Create a required-argument flag that only accepts the given set of values Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
vs    []string            These are the allowable values for the argument
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*string                   This points to a string whose value is updated as this flag is changed

func AlternativesWithLabel Uses

func AlternativesWithLabel(names, vs []string, label string, help string) *string

Create a required-argument flag that only accepts the given set of valuesand has a Help() label Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
vs    []string            These are the allowable values for the argument
label   string            Label for display in Help()
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*string                   This points to a string whose value is updated as this flag is changed

func Expand Uses

func Expand(x string) string

Expand all variables in Vars within the given string. This does not assume any prefix or suffix that sets off a variable from the rest of the text, so a var of A set to HI expanded into HAPPY will become HHIPPY.

func Flag Uses

func Flag(yes []string, no []string, helpyes, helpno string) *bool

Create a no-argument flag that is set by either passing one of the "NO" flags or one of the "YES" flags. The default value is "false" (or "NO"). If you want another default value, you can swap the meaning of "NO" and "YES".

Parameters:

yes   []string            These flags set the boolean value to true (e.g. -i --install)
no    []string            These flags set the boolean value to false (e.g. -I --no-install)
helpyes string            The help text (automatically Expand()ed) to display for the "yes" flags
helpno  string            The help text (automatically Expand()ed) to display for the "no" flags

Returns:

*bool                     This points to a bool whose value is updated as this flag is changed

func Int Uses

func Int(names []string, def int, help string) *int

Create a required-argument flag that accepts int values Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     int               Default value for the flag and label in Help()
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*int                      This points to an int whose value is updated as this flag is changed

func IntWithLabel Uses

func IntWithLabel(names []string, def int, label string, help string) *int

Create a required-argument flag that accepts int values and has a Help() label Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     int               Default value for the flag
label   string            Label for display in Help()
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*int                      This points to an int whose value is updated as this flag is changed

func NoArg Uses

func NoArg(names []string, help string, process func() error)

Add a new flag that does not allow arguments Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
help    string            The help text (automatically Expand()ed) to display for this flag
process func() os.Error   The function to call when this flag is processed with no argument

func OptArg Uses

func OptArg(names []string, def, help string, process func(string) error)

Add a new flag that may optionally have an argument Parameters:

names []string                 These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     string                 The default of the argument in help, e.g. the "value" part of "--flag=value"
help    string                 The help text (automatically Expand()ed) to display for this flag
process func(string) os.Error  The function to call when this flag is processed with an argument

func Parse Uses

func Parse(extraopts func() []string) bool

This parses the command-line arguments. It returns true if '--' was present. Special flags are:

--help               Display the generated help message (calls Help())
--create-manpage     Display a manpage generated by the goopt library (uses Author, Suite, etc)
--list-options       List all known flags

Arguments:

extraopts func() []string     This function is called by --list-options and returns extra options to display

func ReqArg Uses

func ReqArg(names []string, argname, help string, process func(string) error)

Add a new flag that requires an argument Parameters:

names []string                  These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
argname string                  The name of the argument in help, e.g. the "value" part of "--flag=value"
help    string                  The help text (automatically Expand()ed) to display for this flag
process func(string) os.Error   The function to call when this flag is processed

func String Uses

func String(names []string, def string, help string) *string

Create a required-argument flag that accepts string values Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     string            Default value for the string and label in Help()
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*string                   This points to a string whose value is updated as this flag is changed

func StringWithLabel Uses

func StringWithLabel(names []string, def string, label string, help string) *string

Create a required-argument flag that accepts string values and has a Help() label Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     string            Default value for the string
label   string            Label for display in Help()
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*string                   This points to a string whose value is updated as this flag is changed

func Strings Uses

func Strings(names []string, def string, help string) *[]string

Create a required-argument flag that accepts string values but allows more than one to be specified Parameters:

names []string            These are the names that are accepted on the command-line for this flag, e.g. -v --verbose
def     string            The argument name of the strings that are appended (e.g. the val in --opt=val)
help    string            The help text (automatically Expand()ed) to display for this flag

Returns:

*[]string                 This points to a []string whose value will contain the strings passed as flags

func VisitAllNames Uses

func VisitAllNames(f func(string))

Execute the given closure on the name of all known arguments

Directories

PathSynopsis
example
test-program

Package goopt imports 9 packages (graph) and is imported by 99 packages. Updated 2017-06-05. Refresh now. Tools for package owners.