Documentation
¶
Overview ¶
Example ¶
package main
import (
"context"
"fmt"
"go.llib.dev/frameless/pkg/cli"
)
func main() {
var mux cli.Mux
mux.Handle("foo", FooCommand{})
sub := mux.Sub("sub")
sub.Handle("subcmd", SubCommand{})
cli.Main(context.Background(), &mux)
}
type FooCommand struct {
A string `flag:"the-a,a" default:"val" desc:"this is flag A" opt:"T"`
B bool `flag:"the-b,b" default:"true" opt:"T"` // missing description
C int `flag:"c" required:"true" desc:"this is flag C, not B"`
D string `flag:"d" enum:"FOO,BAR,BAZ," desc:"this flag is an enum" opt:"T"`
Arg string `arg:"0" desc:"something something" opt:"T"`
OthArg int `arg:"1" default:"42" opt:"T"`
// Dependency is a dependency of the FooCommand, which is populated though traditional dependency injection.
Dependency string
}
func (cmd FooCommand) ServeCLI(w cli.ResponseWriter, r *cli.Request) {
fmt.Fprintln(w, "hello")
}
type SubCommand struct{}
func (cmd SubCommand) ServeCLI(w cli.ResponseWriter, r *cli.Request) {
w.Write([]byte("sub-cmd"))
}
Example (DependencyInjection) ¶
package main
import (
"context"
"go.llib.dev/frameless/pkg/cli"
)
type CommandWithDependency struct {
Flag1 string `flag:"flag1" default:"val" desc:"this is flag A"`
Flag2 bool `flag:"flag2" default:"true"`
Flag3 int `flag:"othflag" required:"true" desc:"this is flag C, not B"`
Arg1 string `arg:"0" desc:"something something"`
Arg2 int `arg:"1" default:"42" desc:"something something else"`
// Dependency is a dependency of the FooCommand, which is populated though traditional dependency injection.
Dependency any
}
func (CommandWithDependency) ServeCLI(w cli.ResponseWriter, r *cli.Request) {}
func main() {
cli.Main(context.Background(), CommandWithDependency{
Dependency: "important dependency that I need as part of the ServeCLI call",
})
}
Index ¶
- Constants
- Variables
- func ConfigureHandler[H Handler](ptr *H, r *Request) error
- func FPrintTable(w io.Writer, table [][]string, opts ...TableOption) (rErr error)
- func Main(ctx context.Context, h Handler)
- func ServeCLI(h Handler, w ResponseWriter, r *Request)
- func Usage(h Handler, command string) (string, error)
- type ErrInvalidInput
- type ErrorWriter
- type Handler
- type HandlerFunc
- type HelpSummary
- type HelpUsage
- type InitDefaultTagValue
- type Multiplexer
- type Mux
- type Request
- type Responsedeprecated
- type ResponseRecorder
- type ResponseWriter
- type StdResponse
- type TableConfig
- type TableOption
Examples ¶
Constants ¶
View Source
const ( // ExitCodeOK : Success ExitCodeOK = 0 // ExitCodeError : General Error ExitCodeError = 1 // ExitCodeBadRequest : Misuse of shell builtins or invalid command-line usage, often equated with a bad request. ExitCodeBadRequest = 2 )
View Source
const ( ErrFlagMissing errorkitlite.Error = "ErrFlagMissing" ErrFlagParseIssue errorkitlite.Error = "ErrFlagParseIssue" ErrFlagInvalid errorkitlite.Error = "ErrFlagInvalid" ErrArgMissing errorkitlite.Error = "ErrArgMissing" ErrArgParseIssue errorkitlite.Error = "ErrArgParseIssue" ErrArgIndexInvalid errorkitlite.Error = "ErrArgIndexInvalid" ErrInvalidDefaultValue errorkitlite.Error = "ErrInvalidDefaultValue" )
Variables ¶
View Source
var EnumError = errorkit.UserError{
Code: "enum-error",
Message: "invalid enumeration value",
}
Functions ¶
func ConfigureHandler ¶ added in v0.276.0
func FPrintTable ¶ added in v0.308.0
func FPrintTable(w io.Writer, table [][]string, opts ...TableOption) (rErr error)
func ServeCLI ¶ added in v0.308.0
func ServeCLI(h Handler, w ResponseWriter, r *Request)
Types ¶
type ErrInvalidInput ¶ added in v0.311.0
type ErrInvalidInput struct {
Err error
// contains filtered or unexported fields
}
func (ErrInvalidInput) ArgIndex ¶ added in v0.311.0
func (err ErrInvalidInput) ArgIndex() (int, bool)
func (ErrInvalidInput) Envs ¶ added in v0.311.0
func (err ErrInvalidInput) Envs() []string
func (ErrInvalidInput) Error ¶ added in v0.311.0
func (err ErrInvalidInput) Error() string
func (ErrInvalidInput) Flags ¶ added in v0.311.0
func (err ErrInvalidInput) Flags() []string
func (ErrInvalidInput) Unwrap ¶ added in v0.311.0
func (err ErrInvalidInput) Unwrap() error
func (ErrInvalidInput) ValidateError ¶ added in v0.311.0
func (err ErrInvalidInput) ValidateError() (validate.Error, bool)
type ErrorWriter ¶
type Handler ¶
type Handler interface {
ServeCLI(w ResponseWriter, r *Request)
}
type HandlerFunc ¶
type HandlerFunc func(w ResponseWriter, r *Request)
func (HandlerFunc) ServeCLI ¶
func (fn HandlerFunc) ServeCLI(w ResponseWriter, r *Request)
type HelpSummary ¶
type HelpSummary interface {
// Summary returns a summary about the application
//
// TODO: Maybe ranem this to "Desc" as the tag "desc" is used for this purpose
Summary() string
}
type InitDefaultTagValue ¶ added in v0.311.0
type Multiplexer ¶ added in v0.276.0
Multiplexer is an interface that, when implemented by a command, delegates the parsing of input arguments and options to the Handler in cli.Main.
If you want to create your own Mux, simply implement this interface in your structure.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
func (*Mux) ServeCLI ¶
func (m *Mux) ServeCLI(w ResponseWriter, r *Request)
type Request ¶
func NewStdRequest ¶ added in v0.308.0
type Response
deprecated
type ResponseRecorder ¶
func (*ResponseRecorder) ExitCode ¶
func (rr *ResponseRecorder) ExitCode(n int)
func (*ResponseRecorder) Stdeout ¶
func (rr *ResponseRecorder) Stdeout() io.Writer
func (*ResponseRecorder) Stderr ¶
func (rr *ResponseRecorder) Stderr() (_ io.Writer)
type ResponseWriter ¶ added in v0.311.0
type StdResponse ¶ added in v0.308.0
type StdResponse struct{ Code int }
func (*StdResponse) ExitCode ¶ added in v0.308.0
func (rr *StdResponse) ExitCode(n int)
func (*StdResponse) Stdeout ¶ added in v0.308.0
func (rr *StdResponse) Stdeout() io.Writer
func (*StdResponse) Stderr ¶ added in v0.308.0
func (rr *StdResponse) Stderr() io.Writer
type TableConfig ¶ added in v0.308.0
func (TableConfig) Configure ¶ added in v0.308.0
func (c TableConfig) Configure(t *TableConfig)
func (*TableConfig) Init ¶ added in v0.308.0
func (c *TableConfig) Init()
type TableOption ¶ added in v0.308.0
type TableOption option.Option[TableConfig]
func TablePadding ¶ added in v0.308.0
func TablePadding(padding int) TableOption
func TableRowPrefix ¶ added in v0.308.0
func TableRowPrefix(prefix string) TableOption
Click to show internal directories.
Click to hide internal directories.