ezcli

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: MIT Imports: 5 Imported by: 1

README

Ezcli

Create a CLI tool in <10 second without any performance decrease.

Size License Stars Release

What is Ezcli?

Ezcli is a Go package for building CLI tools easily.

Installation

  • Initialize your project with go mod init <name>
  • Get Ezcli with go get github.com/5elenay/ezcli

API Reference

Click here for API reference. (pkg.go.dev sometime does not update package version, so use https://pkg.go.dev/github.com/5elenay/ezcli@<latest version> for get the API reference for latest version.)

Documentations

Example

Simple example for Ezcli.

package main

import (
    "fmt"

    "github.com/5elenay/ezcli"
)

func main() {
    handler := ezcli.NewApp("test") // Create handle function also gives built-in help command. So you don't need to write a help command yourself.

    // Adding a new command.
    handler.AddCommand(&ezcli.Command{
        Name:        "hello", // Command name.
        Description: "Say hello world!", // Command description.
        Options: []*ezcli.CommandOption{ // Command options (example: -force, -confirm etc...).
            {
                Name:        "test", // Option name.
                Description: "A test command", // Option description.
            },
        },
        Execute: func(c *ezcli.Command) { // The function will run.
            fmt.Println("Hello Command!")
        },
    })

    handler.Handle() // Handle commands.
}

Now lets compile our app with go build .

Time to test our app! Run the compiled app with ./<name> help

You should get the following result:

List of all commands. For more information, add a command name parameter to command.
  help | Built-in help command for application.
  hello | Say hello world!

Lets test our hello command with ./<name> hello:

Hello Command!

Congrats, you created your first app with Ezcli!

License

This project is licensed under MIT license.

Documentation

Overview

Private functions that helps Ezcli.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Name, Description string
	Options           []*CommandOption
	Usages, Aliases   []string
	CommandData       *CommandData
	Execute           func(c *Command)
	SubCommands       []*SubCommand
}

Command struct.

func (*Command) FindOption

func (c *Command) FindOption(name string, fn func(o *CommandOption))

Find an option template from command.

func (Command) FindSubcommand added in v1.2.0

func (c Command) FindSubcommand(name string, fn func(sc *SubCommand)) error

Find Sub-command

type CommandData

type CommandData struct {
	Arguments []string
	Options   []*CommandOption
}

Command Data struct (arguments, options...)

func (*CommandData) FindOption

func (c *CommandData) FindOption(name string, fn func(o *CommandOption))

Find an option from command data.

type CommandHandler

type CommandHandler struct {
	Name                string
	Commands            []*Command
	CommandNotFoundFunc func()
}

Command Handler struct.

func NewApp added in v1.2.0

func NewApp(appName string) *CommandHandler

Create a command handler with built-in help command.

func (*CommandHandler) AddCommand

func (ch *CommandHandler) AddCommand(c *Command)

Add a new command to the handler.

func (*CommandHandler) AddCommands

func (ch *CommandHandler) AddCommands(cs []*Command)

Add more command to the handler.

func (*CommandHandler) FindCommand

func (ch *CommandHandler) FindCommand(name string, fn func(c *Command) error) error

Find a command from handler.

func (*CommandHandler) Handle

func (ch *CommandHandler) Handle()

Handle commands.

func (*CommandHandler) SetName added in v1.2.0

func (ch *CommandHandler) SetName(name string)

Set App Name

func (*CommandHandler) SetNotFoundFunction

func (ch *CommandHandler) SetNotFoundFunction(fn func())

Set error function that will run when command not found.

type CommandOption

type CommandOption struct {
	Name, Description, Value string
	Aliases                  []string
}

Command option (flag) struct.

type Question added in v1.2.0

type Question struct {
	Input, Answer string
}

Question struct.

func (*Question) Ask added in v1.2.0

func (q *Question) Ask() error

Ask the question.

type SubCommand added in v1.2.0

type SubCommand struct {
	Name, Description string
	Usages            []string
	CommandData       *CommandData
	Execute           func(sc *SubCommand)
}

Sub-Command struct

Jump to

Keyboard shortcuts

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