Documentation ¶
Overview ¶
Package cli generates powerful CLIs from Go struct types and functions. See package clicore to create a GUI representation of a CLI.
Index ¶
- Constants
- func ParseDirective(comment string) (*types.Directive, error)
- func Run[T any, C CmdOrFunc[T]](opts *Options, cfg T, cmds ...C) error
- func SetFromArgs[T any](cfg T, args []string, errNotFound bool, cmds ...*Cmd[T]) (string, error)
- func SetFromDefaults(cfg any) error
- type Cmd
- type CmdOrFunc
- type OnConfigurer
- type Options
Constants ¶
const ( // ErrNotFound can be passed to [SetFromArgs] and [ParseFlags] // to indicate that they should return an error for a flag that // is set but not found in the configuration struct. ErrNotFound = true // ErrNotFound can be passed to [SetFromArgs] and [ParseFlags] // to indicate that they should NOT return an error for a flag that // is set but not found in the configuration struct. NoErrNotFound = false )
Variables ¶
This section is empty.
Functions ¶
func ParseDirective ¶
ParseDirective parses and returns a comment directive from the given comment string. The returned directive will be nil if there is no directive contained in the given comment. Directives are of the following form (the slashes are optional):
//tool:directive args...
func Run ¶
Run runs an app with the given options, configuration struct, and commands. It does not run the GUI; see cogentcore.org/core/cli/clicore.Run for that. The configuration struct should be passed as a pointer, and configuration options should be defined as fields on the configuration struct. The commands can be specified as either functions or struct objects; the functions are more concise but require using types. In addition to the given commands, Run adds a "help" command that prints the result of [usage], which will also be the root command if no other root command is specified. Also, it adds the fields in [metaConfig] as configuration options. If [Options.Fatal] is set to true, the error result of Run does not need to be handled. Run uses os.Args for its arguments.
func SetFromArgs ¶
SetFromArgs sets config values on the given config object from the given from command-line args, based on the field names in the config struct and the given list of available commands. It returns the command, if any, that was passed in the arguments, and any error than occurs during the parsing and setting process. If errNotFound is set to true, it is assumed that all flags (arguments starting with a "-") must refer to fields in the config struct, so any that fail to match trigger an error. It is recommended that the ErrNotFound and NoErrNotFound constants be used for the value of errNotFound for clearer code.
func SetFromDefaults ¶
SetFromDefaults sets the values of the given config object from `default:` struct field tag values. Errors are automatically logged in addition to being returned.
Types ¶
type Cmd ¶
type Cmd[T any] struct { // Func is the actual function that runs the command. // It takes configuration information and returns an error. Func func(T) error // Name is the name of the command. Name string // Doc is the documentation for the command. Doc string // Root is whether the command is the root command // (what is called when no subcommands are passed) Root bool // Icon is the icon of the command in the tool bar // when running in the GUI via clicore Icon string // SepBefore is whether to add a separator before the // command in the tool bar when running in the GUI via clicore SepBefore bool // SepAfter is whether to add a separator after the // command in the tool bar when running in the GUI via clicore SepAfter bool }
Cmd represents a runnable command with configuration options. The type constraint is the type of the configuration information passed to the command.
func AddCmd ¶
AddCmd adds the given command to the given set of commands if there is not already a command with the same name in the set of commands. Also, if [Cmd.Root] is set to true on the passed command, and there are no other root commands in the given set of commands, the passed command will be made the root command; otherwise, it will be made not the root command.
type CmdOrFunc ¶
CmdOrFunc is a generic type constraint that represents either a *Cmd with the given config type or a command function that takes the given config type and returns an error.
type OnConfigurer ¶
OnConfigurer represents a configuration object that specifies a method to be called at the end of the [config] function, with the command that has been parsed as an argument.
type Options ¶
type Options struct { // AppName is the name of the cli app. AppName string // AppAbout is the description of the cli app. AppAbout string // Fatal is whether to, if there is an error in [Run], // print it and fatally exit the program through [os.Exit] // with an exit code of 1. Fatal bool // PrintSuccess is whether to print a message indicating // that a command was successful after it is run, unless // the user passes -q or -quiet to the command, in which // case the success message will always not be printed. PrintSuccess bool // DefaultEncoding is the default encoding format for config files. // currently toml is the only supported format, but others could be added // if needed. DefaultEncoding string // DefaultFiles are the default configuration file paths DefaultFiles []string // IncludePaths is a list of file paths to try for finding config files // specified in Include field or via the command line --config --cfg or -c args. // Set this prior to calling Config; default is current directory '.' and 'configs'. // The include paths are searched in reverse order such that first specified include // paths get the highest precedence (config files found in earlier include paths // override those found in later ones). IncludePaths []string // SearchUp indicates whether to search up the filesystem // for the default config file by checking the provided default // config file location relative to each directory up the tree SearchUp bool // NeedConfigFile indicates whether a configuration file // must be provided for the command to run NeedConfigFile bool }
Options contains the options passed to cli that control its behavior.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package clicore extends package cli by generating Cogent Core GUIs.
|
Package clicore extends package cli by generating Cogent Core GUIs. |
examples
|
|