Documentation
¶
Overview ¶
*
- cli is a package intended to encourage some standardization in the command line user interface for programs
- developed for Caltech Library. *
- @author R. S. Doiel, <rsdoiel@caltech.edu> *
- Copyright (c) 2016, Caltech
- All rights not granted herein are expressly reserved by Caltech. *
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. *
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*
- cli is a package intended to encourage some standardization in the command line user interface for programs
- developed for Caltech Library. *
- @author R. S. Doiel, <rsdoiel@caltech.edu> *
- Copyright (c) 2016, Caltech
- All rights not granted herein are expressly reserved by Caltech. *
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. *
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
*
- cli is a package intended to encourage some standardization in the command line user interface for programs
- developed for Caltech Library. *
- @author R. S. Doiel, <rsdoiel@caltech.edu> *
- Copyright (c) 2016, Caltech
- All rights not granted herein are expressly reserved by Caltech. *
- Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *
- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. *
- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. *
- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. *
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
generator.go - provides a quick and dirty skeleton for cli based apps.
manpage.go - adds man page output formatting suitable for running through `nroff -man` and rendering a man page. It is a part of the cli page.
Author: R. S. Doiel, <rsdoiel@library.caltech.edu>
Copyright (c) 2018, Caltech All rights not granted herein are expressly reserved by Caltech.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
markdown.go - this is a part of the cli package. This code focuses on generating Markdown docs from the internal help information.
usage.go - handles rendering the "usage" message for cli package
Index ¶
- Constants
- func CloseFile(filename string, fp *os.File) error
- func Create(filename string, fallbackFile *os.File) (*os.File, error)
- func ExitOnError(out *os.File, err error, quiet bool)
- func Generate(...) []byte
- func IsPipe(in *os.File) bool
- func OnError(out *os.File, err error, quiet bool)
- func Open(filename string, fallbackFile *os.File) (*os.File, error)
- func PopArg(args []string) (string, []string)
- func PushArg(s string, args []string) []string
- func ReadLines(in *os.File) ([]string, error)
- func ShiftArg(args []string) (string, []string)
- func UnshiftArg(s string, args []string) []string
- type Action
- type Cli
- func (c *Cli) Action(verb string) string
- func (c *Cli) Actions() map[string]string
- func (c *Cli) AddAction(verb string, fn func(io.Reader, io.Writer, io.Writer, []string) int, ...) error
- func (c *Cli) AddHelp(keyword string, usage []byte) error
- func (c *Cli) AddParams(params ...string)
- func (c *Cli) AddVerb(verb string, usage string) error
- func (c *Cli) AppName() string
- func (c *Cli) Arg(i int) string
- func (c *Cli) Args() []string
- func (c *Cli) BoolVar(p *bool, names string, value bool, usage string)
- func (c *Cli) DurationVar(p *time.Duration, names string, value time.Duration, usage string)
- func (c *Cli) Env(name string) string
- func (c *Cli) EnvAttribute(name string) (*EnvAttribute, error)
- func (c *Cli) EnvBool(name string, value bool, usage string) *bool
- func (c *Cli) EnvBoolVar(p *bool, name string, value bool, usage string) error
- func (c *Cli) EnvDuration(name string, value time.Duration, usage string) *time.Duration
- func (c *Cli) EnvDurationVar(p *time.Duration, name string, value time.Duration, usage string) error
- func (c *Cli) EnvFloat64(name string, value float64, usage string) *float64
- func (c *Cli) EnvInt(name string, value int, usage string) *int
- func (c *Cli) EnvInt64(name string, value int64, usage string) *int64
- func (c *Cli) EnvInt64Var(p *int64, name string, value int64, usage string) error
- func (c *Cli) EnvIntVar(p *int, name string, value int, usage string) error
- func (c *Cli) EnvString(name string, value string, usage string) *string
- func (c *Cli) EnvStringVar(p *string, name string, value string, usage string) error
- func (c *Cli) EnvUint(name string, value uint, usage string) *uint
- func (c *Cli) EnvUint64(name string, value uint64, usage string) *uint64
- func (c *Cli) EnvUint64Var(p *uint64, name string, value uint64, usage string) error
- func (c *Cli) EnvUintVar(p *uint, name string, value uint, usage string) error
- func (c *Cli) Float64Var(p *float64, names string, value float64, usage string)
- func (c *Cli) GenerateManPage(w io.Writer)
- func (c *Cli) GenerateMarkdown(w io.Writer)
- func (c *Cli) Getenv(name string) string
- func (c *Cli) Help(keywords ...string) string
- func (c *Cli) Int64Var(p *int64, names string, value int64, usage string)
- func (c *Cli) IntVar(p *int, names string, value int, usage string)
- func (c *Cli) License() string
- func (c *Cli) NArg() int
- func (c *Cli) Option(op string) string
- func (c *Cli) Options() map[string]string
- func (c *Cli) Parse() error
- func (c *Cli) ParseEnv() error
- func (c *Cli) ParseOptions()
- func (c *Cli) Run(args []string) int
- func (c *Cli) StringVar(p *string, names string, value string, usage string)
- func (c *Cli) Uint64Var(p *uint64, names string, value uint64, usage string)
- func (c *Cli) UintVar(p *uint, names string, value uint, usage string)
- func (c *Cli) Usage(w io.Writer)
- func (c *Cli) Verb(args []string) string
- func (c *Cli) Version() string
- type Config
- func (cfg *Config) AddExample(topic, text string)
- func (cfg *Config) AddHelp(topic, text string)
- func (cfg *Config) CheckOption(envVar, value string, required bool) string
- func (cfg *Config) Example(topics ...string) string
- func (cfg *Config) Get(key string) string
- func (cfg *Config) Help(topics ...string) string
- func (cfg *Config) License() string
- func (cfg *Config) MergeEnv(envVar, flagValue string) string
- func (cfg *Config) MergeEnvBool(envVar string, flagValue bool) bool
- func (cfg *Config) StandardOptions(showHelp, showExamples, showLicense, showVersion bool, args []string) string
- func (cfg *Config) Usage() string
- func (cfg *Config) Version() string
- type EnvAttribute
Constants ¶
const Version = `v0.0.13`
Variables ¶
This section is empty.
Functions ¶
func CloseFile ¶
CloseFile accepts a filename and os.File pointer, if filename is "" or "-" it skips the close otherwise is does a fp.Close() on the file.
func Create ¶
Create accepts a filename, fallbackFile (usually os.Stdout, os.Stdin, os.Stderr) and returns a file pointer and error. It is a conviences function for wrapping stdin, stdout, stderr If filename is "-" or filename is "" then fallbackFile is used.
func ExitOnError ¶
ExitOnError is used by the cli programs to handle exit cuasing errors constitantly. E.g. it respects the -quiet flag past to it.
func Generate ¶ added in v0.0.13
func Generate(appName, synopsis, author, descriptionFName, examplesFName, bugsFName, licenseFName string) []byte
Generate creates main.go source code
func IsPipe ¶
IsPipe accepts a file pointer and returns true if data on file pointer is from a pipe or false if not.
func OnError ¶
OnError writes an error message to out if err != nil taking into consideration the state of quiet
func Open ¶
Open accepts a filename, fallbackFile (usually os.Stdout, os.Stdin, os.Stderr) and returns a file pointer and error. It is a conviences function for wrapping stdin, stdout, stderr If filename is "-" or filename is "" then fallbackFile is used.
func PopArg ¶
PopArg takes an array pops the last element returning the element and an updated array. If no elements are left then a empty string and nil are turned.
func ShiftArg ¶ added in v0.0.13
ShiftArg takes an array of strings and if array is not empty returns a string and the rest of the args. If array is empty it returns an empty string, when there are no more args it returns nil for the arg parameter
func UnshiftArg ¶ added in v0.0.13
Unshift takes a string and an array of strings and returns a new array with the value of string as the first element and the rest are elements of the array of strings
Types ¶
type Action ¶
type Action struct {
// Name is usually a verb like list, test, build as needed by the cli
Name string
// Fn is action that will be run by Cli.Run() if Name is the first non-option arg on the command line
//NOTE: currently the signature is io based but may be changed to *os.File in
// the future
Fn func(io.Reader, io.Writer, io.Writer, []string) int
// Usage is a short description of what the action does and description of any expected additoinal parameters
Usage string
}
Action describes an "action" that a cli might take. Actions aren't prefixed with a "-".
type Cli ¶
type Cli struct {
// In is usually set to os.Stdin
In *os.File
// Out is usually set to os.Stdout
Out *os.File
// Eout is usually set to os.Stderr
Eout *os.File
// Documentation specific help pages, e.g. -help example1
Documentation map[string][]byte
// SectionNo is the numeric value section value for man page generation
SectionNo int
// ActionsRequired is true then the USAGE line shows ACTION rather than [ACTION]
ActionsRequired bool
// contains filtered or unexported fields
}
Cli models the metadata for running a common cli program
func NewCli ¶
NewCli creates an Cli instance, an Cli describes the running of the command line interface making it easy to expose the functionality in packages as command line tools.
func (*Cli) AddAction ¶
func (c *Cli) AddAction(verb string, fn func(io.Reader, io.Writer, io.Writer, []string) int, usage string) error
AddAction associates a wrapping function with a action name, the wrapping function has 4 parameters in io.Reader, out io.Writer, err io.Writer, args []string. It should return an integer reflecting an exit code like you'd pass to os.Exit().
func (*Cli) AddHelp ¶
AddHelp takes a string keyword and byte slice of content and updates the Documentation attribute
func (*Cli) AddParams ¶
Add Params documents any parameters not defined as Options or Actions, it is an orders list of strings
func (*Cli) AddVerb ¶ added in v0.0.13
AddVerb associates documentation with a verb without assigning a function (e.g. if you aren't going to use cli.Run() so we don't need an "Action" only the docs
func (*Cli) BoolVar ¶
BoolVar updates c.options doc strings, then splits options and calls flag.BoolVar()
func (*Cli) DurationVar ¶
DurationVar updates c.options doc strings, then splits options and calls flag.DurationVar()
func (*Cli) EnvAttribute ¶
func (c *Cli) EnvAttribute(name string) (*EnvAttribute, error)
EnvAttribute returns the struct corresponding to the matchine name
func (*Cli) EnvBool ¶
EnvBool adds an environment variable which is evaluate before evaluating options returns a pointer to the value.
func (*Cli) EnvBoolVar ¶
EnvBoolVar adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.BoolVar()
func (*Cli) EnvDuration ¶
EnvDuration adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.DurationVar()
func (*Cli) EnvDurationVar ¶
func (c *Cli) EnvDurationVar(p *time.Duration, name string, value time.Duration, usage string) error
EnvDurationVar adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.DurationVar()
func (*Cli) EnvFloat64 ¶
EnvFloat64 adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.Float64Var()
func (*Cli) EnvInt ¶
EnvInt adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.IntVar()
func (*Cli) EnvInt64 ¶
EnvInt64 adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.Int64Var()
func (*Cli) EnvInt64Var ¶
EnvInt64Var adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.Int64Var()
func (*Cli) EnvIntVar ¶
EnvIntVar adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.IntVar()
func (*Cli) EnvString ¶
EnvString adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.StringVar()
func (*Cli) EnvStringVar ¶
EnvStringVar adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.StringVar()
func (*Cli) EnvUint ¶
EnvUint adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.UintVar()
func (*Cli) EnvUint64 ¶
EnvUint64 adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.Uint64Var()
func (*Cli) EnvUint64Var ¶
EnvUint64Var adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.Uint64Var()
func (*Cli) EnvUintVar ¶
EnvUintVar adds environment variable which is evaluate before evaluating options It is the environment counterpart to flag.UintVar()
func (*Cli) Float64Var ¶
Float64Var updates c.options doc strings, then splits options and calls flag.Float64Var()
func (*Cli) GenerateManPage ¶ added in v0.0.13
GenerateManPage writes a manual page suitable for running through `nroff --man`. May need some human clean up depending on content and internal formatting (e.g markdown style, spacing, etc.)
func (*Cli) GenerateMarkdown ¶ added in v0.0.13
GenerateMarkdown writes a Markdown page to io.Writer provided. Documentation is based on the application's metadata like app name, version, options, actions, etc.
func (*Cli) Int64Var ¶
Int64Var updates c.options doc strings, then splits options and calls flag.Int64Var()
func (*Cli) IntVar ¶
IntVar updates c.options doc strings, then splits options and calls flag.IntVar()
func (*Cli) ParseEnv ¶
ParseEnv loops through the os environment using os.Getenv() and updates c.env EnvAttribute. Returns an error if there is a problem with environment.
func (*Cli) ParseOptions ¶
func (c *Cli) ParseOptions()
ParseOptions envokes flag.Parse() updating variables set in AddOptions
func (*Cli) Run ¶
(c *Cli) Run takes a list of non-option arguments and runs them if the fist arg (i.e. arg[0] has a corresponding action. Returns an int suitable to passing to os.Exit()
func (*Cli) StringVar ¶
StringVar updates c.options doc strings, then splits options and calls flag.StringVar()
func (*Cli) Uint64Var ¶
Uint64Var updates c.options doc strings, then splits options and calls flag.Int64Var()
func (*Cli) UintVar ¶
UintVar updates c.options doc strings, then splits options and calls flag.Int64Var()
func (*Cli) Usage ¶
Usage writes a help page to io.Writer provided. Documentation is based on the application's metadata like app name, version, options, actions, etc.
type Config ¶
type Config struct {
EnvPrefix string `json:"env_prefix"`
LicenseText string `json:"license_text"`
VersionText string `json:"version_text"`
UsageText string `json:"usage_text"`
DescriptionText string `json:"description_text"`
ExampleText string `json:"example_text"`
OptionText string `json:"option_text"`
// Options are the running options for an application, often this can be expressed as a cli
// parameter or an environment variable.
Options map[string]string `json:"options"`
// contains filtered or unexported fields
}
Config holds the merged environment and options selected when the program runs DEPRECIATED
func (*Config) AddExample ¶
AddExample takes a topic and example text and adds it to the examples index DEPRECIATED
func (*Config) AddHelp ¶
AddTopic takes a topic and text and address it to the help index DEPRECIATED
func (*Config) CheckOption ¶
CheckOption checks the trimmer string value, if len is 0, log an error message and if required is true exit(1) else return the value passed in. DEPRECIATED
func (*Config) Get ¶
Get returns the associate value of the key in the Config struct's Options DEPRECIATED
func (*Config) MergeEnv ¶
MergeEnv merge environment variables into the configuration structure. options are + prefix - e.g. EPGO, name space before the first underscore in the envinronment
- prefix plus uppercase key forms the complete environment variable name
+ key - the field map (e.g ApiURL maps to API_URL in EPGO_API_URL for prefix EPGO) + proposedValue - the proposed value, usually the value from the flags passed in (an empty string means no value provided)
returns the new value of the environment string merged DEPRECIATED
func (*Config) MergeEnvBool ¶
MergeEnvBool till pick use flagValue if present, otherwise is the environment value. It returns the value selected (e.g. useful when combined with CheckOption() DEPRECIATED
func (*Config) StandardOptions ¶
func (cfg *Config) StandardOptions(showHelp, showExamples, showLicense, showVersion bool, args []string) string
StandardOptions() processing the booleans associated with standard options and any additional cli parameters in args return text as a string. DEPRECIATED
type EnvAttribute ¶
type EnvAttribute struct {
// Name is the environment variable (e.g. DATASET, USERNAME)
Name string
// Type holds the type name of the attribute, e.g. int, int64, float64, string, bool, uint, uint64, time.Duration
Type string
// BoolValue holds the default boolean
BoolValue bool
// IntValue holds the default int
IntValue int
// Int64Value holds the default int64
Int64Value int64
// UintValue holds the default uint
UintValue uint
// Uint64Value holds the default uint64
Uint64Value uint64
// Float64Value holds the default float64
Float64Value float64
// Dura1tionValue holds the default time.Duration
DurationValue time.Duration
// StringValue holds the default string
StringValue string
// Usage describes the environment variable role and expected setting
Usage string
}
EnvAttribute describes expected environmental attributes associated with the cli app. It also provides the default value of the environmental attribute if missing from the environment.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
cligenerate
command
cligenerate is a cli application generator, e.g.
|
cligenerate is a cli application generator, e.g. |