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"`
B bool `flag:"the-b,b" default:"true"` // 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"`
Arg string `arg:"0" desc:"something something"`
OthArg int `arg:"1" default:"42"`
// Dependency is a dependency of the FooCommand, which is populated though traditional dependency injection.
Dependency string
}
func (cmd FooCommand) ServeCLI(w cli.Response, r *cli.Request) {
fmt.Fprintln(w, "hello")
}
type SubCommand struct{}
func (cmd SubCommand) ServeCLI(w cli.Response, 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.Response, 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 Response, r *Request)
- func Usage(h Handler, command string) (string, error)
- type ErrorWriter
- type Handler
- type HandlerFunc
- type HelpSummary
- type HelpUsage
- type Multiplexer
- type Mux
- type Request
- type Response
- type ResponseRecorder
- 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)
Types ¶
type ErrorWriter ¶
type HandlerFunc ¶
func (HandlerFunc) ServeCLI ¶
func (fn HandlerFunc) ServeCLI(w Response, 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 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 Request ¶
func NewStdRequest ¶ added in v0.308.0
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 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.