Simple and complete API for building command line applications in Go

Build status Coverage GoReportCard API documentation

Simple and complete API for building command line applications in Go

Module cli provides a simple, fast and complete API for building command line applications in Go. In contrast to other libraries the emphasis is put on the definition and validation of positional arguments, handling of options from all levels in a single block as well as a minimalistic set of dependencies.

The core of the module is the command, option and argument parsing logic. After a successful parsing the command action is evaluated passing a slice of (validated) positional arguments and a map of (validated) options. No more no less.

Definition

co := cli.NewCommand("checkout", "checkout a branch or revision").
  WithShortcut("co").
  WithArg(cli.NewArg("revision", "branch or revision to checkout")).
  WithOption(cli.NewOption("branch", "Create branch if missing").WithChar('b').WithType(cli.TypeBool)).
  WithOption(cli.NewOption("upstream", "Set upstream for the branch").WithChar('u').WithType(cli.TypeBool)).
  WithAction(func(args []string, options map[string]string) int {
    // do something
    return 0
  })

add := cli.NewCommand("add", "add a remote").
  WithArg(cli.NewArg("remote", "remote to add"))

rmt := cli.NewCommand("remote", "Work with git remotes").
  WithCommand(add)

app := cli.New("git tool").
  WithOption(cli.NewOption("verbose", "Verbose execution").WithChar('v').WithType(cli.TypeBool)).
  WithCommand(co).
  WithCommand(rmt)
  // no action attached, just print usage when executed

os.Exit(app.Run(os.Args, os.Stdout))

Execution

Given the above definition for a git client, e.g. gitc, running gitc with no arguments or with -h will produce the following output (the exit code will be 1 in the former case, because the action is missing, and 0 in the latter, because help was explicitly requested):

gitc [--verbose]

Description:
    git tool

Options:
    -v, --verbose   Verbose execution

Sub-commands:
    git checkout    checkout a branch or revision
    git remote      Work with git remotes

Running gitc with arguments matching e.g. the checkout definition, gitc co -vbu dev or gitc checkout -v --branch -u dev will execute the command as expected. Running into a parsing error, e.g. by providing an unknown option gitc co -f dev, will output a parsing error and a short usage string:

fatal: unknown flag -f
usage: gitc checkout [--verbose] [--branch] [--upstream] <revision>

License and copyright

Copyright (c) 2017. Oleg Sklyar and teris.io. MIT license applies. All rights reserved.
Similar Resources

Watcher - A simple command line app to watch files in a directory for changes and run a command when files change!

Watcher - Develop your programs easily Watcher watches all the files present in the directory it is run from of the directory that is specified while

Mar 27, 2022

Interactive prompt for command-line applications

Interactive prompt for command-line applications

promptui Interactive prompt for command-line applications. We built Promptui because we wanted to make it easy and fun to explore cloud services with

Jan 8, 2023

MyApps is a universal command line tool for managing manually installed applications.

MyApps is a universal command line tool for managing manually installed applications.

MyApps MyApps is a universal command line tool for managing manually installed applications. Disclaimer I wrote this tool over two long nights while p

Jul 15, 2022

A versatile library for building CLI applications in Go

mow.cli Package cli provides a framework to build command line applications in Go with most of the burden of arguments parsing and validation placed o

Dec 28, 2022

Several Examples for building docker containers for your Go applications

go-docker Several Examples for building docker containers for your Go applicatio

Dec 27, 2021

Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.

asciigraph Go package to make lightweight ASCII line graphs ╭┈╯. Installation go get github.com/guptarohit/asciigraph Usage Basic graph package main

Jan 8, 2023

Simple command line tool helper to integrate with hashicorp vault & github api

Overview CI/CD Toolkit is small command line tool helper to integrate with vault secret kv management & github api We can use simple command to genera

Apr 2, 2022

A go1.18 wrapper to provide simple generics based API for defining command line flags.

gflag A go1.18 wrapper to provide simple generics based API for defining command line flags. Example package main import ( "flag" "fmt" "time" "

Dec 20, 2021

git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.

git-xargs is a command-line tool (CLI) for making updates across multiple Github repositories with a single command.

Table of contents Introduction Reference Contributing Introduction Overview git-xargs is a command-line tool (CLI) for making updates across multiple

Dec 31, 2022
Comments
  • Long command name

    Long command name

    On Windows, the program name seems to always be the absolute path to the executable like so:

    image

    Perhaps this is intentional, but it makes the help message unnecessarily long. Could the program name be just the filename of the command rather than the path? Or perhaps could it be manually overridden? Perhaps like this:

    app := cli.New("myapp", "here is the description...")
    
  • Support `WithDefault` method

    Support `WithDefault` method

    This issue is a feature request

    Both Arg and Option don't support default value now. In order to do that, I can write something like below:

    installCommand := cli.NewCommand("install", "Install a package").
    	WithOption(
    		cli.NewOption("retry", "maximal retry times (default: 3)").WithType(cli.TypeInt),
    	).
    	WithArg(
    		cli.NewArg("package", "package name").WithType(cli.TypeString),
    	).
    	WithAction(func(args []string, options map[string]string) int {
    		if _, ok := options["retry"]; !ok {
    			options["retry"] = "3"
    		}
    		// do something
    	})
    

    With WithDefault method, I can write the default value 3 for only one time and avert a conflict between the actual code and the doc.

Related tags
Brigodier is a command parser & dispatcher, designed and developed for command lines such as for Discord bots or Minecraft chat commands. It is a complete port from Mojang's "brigadier" into Go.

brigodier Brigodier is a command parser & dispatcher, designed and developed to provide a simple and flexible command framework. It can be used in man

Dec 15, 2022
A simple, fast, and fun package for building command line apps in Go

cli cli is a simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable comm

Dec 31, 2022
cli is a simple, fast, and fun package for building command line apps in Go.

cli cli is a simple, fast, and fun package for building command line apps in Go. The goal is to enable developers to write fast and distributable comm

Jul 10, 2022
A command line tool that builds and (re)starts your web application everytime you save a Go or template fileA command line tool that builds and (re)starts your web application everytime you save a Go or template file

# Fresh Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file. If the web framework yo

Nov 22, 2021
Viper: a complete configuration solution for Go applications including 12-Factor apps
Viper: a complete configuration solution for Go applications including 12-Factor apps

Viper v2 feedback Viper is heading towards v2 and we would love to hear what you would like to see in it. Share your thoughts here: https://forms.gle/

Dec 6, 2021
An open-source GitLab command line tool bringing GitLab's cool features to your command line
An open-source GitLab command line tool bringing GitLab's cool features to your command line

GLab is an open source GitLab CLI tool bringing GitLab to your terminal next to where you are already working with git and your code without switching

Dec 30, 2022
A command line tool to prompt for a value to be included in another command line.

readval is a command line tool which is designed for one specific purpose—to prompt for a value to be included in another command line. readval prints

Dec 22, 2021
Package command provide simple API to create modern command-line interface

Package command Package command provide simple API to create modern command-line interface, mainly for lightweight usage, inspired by cobra Usage pack

Jan 16, 2022
Jan 3, 2023
CLI - A package for building command line app with go
CLI - A package for building command line app with go

Command line interface Screenshot Key features Lightweight and easy to use. Defines flag by tag, e.g. flag name(short or/and long), description, defau

Dec 23, 2022