subcmd

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: MIT Imports: 6 Imported by: 5

README

koron-go/subcmd

PkgGoDev Actions/Go Go Report Card

koron-go/subcmd is very easy and very simple sub-commander library. It focuses solely on providing a hierarchical subcommand mechanism. It does not provide any flags nor options.

Getting Started

Install or update:

$ go install github.com/koron-go/subcmd@latest

Documentation

Overview

Package subcmd provides sub commander.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Names

func Names(ctx context.Context) []string

Names retrives names layer of current sub command.

func Run

func Run(r Runner, args ...string) error

Run runs a Runner with ctx and args.

Types

type Command

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

Command repreesents a sub-command, and implements Runner interface.

func DefineCommand

func DefineCommand(name, desc string, fn CommandFunc) Command

DefineCommand defines a Command with name, desc, and function.

func (Command) Desc

func (c Command) Desc() string

Desc returns description of the command.

func (Command) Name

func (c Command) Name() string

Name returns name of the command.

func (Command) Run

func (c Command) Run(ctx context.Context, args []string) error

Run executes sub-command. It will invoke CommandFunc which passed to DefineCommand.

type CommandFunc

type CommandFunc func(ctx context.Context, args []string) error

CommandFunc is handler of sub-command, and an entry point.

type Runner

type Runner interface {
	// Name returns name of runner.
	Name() string

	// Desc returns description of runner.
	Desc() string

	// Run runs runner with context and arguments.
	Run(ctx context.Context, args []string) error
}

Runner defines a base interface for Command and Set. Runner interface is defined for use only with DefineSet function.

type Set

type Set struct {
	Runners []Runner
	// contains filtered or unexported fields
}

Set provides set of Commands or nested Sets.

func DefineRootSet

func DefineRootSet(runners ...Runner) Set

DefineRootSet defines a set of Runners which used as root of Set (maybe passed to Run).

func DefineSet

func DefineSet(name, desc string, runners ...Runner) Set

DefineSet defines a set of Runners with name, and desc.

func (Set) Desc

func (s Set) Desc() string

Desc returns description of Set.

func (Set) Name

func (s Set) Name() string

Name returns name of Set.

func (Set) Run

func (s Set) Run(ctx context.Context, args []string) error

Jump to

Keyboard shortcuts

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