DEV Community

5elenay
5elenay

Posted on • Updated on

Ezcli - Minimal Package for Creating CLI apps.

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.)

Source Code

Click here for source code.

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.Command) { // The function will run.
            fmt.Println("Hello Command!")
        },
    })

    handler.Handle() // Handle commands.
}

Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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

Hello Command!
Enter fullscreen mode Exit fullscreen mode

Congrats, you created your first app with Ezcli!

Contribution

Found Bug? Issue? Please report at here. Or if you want to improve ezcli:

  • Fork the project from GitHub.
  • Make commits.
  • Open a pull-request and wait.

Top comments (0)