cmdos

package module
v0.0.0-...-68c1a46 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MPL-2.0 Imports: 5 Imported by: 0

README

cmdos

A flag and subcommand library

Go Documentation

package main

import (
	"fmt"
	"os"

	"codeberg.org/roble/cmdos"
)

type rootCmd struct{}

func (*rootCmd) Details() cmdos.Details {
	return cmdos.Details{
		Name:        "mycmd",
		Description: "my command",
	}
}

func (*rootCmd) Flags() []cmdos.Flag {
	return []cmdos.Flag{
        cmdos.FlagHelp,
		{
			Short:       'a',
			Long:        "addr",
			Description: "listen address",
			Value:         cmdos.ValueOptional(":8082"),
		},
		{
			Short:       'o',
			Long:        "output",
			Description: "file location to save",
			Value:       cmdos.ValueRequired(),
		},
	}
}

func (c *rootCmd) Run(vs *cmdos.Values) error {
	var opt struct {
		Help   bool   `cmdos:"help"`
		Addr   string `cmdos:"addr"`
		Output string
	}
	if err := vs.Decode(&opt); err != nil {
		return err
	}

	if opt.Help {
		return cmdos.WriteHelp(os.Stdout, c)
	}

	fmt.Println("addr:", opt.Addr)

	return nil
}

func main() {
	if err := cmdos.Run(os.Args, new(rootCmd)); err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}
$ cmdostest -h
my command

usage:
  mycmd [options]

options:
  -h, --help  display help and exit
  -a, --addr  listen address
  -o, --output  file location to save

Documentation

Index

Constants

View Source
const (
	ValueModeNone int = iota
	ValueModeRequired
	ValueModeDefault
	ValueModeOptional
)

Variables

View Source
var (
	FlagHelp = Flag{
		Short:       'h',
		Long:        "help",
		Description: "Print help and exit",
		SetID:       "help",
	}

	FlagVersion = Flag{
		Short:       'v',
		Long:        "version",
		Description: "Print version and exit",
		SetID:       "version",
	}
)

Functions

func Run

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

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 {
	Details() Details
	Run(*Values) error
}

type Details

type Details struct {
	Name        string
	Usages      []string
	Description string
	Summary     string
}

type Flag

type Flag struct {
	Short       rune
	Long        string
	Description string

	Value FlagValue

	SetID string
}

type FlagValue

type FlagValue struct {
	Mode int

	Default  string
	Optional string
}

func ValueDefault

func ValueDefault(def string) FlagValue

func ValueOptional

func ValueOptional(def, opt string) FlagValue

func ValueRequired

func ValueRequired() FlagValue

type Flags

type Flags interface {
	Flags() []Flag
}

type Subcommands

type Subcommands interface {
	Subcommands() []Command
}

type Values

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

func (*Values) Args

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

func (*Values) Decode

func (vs *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"`

Jump to

Keyboard shortcuts

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