cli

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

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

Go to latest
Published: Nov 30, 2024 License: MIT Imports: 8 Imported by: 0

README

Simple command line interface package.

Example

// List is a `Command`.
type List {
	Pattern string `descr:"pattern to match"` // filled with the value for -pattern
	Kind    cli.Enum `descr:"kind to select" enum:"kind1 kind2"` // space separated enum values (default is the first one)

	hidden bool // non exported fields are ignored
}

func (l List) Name() string {
	return "list"
}

func (l List) Description() string {
	return "list files for the given pattern"
}

func (l List) Run(args []string) error {
	files := []string{"foo", "bar"}
	for _, file := range files {
		if strings.Contain(file, l.Pattern) {
			fmt.Println(file)
		}
	}
	return nil
}

cli.Run("mycli", "a cli example", &List{})

Documentation

Overview

Package cli helps in building command based command line tools.

e.g. myexe do_something -flag1 arg1 -flag2 arg2 arg3

Commands are defined as types implementing the `Command` interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(name, descr string, commands ...Command)

Run the command from the cli arguments. The commands must be pointers.

Types

type Command

type Command interface {
	Name() string
	Description() string
	Run(args []string) error
}

type Enum

type Enum []string

Enum restricts a value to one of the slice's values.

Jump to

Keyboard shortcuts

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