cmdos – codeberg.org/roble/cmdos Index | Files

package cmdos

import "codeberg.org/roble/cmdos"

package cmdos is a library for creating CLIs.

Index

Functions

func ArgDefault

func ArgDefault(val string) flagArg

ArgDefault can be used when you want to give a flag a default value.

func ArgRequire

func ArgRequire() flagArg

ArgRequire can be used to force the user to set a flag on a command.

func Run

func Run(c Command, args []string) error

Run is the main entry point for a cmdos CLI.

func WriteHelp

func WriteHelp(w io.Writer, c Command) error

WriteHelp writes the help info from a Command to a io.Writer.

Types

type Command

type Command interface {
	Info() Info
	Flags() []Flag
	Run(*Values) error
}

Command is an interface for creating a CLI command.

type Flag

type Flag struct {
	Short       rune
	Long        string
	Description string

	Arg flagArg
}

Flag holds info to define a flag on a command.

type HelpTemplate

type HelpTemplate interface {
	HelpTemplate() string
}

HelpTemplate is an optional interface. Implement this method to use a custom template to generate help text.

type Info

type Info struct {
	Name   string
	Usages []string

	Summary     string
	Description string
}

Info contains info about the command we're defining.

type Subcommands

type Subcommands interface {
	Subcommands() []Command
}

Subcommands is an optional interface. Implement this method to register subcommands on a Command.

type Values

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

Values gives you access to flag values.

func (*Values) Args

func (v *Values) Args() []string

Args returns the arguments passed to a command.

func (*Values) Count

func (v *Values) Count(name string) (int, error)

Count returns the amount of times a flag was set.

func (*Values) Decode

func (v *Values) Decode(dst any) (err error)

Decode unmarshals flag values into a dst struct. The dst struct should tag its fields. The tag value should match the name of the flag.

Help bool   `cmdos:"help"`
Addr string `cmdos:"addr"`

func (*Values) Has

func (v *Values) Has(name string) (bool, error)

Has returns whether a flag was set or not.

Source Files

command.go flag.go help.go run.go values.go

Version
v0.0.0-20250511070247-987e74490f10 (latest)
Published
May 11, 2025
Platform
linux/amd64
Imports
5 packages
Last checked
2 months ago

Tools for package owners.