cmndr

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

cmndr

Go Reference

A simple way for creating command-line applications, in Go.

Documentation

Overview

Package cmndr provides a lightweight means of constructing command-line interfaces for programs.

The goal of this package, is to provide something that is roughly comparable to github.com/spf13/cobra, but to provides something that is more lightweight, and relies on the Go standard library, as much as possible.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cmd

type Cmd struct {
	// The name of the command.
	Name string

	// A brief, single line description of the command.
	Description string

	// A *flag.FlagSet for registering command-line flags for the command.
	Flags *flag.FlagSet

	// Will be nil unless subcommands are registered with the AddCmd()
	// method.
	Commands map[string]*Cmd

	// The function to run.
	Run RunFunc
}

Cmd defines the structure of a command that can be run.

func New

func New(name string, run RunFunc) *Cmd

New is a convenience function for creating and returning a new *Cmd.

New will automatically add a "help" subcommand that, when called with no arguments, will print the help message for its parent command. If any arguments are provided to the "help" subcommand, only the first argument will be consulted, and it will print the help message for the specified subcommand.

Note, that the following two command-line calls are effectively the same:

$ my-command help <subcommand>
$ my-command <subcommand> help

func (*Cmd) AddCmd

func (c *Cmd) AddCmd(cmd *Cmd)

AddCmd registers a subcommand.

AddCmd will panic if the given cmd's Name field is an empty string. If there is a subcommand already registered with the same name, it will be replaced.

func (*Cmd) Exec

func (c *Cmd) Exec()

Exec parses the arguments provided on the command line. This is the method that should be called from the outer-most command (e.g. the "root" command).

It is essentially a short-hand invocation of

c.ExecArgs(os.Args[1:])

func (*Cmd) ExecArgs

func (c *Cmd) ExecArgs(args []string)

ExecArgs executes c.Run with the given arguments. If c.Run == nil, and no subcommand was provided as a positional argument, this method will print a usage message, and exit.

To customize the usage message that is printed, set c.Flags.Usage (refer to the documentation for flag.FlagSet).

type RunFunc

type RunFunc func(cmd *Cmd, args []string) error

RunFunc defines the arity and return signatures of a function that a Cmd will run.

Jump to

Keyboard shortcuts

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