ezcli

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 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.)

Example

Simple example for Ezcli.

package main

import (
    "fmt"

    "github.com/5elenay/ezcli"
)

func main() {
    handler := ezcli.CreateHandler() // Create handle function also gives built-in help command. So you dont 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.CommandData) { // 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

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            []string
	CommandData       *CommandData
	Execute           func(data *CommandData)
}

Command struct.

func (*Command) FindOption

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

Find an option from command.

type CommandData

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

Command Data struct (arguments, options...)

type CommandHandler

type CommandHandler struct {
	Commands []*Command
}

Command Handler struct.

func CreateHandler

func CreateHandler() *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) 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.

type CommandOption

type CommandOption struct {
	Name, Description string
}

Command option (flag) struct.

Jump to

Keyboard shortcuts

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