cli

package module
v0.0.0-...-8592f98 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 8 Imported by: 0

README

CLI

Go CLI tool, used to create a CLI only with a config file and a struct with the methods.

How it works

  1. Define config in a config file
  2. Create a struct which has methods as defined in the config file
  3. Apply it all together

Example

  1. Sample yaml file
---
initFunc: Setup
exitFunc: Leave
exitCmd: leave
helpCmd: help
prompt: ">>> "
commands:
  - label: show
    arguments:
      - label: ""
        help: "Show will show the stuff you want"
        execFunc: MyFunc
        options:
          - label: readOnly
            short: -r
            long: --read-only
            help: "Short desc"
            variable:
              label: var4
              required: false
              defaul: "das"
          - label: desc
            short: -d
            long: --description
            help: "Short desc"
            variable:
              label: var4
              required: true
              defaul: "das"
      - label: daily-tasks
        help: "Show will show the other stuff you want"
        execFunc: MyFunc2
        options:
          - label: readOnly
            short: -r
            long: --read-only
            help: "Short desc"
            variable:
              label: var4
              required: true
              defaul: "das"
  1. Program struct
type Program struct {
}

func (program *Program) Setup(cli.Flags) []byte {
	return []byte("Starting\n")
}

func (program *Program) Leave(cli.Flags) []byte {
	return []byte("Leaving\n")
}

func (program *Program) MyFunc(flags cli.Flags) []byte {
	return []byte(fmt.Sprintf("MyFunc called flags %v\n", flags))
}

func (program *Program) MyFunc2(flags cli.Flags) []byte {
	return []byte(fmt.Sprintf("MyFunc2 called flags %v\n", flags))
}
  1. Run in main.go
func main() {

	config, err := cli.LoadConfig("config.yaml")
	if err != nil {
		log.Fatal(err)
	}

	app, err := cli.New(config).Using(&Program{})
	if err != nil {
		log.Fatal(err)
	}

	app.Run()
}

Ideas for future

  • Use of arrow keys to scroll up and down previous inputs/outputs
  • Allow split config file into many config files, with hierarchical structure

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is the CLI application

func New

func New(config *Config) (app *App)

New creates a new App from the given config

func (*App) Run

func (app *App) Run()

Run runs the CLI

func (*App) Using

func (app *App) Using(program interface{}) (*App, error)

Using gets the App to use the methods from program

type Argument

type Argument struct {

	// When the argument is parsed from the input, the argument
	// text must match the label in order to invoke the command.
	Label string `yaml:"label"`

	// If applicable, any options that are required for the
	// command.
	Options []Option `yaml:"options"`

	// The function performed when this command is invoked.
	// The options will be passed to this function as Flags.
	ExecFunc string `yaml:"execFunc"`

	// (optional) help message for this argument.
	HelpMsg string `yaml:"help"`
	// contains filtered or unexported fields
}

Argument is the word, words or set of consecutive characters, that follow the command. If the command has no arguments, Label should be set as an empty string.

type Command

type Command struct {

	// When the command is parsed from the input, the command
	// text must match the label in order to invoke the command.
	Label string `yaml:"label"`

	// Any arguments that are required for the command.
	// For a command without any arguments, the argument label
	// should be an empty string.
	Arguments []Argument `yaml:"arguments"`
	// contains filtered or unexported fields
}

Command is the first word or set of consecutive characters.

type Config

type Config struct {

	// The output to the CLI to prompt input from the user.
	Prompt string `yaml:"prompt"`

	// The commands that are configured.
	Commands []Command `yaml:"commands"`

	// The function performed when the CLI is intialised.
	// The output from this function will appear before
	// any other output in the CLI.
	InitFunc string `yaml:"initFunc"`

	// The function performed when the CLI is terminated.
	// This function's output will be the last output to
	// appear in the CLI before it closes.
	ExitFunc string `yaml:"exitFunc"`

	// The CLI command used to trigger an exit.
	ExitCmd string `yaml:"exitCmd"`

	// The CLI command used to print a help message.
	HelpCmd string `yaml:"helpCmd"`
	// contains filtered or unexported fields
}

Config represents the all the config options required to run the CLI.

func LoadConfig

func LoadConfig(filename string) (config *Config, err error)

LoadConfig extracts the config from the given yaml file and unmarshals it into a Config. Any errors reading the file or unmarshaling the file will be returned.

type Flags

type Flags struct {
	// contains filtered or unexported fields
}

Flags stores data for the options and variables for a command.

func (Flags) Exists

func (flags Flags) Exists(label string) bool

Exists returns whether the given label exists in the Flags.

func (Flags) GetVar

func (flags Flags) GetVar(label string) (variable string, exists bool)

GetVar returns the variable set for the option with the given label. If the option has not been set, doesn't have a variable or doesn't exist in Flags, ("", false) will be returned instead.

func (Flags) IsSet

func (flags Flags) IsSet(label string) bool

IsSet returns whether the given label has been set in the command.

type Option

type Option struct {

	// Name of the variable that will be used as a key for
	// a flag mapping when used in a action function.
	Label string `yaml:"label"`

	// Short name, single dash (–) followed by a signle
	// character.
	Short string `yaml:"short"`

	// Long name, double dash (--) followed by a
	// descriptive name.
	Long string `yaml:"long"`

	// (optional) if this option requires a variable,
	// it should be defined here.
	Variable *Variable `yaml:"variable"`

	// (optional) help message for this option.
	HelpMsg string `yaml:"help"`
}

Option is a character, set of consecutive characters, or a word that follows the command and any arguments. Options are preceded by an dash (–).

type Variable

type Variable struct {

	// Name of the variable that will be used as a key for
	// a variable mapping when used in a action function.
	// It will also be used as the variable placeholder in
	// the help messages.
	Label string `yaml:"label"`

	// Whether the option for this variable is required
	// for the command.
	Required bool `yaml:"required"`

	// (optional) The default value for the variable
	Default string `yaml:"default"`
}

Variable is any set of consecutive characters or word that follows an option.

Jump to

Keyboard shortcuts

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