Archive

Posts Tagged ‘Interactive CLI applications’

Building Command Line Apps with urfave/cli in Go

August 25, 2023 Leave a comment
Command line applications play a crucial role in software development, allowing developers to interact with their programs through terminal commands. One of the popular packages to build efficient and user-friendly command line apps in Go is urfave/cli.

What is urfave/cli?

urfave/cli is a simple, fast, and enjoyable package designed for creating command line applications in Go. It provides a robust framework for creating commands, flags, and subcommands, making it an excellent choice for building user-friendly command line interfaces.

Getting Started

To begin building your command line app using urfave/cli, follow these steps:

1. Installation: Install the urfave/cli package by running the following command:

go get github.com/urfave/cli/v2

2. Create a Basic Example:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/urfave/cli/v2"
)

func main() {
	app := cli.NewApp()
	app.Name = "decision-engine"

	app.Commands = []*cli.Command{
		{
			Name:   "zap",
			Action: zapLogger,
			Usage:  "Demostrate Zap logger",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:  "app_config",
					Usage: "Specify your app configuration in a TOML format",
				},
			},
		},
		{
			Name:   "config",
			Action: runConfigDemo,
			Usage:  "Demostrate Tomal, yaml and json",
			Subcommands: []*cli.Command{
				{
					Name:  "Tomal",
					Usage: "Tomal template",
					Action: func(cCtx *cli.Context) error {
						fmt.Println("new Tomal template: ", cCtx.Args().First())
						return nil
					},
				},
				{
					Name:  "json",
					Usage: "Json template",
					Action: func(cCtx *cli.Context) error {
						fmt.Println("removed Json template: ", cCtx.Args().First())
						return nil
					},
				},
				{
					Name:  "yaml",
					Usage: "Yaml template",
					Action: func(cCtx *cli.Context) error {
						fmt.Println("removed Yaml template: ", cCtx.Args().First())
						return nil
					},
				},
			},
		},
		{
			Name:   "redis",
			Action: redisDemo,
			Usage:  "Demostrate Resis",
			Flags: []cli.Flag{
				&cli.StringFlag{
					Name:  "app_config",
					Usage: "Specify your app configuration in a TOML format",
				},
			},
		},
	}
	if err := app.Run(os.Args); err != nil {
		log.Fatalf("app.Run() failed: %v", err)
	}
}

func zapLogger(cliCtx *cli.Context) error {
	fmt.Println("new task template: ")
	return nil
}

func runConfigDemo(cliCtx *cli.Context) error {
	fmt.Println("new task template: ")
	return nil
}
func redisDemo(cliCtx *cli.Context) error {
	fmt.Println("new task template: ")
	return nil
}

3. Build and Run: Save the code above to a file named main.go, and then build and run your application using these commands:

go build main.go //Build 
.\main.exe config //Run command
.\main.exe config yaml //Run sub command

Advanced Features

urfave/cli offers powerful features for handling complex command line applications:

  • Subcommands: Create hierarchical command structures with subcommands for organized functionality.
  • Flags: Define flags for your commands to accept input from the user. Flags can be of various types, including strings, integers, and booleans.
  • Aliases: Assign aliases to your commands, allowing users to use shorter or alternative names to execute them.
  • Context: Utilize the cli.Context object to access command line arguments and flags within your command’s functions.

Conclusion

In conclusion, urfave/cli is a powerful and versatile package for building command line apps in Go. It