conq

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2022 License: Apache-2.0 Imports: 12 Imported by: 1

README

conq - go1.18+ generic commander

text/template based help-generation:

$ example help example
`example` is a CLI app built with patroclos/go-conq.

example [commands] [options] arguments
example help [commands|subjects]

Commands:
- help
- completion
- foo
- bar

conq contains Option instances (conq.O struct), these stand for flag-parameters and positional-arguments and are usually used with the generic Opt[T] and ReqOpt[T] types, which implement parsing for scalars and types implementing encoding.TextUnmarshaler. See example to see interesting standard-library types that can just be used as-is, things like IP, Mac-Addresses, etc.

The biggest focus of conq is composability. Eventually there should even be standardized tree-operations to run on a Cmd-tree, like wrapping help-sections, marking deprecations, etc.

The standard help command is just another command-package in aid/cmdhelp, and can be replaced completely, changing how help is resolved from multiple sources and rendered.

Parameters are something special in conq. They have a name (ie a 'depth' parameter). There is a builtin getopt conq.Optioner implementation. It enables extraction and completion of getopt-style flags, and is also completely modular. You can make your own parameter-style extractor.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTerm

func IsTerm(f interface{}) bool

only returns true when f has a `Stat()(fs.FileInfo,error)` method that designates it as a `os.ModeCharDevice`

Types

type Cmd

type Cmd struct {
	Name     string
	Commands []*Cmd
	Run      func(Ctx) error
	Opts     Opts
	Args     Opts
	Env      Opts
	Version  string
}

Cmd is a node in a rooted node tree describing a command-hierarchy.

type CmdHelp

type CmdHelp struct {
	// Filter for the subjects that will considered in help-generation.
	// An interesting usecase could be to skip non-runnable commands or select for
	// a certain depth in the tree.
	Select HelpSelector
	// Articles contains templates for generating helptexts using cmdhelp.HelpContext.
	Articles fs.FS
}

CmdHelp details how this command is to be treated by help/assistance commands

type Commander

type Commander interface {
	ResolveCmd(root *Cmd, ctx Ctx) Ctx
	Execute(root *Cmd, ctx Ctx) error
	Optioner() Optioner
}

type Ctx

type Ctx struct {
	In       io.Reader
	Out, Err io.Writer
	Args     []string
	Values   map[string]any
	Strings  map[string]string
	Printer  *message.Printer
	Path     Pth
	Com      Commander
}

Ctx is the context in which a command (Cmd) runs. It contains the std-streams, arguments (options extracted before calling Cmd.Run), option-values, the path within the command-tree this invocation is located in and a locale-aware message-printer.

func OSContext

func OSContext(args ...string) Ctx

A Ctx value that uses stdin,out,err, the os.Args and a locale-sensitive message.Printer

type HelpSelector

type HelpSelector func(*Cmd, HelpSubject, Helper, string) (accept, recurse bool)

HelpSelector is a func that is used when walking the command-tree to assemble only the subjects the selector accepts into the helptext.

Example

Output:

type HelpSubject

type HelpSubject struct {
	Cmd *Cmd
	Opt *O
	Ctx *Ctx
}

HelpSubject represents a command&option-tree node that is subject to a help query.

type Helper

type Helper interface {
	Help(HelpSubject) string
}

Helper provides a helptexts for nodes in the command&options tree.

type O

type O struct {
	// a comma-separated list of at least one name followed by aliases
	Name string
	// should invoking a command fail, if this option isn't set?
	Require bool
	// a parser for the string extracted from the shell arguments
	Parse func(string) (any, error)
	// describes the type of results returned by Parse
	Type reflect.Type
	// shell-completion
	Predict complete.Predictor
}

O is a descriptor for command-parameters (options, positional arguments or environment variables)

func (O) WithName

func (o O) WithName(name string) O

type Opt

type Opt[T any] O

Opt[T any] wraps a base-option (usually only containing a name) in an Opter interface, which will apply defaults to O.Parse and O.Type values. The default O.Parse implementation will use the github.com/alexflint/go-scalar package to parse a (value T) from a string. The default implementations supports the encoding.TextUnmarshaler interface. Opt[T] is meant both as the definition for the option and as the access-hatch for it's values, so it provides `Get(Ctx)(T,error)` and `Getp(Ctx)(*T, error)` to access the options value or pointer to it from the Ctx.Values map.

func (Opt[T]) Get

func (o Opt[T]) Get(c Ctx) (val T, err error)

func (Opt[T]) Getp

func (o Opt[T]) Getp(c Ctx) (val *T, err error)

func (Opt[T]) Opt

func (o Opt[T]) Opt() O

type Opter

type Opter interface {
	Opt() O
}

This interface exists to facilitate the Opt[T] and ReqOpt[T] types with filter effects

type Optioner

type Optioner interface {
	ExtractOptions(Ctx, ...Opter) (Ctx, error)
	CompleteOptions(completion.Context, ...Opter) []string
}

Optioner provides extraction and completion of CLI options.

type Opts

type Opts []Opter

type Pth

type Pth []*Cmd

type ReqOpt

type ReqOpt[T any] O

ReqOpt[T any] is a simple wrapper for Opt[T]. It's Opter implementation sets the O.Require to true. Assuming the Cmd has been setup correctly we can now know for sure that the Ctx is going to have a value for this option. That's why the `Get(Ctx)T` and `Getp(Ctx)*T` have been simplified from their counterparts in Opt[T]

func (ReqOpt[T]) Get

func (o ReqOpt[T]) Get(c Ctx) (val T)

func (ReqOpt[T]) Getp

func (o ReqOpt[T]) Getp(c Ctx) (val *T)

func (ReqOpt[T]) Opt

func (o ReqOpt[T]) Opt() O

Directories

Path Synopsis
aid

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL