goldcmd

package module
v0.0.0-...-e9d6cd3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2021 License: MIT Imports: 5 Imported by: 0

README

README

This is a small library for implementing command line utilities. This was originally implemented as part of the larger geode project.

Quick Start

This script can be found in goldcmd/examples/simpleecho/main.go. It implements a simple version of the UNIX echo command.

package main

import (
	"fmt"

	"github.com/GeorgeSaussy/goldcmd"
)

/// Get a subcommand handler to echo a string.
func handler() *goldcmd.SubcommandHandler {
	ret, err := goldcmd.NewSubcommandHandler("echo", "Echo a string.")
	if err != nil {
		panic(err)
	}
	if err = ret.AddStrArg([]string{"s"}, "A string to echo"); err != nil {
		panic(err)
	}
	ret.Example("Echo a string", "dumbecho echo -s=example_string", "example_string")
	ret.Example("Echo a string", "dumbecho echo --s \"example string\"", "example string")
	ret.Handle(func(handler *goldcmd.SubcommandHandler) {
		if a, err := handler.GetStr("s"); err != nil {
			fmt.Printf("No string found!\n")
		} else {
			fmt.Printf("%s\n", a)
		}
	})
	return ret
}

func main() {
	cli := goldcmd.NewCli("latest", "A dumb echo app.")
	cli.HandleSubcommand(*handler())
	cli.Run()
}

If you compile this script to ./a.out, it could then be called like this:

  • Example: No arguments
% ./a.out 
A simple echo command line tool.

SUBCOMMANDS
  echo  Echo a string.
  help  this help message

Get help with a subcommand with by passing it as an argument to the 'help' subcommand.

The same output would be printed for ./a.out -h and ./a.out --help.

  • Example: Help with the echo subcommand
% ./a.out help echo
Echo a string.

ARGUMENTS
 --s, --text    A string to echo


EXAMPLES
$ # Echo a string
$ simpleecho echo -s=example_string
example_string

$ # Echo a string
$ simpleecho echo --s "example string"
example string

Note, everything after the first line is the output of the command.

  • Example: Echoing things
% ./a.out echo -s "Hello, World!"
Hello, World!
% ./a.out echo --text="Bonsoir, Elliot."
Bonsoir, Elliot.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cli

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

The CLI "server" object

func NewCli

func NewCli(version string, doc string) Cli

Create a new Cli instance.

This Cli will do nothing when run is called unless it is mutated. The argument `version` should be a string describing the version of the CLI e.g. "2.1.3", "beta", "latest", &c The argument `doc` should contain brief documentation of the command.

func (*Cli) HandleSubcommand

func (cli *Cli) HandleSubcommand(subcmd *SubcommandHandler)

Add a subcommand to the CLI instance.

func (*Cli) Run

func (cli *Cli) Run()

Either print help, or run a subcommand.

type SubcommandHandler

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

Handle a CLI subcommand.

For each row in ints, each element in the row can be used internally to refer to the value given at the command line. For example, if ints is [["a", "apple"]], then GetInt("a") should equal GetInt("apple") no matter what the user wrote at the command line.

func NewSubcommandHandler

func NewSubcommandHandler(name string, doc string) (*SubcommandHandler, error)

Create a new SubcommandHandler instance. An error may occur if the subcommand labels are not valid e.g. if the one of the labels had a leading '-'.

func (*SubcommandHandler) AddBoolArg

func (h *SubcommandHandler) AddBoolArg(aliases []string, doc string) error

Add a boolean argument to the subcommand. Warning, the subcommand will always fail if the argument is not set. If one of the aliases is used by another argument or parameter, the function will return an error and handler will not be mutated.

func (*SubcommandHandler) AddBoolParamWithDefault

func (h *SubcommandHandler) AddBoolParamWithDefault(aliases []string, doc string, deflt bool) error

Add a boolean parameter to the subcommand with a default value.

func (*SubcommandHandler) AddFloatArg

func (h *SubcommandHandler) AddFloatArg(aliases []string, doc string) error

Add a float argument to the subcommand. Warning, the subcommand will always fail if the argument is not set. If one of the aliases is used by another argument or parameter, the function will return an error and handler will not be mutated.

func (*SubcommandHandler) AddFloatParamWithDefault

func (h *SubcommandHandler) AddFloatParamWithDefault(aliases []string, doc string, deflt float64) error

Add a float parameter to the subcommand with a default value.

func (*SubcommandHandler) AddIntArg

func (h *SubcommandHandler) AddIntArg(aliases []string, doc string) error

Add an integer argument to the subcommand. Warning, the subcommand will always fail if the argument is not set. If one of the aliases is used by another argument or parameter, the function will return an error and handler will not be mutated.

func (*SubcommandHandler) AddIntParamWithDefault

func (h *SubcommandHandler) AddIntParamWithDefault(aliases []string, doc string, deflt int) error

Add an integer parameter to the subcommand with a default value.

func (*SubcommandHandler) AddStrArg

func (h *SubcommandHandler) AddStrArg(aliases []string, doc string) error

Add a string argument to the subcommand. Warning, the subcommand will always fail if the argument is not set. If one of the aliases is used by another argument or parameter, the function will return an error and handler will not be mutated.

func (*SubcommandHandler) AddStrParamWithDefault

func (h *SubcommandHandler) AddStrParamWithDefault(aliases []string, doc string, deflt string) error

Add string parameter to the subcommand with a default value.

func (*SubcommandHandler) Example

func (h *SubcommandHandler) Example(doc string, cmd string, out string)

Add an example to a SubcommandHandler instance. The argument `doc` is documentation for the example. The argument `cmd` is the text of the command with command line arguments. The argument `out` is the plausible output for the command

func (*SubcommandHandler) GetBool

func (h *SubcommandHandler) GetBool(key string) (val bool, err error)

Get a string argument or parameter from the command line. Warning: This function will fail if the command line arguments have not already been parsed.

func (*SubcommandHandler) GetFloat

func (h *SubcommandHandler) GetFloat(key string) (float64, error)

Get a string argument or parameter from the command line. Warning: This function will fail if the command line arguments have not already been parsed.

func (*SubcommandHandler) GetInt

func (h *SubcommandHandler) GetInt(key string) (int, error)

Get an integer argument or parameter from the command line. Warning: This function will fail if the command line arguments have not already been parsed.

func (*SubcommandHandler) GetStr

func (h *SubcommandHandler) GetStr(key string) (string, error)

Get a string argument or parameter from the command line. Warning: This function will fail if the command line arguments have not already been parsed.

func (*SubcommandHandler) Handle

func (h *SubcommandHandler) Handle(f func(insub *SubcommandHandler))

Set the handler function for a SubcommandHandler instance. # Arguments - sub: a SubcommandHandler instance to be mutated - f: the handler function

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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