Documentation ¶
Overview ¶
Flargs provides pleasant, fragrant, parsimonious parsing of flags and arguments, resulting in clean, testable, hermetic apps that provide feasible breeze and are easy to reason about.
Index ¶
- Constants
- func Pipe(f1 Command, f2 Command) (int64, error)
- type Command
- type Environment
- type ExitCode
- type FlargError
- type Flarger
- type NullDevice
- func (b NullDevice) Open(_ string) (fs.File, error)
- func (b NullDevice) OpenFile(name string, flag int, perm fs.FileMode) (rfs.WritableFile, error)
- func (b NullDevice) Read(_ []byte) (int, error)
- func (b NullDevice) ReadDir(_ string) ([]fs.DirEntry, error)
- func (b NullDevice) ReadFile(_ string) ([]byte, error)
- func (b NullDevice) Remove(_ string) error
- func (b NullDevice) Stat(_ string) (fs.FileInfo, error)
- func (b NullDevice) WriteFile(_ string, _ []byte, _ fs.FileMode) error
- type Phase
- type StateMachine
Constants ¶
const ( ExitCodeSuccess ExitCode = iota ExitCodeGenericError ExitCodeMisuseOfBuiltIns ExitCodeCannotExecute = iota + 123 ExitCodeCommandNotFound // Command not found ExitCodeInvalidArgumentToExit ExitCodeFatalErrorSignal1 ExitCodeFatalErrorSignal2 // Ctrl-C was pressed ExitCodeFatalErrorSignal3 ExitCodeFatalErrorSignal4 ExitCodeFatalErrorSignal5 ExitCodeFatalErrorSignal6 ExitCodeFatalErrorSignal7 ExitCodeFatalErrorSignal8 ExitCodeFatalErrorSignal9 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Command ¶
type Command struct { Flarger *Environment }
a Command is a Flarger with an Environment
func (Command) Load ¶ added in v1.0.0
Load processes the flarg configuration in the context of an Environment
func (Command) LoadAndRun ¶ added in v1.1.0
LoadAndRun combines Command.Load and Command.Run
func (Command) ParseAndLoad ¶ added in v1.1.0
combines [Command.Parse] and Command.Load
func (Command) Run ¶
Run runs Flarger.Run in the context of an Environment
type Environment ¶
type Environment struct { InputStream io.ReadWriter OutputStream io.ReadWriter ErrorStream io.ReadWriter Randomness rand.Source Filesystem rfs.WritableFs Variables map[string]string Arguments []string }
Enviroment is an execution environment for a Command. In the context of a CLI, these would be os.StdIn, os.StdOut, etc. In the context of a test-suite, you can use bytes.Buffer and [fstest.MapFS]. For benchmarking, you can use a NullDevice.
func NewCLIEnvironment ¶
func NewCLIEnvironment(baseDir string) *Environment
NewCLIEnvironment produces an Environment suitable for a CLI. It's a helper function with sane defaults.
func NewNullEnvironment ¶ added in v1.1.0
func NewNullEnvironment() *Environment
func NewTestingEnvironment ¶
func NewTestingEnvironment(randomnessProvider rand.Source) *Environment
NewTestingEnvironment produces an Environment suitable for testing. Pass in a "randomnessProvider" that offers a level of determinism that works for you. For good ole fashioned regular randomness, pass in rand.Reader If your program doesn't use randomness, just pass in nil.
func (Environment) GetError ¶ added in v0.1.2
func (e Environment) GetError() []byte
func (Environment) GetInput ¶ added in v0.1.2
func (e Environment) GetInput() []byte
func (Environment) GetOutput ¶ added in v0.1.2
func (e Environment) GetOutput() []byte
type FlargError ¶
func NewFlargError ¶
func NewFlargError(exitcode ExitCode, underlying error) *FlargError
func (*FlargError) Error ¶
func (fe *FlargError) Error() string
type Flarger ¶ added in v1.0.0
type Flarger interface { Parse([]string) error Load(*Environment) error Run(*Environment) error }
a Flarger is a custom object that represents the state and functionality of your command
type NullDevice ¶ added in v1.1.0
func (NullDevice) OpenFile ¶ added in v1.5.0
func (b NullDevice) OpenFile(name string, flag int, perm fs.FileMode) (rfs.WritableFile, error)
func (NullDevice) ReadDir ¶ added in v1.5.0
func (b NullDevice) ReadDir(_ string) ([]fs.DirEntry, error)
func (NullDevice) Remove ¶ added in v1.5.0
func (b NullDevice) Remove(_ string) error
type StateMachine ¶ added in v1.1.0
StateMachine implements Flarger it provides basic functionality and default methods allowing you to not bother writing them if you don't need them
func (*StateMachine) Load ¶ added in v1.1.0
func (s *StateMachine) Load(_ *Environment) error
no-op. This will run if you don't define Load() in your konf.
func (*StateMachine) Parse ¶ added in v1.1.0
func (s *StateMachine) Parse(a []string) error
no-op. This will run if you don't define Parse() in your konf.
func (*StateMachine) Run ¶ added in v1.1.0
func (s *StateMachine) Run(_ *Environment) error
no-op. This will run if you don't define Run() in your konf.