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")) }
Output:
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", }) }
Output:
Index ¶
- Constants
- Variables
- func ConfigureHandler[H Handler](h H, path string, r *Request) (zero H, _ error)
- func Main(ctx context.Context, h Handler)
- func Usage(h Handler, pattern string) (string, error)
- type ErrorWriter
- type Handler
- type HandlerFunc
- type HelpSummary
- type HelpUsage
- type Multiplexer
- type Mux
- type Request
- type Response
- type ResponseRecorder
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 errorkit.Error = "ErrFlagMissing" ErrFlagParseIssue errorkit.Error = "ErrFlagParseIssue" ErrFlagInvalid errorkit.Error = "ErrFlagInvalid" ErrArgMissing errorkit.Error = "ErrArgMissing" ErrArgParseIssue errorkit.Error = "ErrArgParseIssue" ErrArgIndexInvalid errorkit.Error = "ErrArgIndexInvalid" ErrInvalidDefaultValue errorkit.Error = "ErrInvalidDefaultValue" )
Variables ¶
View Source
var EnumError = errorkit.UserError{
Code: "enum-error",
Message: "invalid enumeration value",
}
Functions ¶
func ConfigureHandler ¶ added in v0.276.0
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 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)
Click to show internal directories.
Click to hide internal directories.