Documentation
¶
Overview ¶
Package middleware provides useful middleware for a clir.Router.
Index ¶
- func Args(cb func(as *ArgSet)) clir.Middleware
- func Flags(cb func(fs *flag.FlagSet)) clir.Middleware
- type ArgSet
- func (a *ArgSet) Args() []string
- func (a *ArgSet) Bool(name string, value bool, usage string) *bool
- func (a *ArgSet) BoolVar(p *bool, name string, value bool, usage string)
- func (a *ArgSet) Float64(name string, value float64, usage string) *float64
- func (a *ArgSet) Float64Var(p *float64, name string, value float64, usage string)
- func (a *ArgSet) Int(name string, value int, usage string) *int
- func (a *ArgSet) IntVar(p *int, name string, value int, usage string)
- func (a *ArgSet) Parse(args []string) error
- func (a *ArgSet) SetOutput(w io.Writer)
- func (a *ArgSet) String(name string, value string, usage string) *string
- func (a *ArgSet) StringVar(p *string, name string, value string, usage string)
- func (a *ArgSet) Var(value flag.Value, name string, usage string)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Args ¶
func Args(cb func(as *ArgSet)) clir.Middleware
Args middleware allows you to set positional arguments on a route.
Example ¶
package main
import (
"os"
"maragu.dev/clir"
"maragu.dev/clir/middleware"
)
func main() {
r := clir.NewRouter()
var name *string
r.Use(middleware.Args(func(as *middleware.ArgSet) {
name = as.String("name", "world", "name to greet")
}))
r.RouteFunc("", func(ctx clir.Context) error {
ctx.Printfln("Hello, %s!", *name)
return nil
})
_ = r.Run(clir.Context{
Args: []string{"Funky Person"},
Out: os.Stdout,
})
}
Output: Hello, Funky Person!
func Flags ¶
func Flags(cb func(fs *flag.FlagSet)) clir.Middleware
Flags middleware allows you to set flags on a route.
Example ¶
package main
import (
"flag"
"os"
"maragu.dev/clir"
"maragu.dev/clir/middleware"
)
func main() {
r := clir.NewRouter()
var v *bool
r.Use(middleware.Flags(func(fs *flag.FlagSet) {
v = fs.Bool("v", false, "verbose output")
}))
r.RouteFunc("", func(ctx clir.Context) error {
if *v {
ctx.Println("Hello!")
}
return nil
})
_ = r.Run(clir.Context{
Args: []string{"-v"},
Out: os.Stdout,
})
}
Output: Hello!
Types ¶
type ArgSet ¶
type ArgSet struct {
Usage func()
// contains filtered or unexported fields
}
ArgSet is like flag.FlagSet but for positional arguments. The order of calls is significant.
func (*ArgSet) Float64Var ¶
Float64Var defines a float64 positional argument with a pointer.
Click to show internal directories.
Click to hide internal directories.