easycmd

package module
v0.0.0-...-99838b3 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 8 Imported by: 3

README

easycmd

easycmd is a comprehensive command-line interface (CLI) toolkit simplifying the development of structured command-line applications. Relying on urfave/cli, easycmd streamlines CLI creation by abstracting away boilerplate code and providing easy-to-use scaffolding facilities.

Whether you are crafting simple tools or complex multi-command apps, easycmd enables you to focus on core functionality.

Features

  • Simplifies the definition of command hierarchies.
  • Supports aliases and custom flag handling.
  • Facilitates middleware usage for common setup functions.
  • Extensible through custom builders for individual command actions.
  • Well-defined error handling and custom output formatting.

Installation

To install easycmd, simply run:

bash go get github.com/bagaking/easycmd

Quick Start

Below is a concise example displaying how you can set up a CLI application that manages inventory with commands to sell different items:

package main

import (
    "github.com/bagaking/easycmd"
    "github.com/urfave/cli/v2"
)

// Define actions for different commands
func SellGem(c *cli.Context) error {
// Your code to sell a gem.
return nil
}

func SellWood(c *cli.Context) error {
// Your code to sell wood.
return nil
}

func main() {
    app := easycmd.New("inventory").
        Child("sell").Set.
        Alias("s").Usage("Commands to sell items").End.
        Base().Child("gem").Set.
        Alias("g").Usage("Sell some gems").End.Action(SellGem).
        Base().Child("wood").Set.
        Alias("w").Usage("Sell some wood").End.Action(SellWood).Flags(flagsWood).
		Base().Child("idle").Handler(mainIdleHandler).
		RunBaseAsApp(); 

    if err := app.RunBaseAsApp(); err != nil {
        panic(err)
    }
}

In this example, we created a CLI with an inventory command followed by sell command having subcommands gem and wood each with their specific actions and usages.

Documentation

For more detailed information, visit urfave/cli documentation as easycmd is a complementary wrapper to simplify using urfave/cli functionalities.

Contributions

Contributions are welcome! Feel free to submit pull requests to help improve easycmd or create an issue for any bugs or enhancements.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFlagAlreadyExist = errors.New("flag already exists")

Functions

func MergeFlags

func MergeFlags(flags1, flags2 []cli.Flag, ignoreError ...bool) ([]cli.Flag, error)

SetCustomOptions can merge two list of cli.Flags

func Print

func Print(cliCtx *cli.Context, msgLst ...interface{}) (err error)

func Println

func Println(cliCtx *cli.Context, msgLst ...interface{}) (err error)

func SetCustomOptions

func SetCustomOptions(opt CustomOption)

SetCustomOptions can set some easycmd-layer options

func ToApp

func ToApp(cmd *cli.Command) (*cli.App, error)

ToApp creates an app from a single command when flags conflict, error will be thrown and the procedure will be stock to ignore the error, you can stash the flags of the command, and the call MergeFlags by yourself

Types

type Builder

type Builder struct {
	Set *Setter
	// contains filtered or unexported fields
}

func New

func New(cmd string) *Builder

New method can create a New Builder by cmd name

func (*Builder) Action

func (cb *Builder) Action(action cli.ActionFunc) *Builder

Action sets the action of current Builder

func (*Builder) Base

func (cb *Builder) Base() *Builder

Base returns base Builder

func (*Builder) BuildBase

func (cb *Builder) BuildBase() *cli.Command

BuildBase returns root Builder's cmd

func (*Builder) BuildCur

func (cb *Builder) BuildCur() *cli.Command

BuildCur returns current Builder's cmd

func (*Builder) Child

func (cb *Builder) Child(cmd string) *Builder

Child creates a child of current Builder by a given cmd name

func (*Builder) Cur

func (cb *Builder) Cur() *Builder

Cur returns current Builder

func (*Builder) Flags

func (cb *Builder) Flags(flags ...cli.Flag) *Builder

Flags override all flags of current Builder

func (*Builder) Handler

func (cb *Builder) Handler(handler ICliHandler, mws ...Middleware) *Builder

Handler method sets the cmd by a ICliHandler

func (*Builder) RunBaseAsApp

func (cb *Builder) RunBaseAsApp() error

RunBaseAsApp runs the command as a single app

func (*Builder) RunBaseAsAppWithCtx

func (cb *Builder) RunBaseAsAppWithCtx(ctx context.Context) error

RunBaseAsAppWithCtx runs the command as a single app

func (*Builder) SubCmd

func (cb *Builder) SubCmd(child *cli.Command) *Builder

SubCmd creates a child of current Builder by a given cli.Command

type CustomOption

type CustomOption struct {
	ExitAfterPrintHelpMsg bool
}

type ICliHandler

type ICliHandler interface {
	Flags() []cli.Flag
	Parse(c *cli.Context) (ICliHandler, error)
	Handle(c *cli.Context) error
}

type Middleware

type Middleware func(cli.ActionFunc) cli.ActionFunc

type Setter

type Setter struct {
	End *Builder
}

func (*Setter) Alias

func (set *Setter) Alias(a string) *Setter

func (*Setter) Custom

func (set *Setter) Custom(setter func(*cli.Command)) *Setter

func (*Setter) Usage

func (set *Setter) Usage(usage string) *Setter

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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