All Projects → trhura → simplecli

trhura / simplecli

Licence: MIT license
The simplest way to parse cli arguments in golang

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to simplecli

commandline
Getopt styled command line parser.
Stars: ✭ 17 (+13.33%)
Mutual labels:  commandline
momoapi-python
MTN MoMo API Client Library for Python
Stars: ✭ 37 (+146.67%)
Mutual labels:  commandline
note-keeper
📓 A tiny bash tool for taking and organizing notes.
Stars: ✭ 58 (+286.67%)
Mutual labels:  commandline
command-line-quick-reference
quick reference on command line tools and techniques for the people with limited time
Stars: ✭ 331 (+2106.67%)
Mutual labels:  commandline
scrift
New generation shell and scripting language for everyone.
Stars: ✭ 46 (+206.67%)
Mutual labels:  commandline
console-logging
Better, prettier commandline logging for Python--with colors! 👻
Stars: ✭ 111 (+640%)
Mutual labels:  commandline
fasdr
PowerShell command line productivity booster
Stars: ✭ 31 (+106.67%)
Mutual labels:  commandline
prettyBenching
🦕 A small lib to make your Deno benchmarking progress and results look pretty
Stars: ✭ 23 (+53.33%)
Mutual labels:  commandline
ufolint
UFO source format linter
Stars: ✭ 23 (+53.33%)
Mutual labels:  commandline
import-cli-simple
This the meta package for Pacemaker Community, a Symfony based CLI application that provides import functionality for products, categories, attributes, and attribute-sets. The default format is CSV, adapters for XML are also available. The application can be declaratively extended by additional operations, which can be used to reassemble and exe…
Stars: ✭ 69 (+360%)
Mutual labels:  commandline
elive-tools
Set of handy and useful tools by Elive, for specially use in the Elive project
Stars: ✭ 13 (-13.33%)
Mutual labels:  commandline
include gardener
This is a small C++ based commandline-tool which analyzes include statements in C/C++ code.
Stars: ✭ 66 (+340%)
Mutual labels:  commandline
DfmExtractor
Small command line utility which allows you to extract DFM, LFM and FRM forms from executable files compiled by Delphi, Lazarus and CodeTyphon.
Stars: ✭ 22 (+46.67%)
Mutual labels:  commandline
dynamic-cli
A Modern, user-friendly command-line HTTP client for the API testing, and if you're stuck - Search and browse StackOverflow without leaving the CLI
Stars: ✭ 151 (+906.67%)
Mutual labels:  commandline
select-run
A CLI tool to interactively search & select one or many package.json npm scripts to run
Stars: ✭ 29 (+93.33%)
Mutual labels:  commandline
machinescli
This tool provides commandline access for https://www.hackthebox.eu, https://tryhackme.com/ and https://www.vulnhub.com/ machines.
Stars: ✭ 34 (+126.67%)
Mutual labels:  commandline
ansiart2utf8
Processes legacy BBS-style ANSI art (ACiDDraw, PabloDraw, etc.) to UTF-8. Escape codes and line endings are processed for terminal friendliness.
Stars: ✭ 32 (+113.33%)
Mutual labels:  commandline
cotp
Trustworthy, encrypted, command-line TOTP/HOTP authenticator app with import functionality.
Stars: ✭ 45 (+200%)
Mutual labels:  commandline
vt100
💻 VT100 Terminal Package
Stars: ✭ 19 (+26.67%)
Mutual labels:  commandline
Commandline-Games-hacktoberfest
A repository to share command line games. An opportunity to start and learn about open source code contributions flow.
Stars: ✭ 16 (+6.67%)
Mutual labels:  commandline

SimpleCLI

The simplest way to handle cli arguments in golang. Inspired by python Fire package.

Basic usage

package main

import (
        "fmt"
        "github.com/trhura/simplecli"
)

type Calc struct{}

// Need to be a public method, so that it is accessible by external package.
func (c Calc) Add(x int, y int) {
        fmt.Println(x + y)
}

func (c Calc) Multiply(x int, y int) {
        fmt.Println(x * y)
}

func main() {
        simplecli.Handle(&Calc{})
}

CLI usage

thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc
Usage: ./calc add (int, int)
              multiply (int, int)
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc divide
Error:  divide is not a valid command.
...
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc add 3
Error: add requires 2 argument(s).
...
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc add 3 as
Error: as is not a valid number.
...
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc add 3 4
7

With options / flags

package main

import (
	"fmt"
	"github.com/trhura/simplecli"
	"strconv"
)

type Calc struct {
	// Need to be a public field, so that it is accessible by external package.
	Base    int  `base (radix) of input numbers`
	Verbose bool `print verbose output`
}

// Need to be a public method, so that it is accessible by external package.
func (c Calc) Add(x int, y int) {
	xb, _ := strconv.ParseInt(strconv.Itoa(x), c.Base, 32)
	yb, _ := strconv.ParseInt(strconv.Itoa(y), c.Base, 32)
	if c.Verbose {
		fmt.Printf("%d + %d = ", xb, yb)
	}
	fmt.Println(xb + yb)
}

func (c Calc) Multiply(x int, y int) {
	xb, _ := strconv.ParseInt(strconv.Itoa(x), c.Base, 32)
	yb, _ := strconv.ParseInt(strconv.Itoa(y), c.Base, 32)
	if c.Verbose {
		fmt.Printf("%d * %d = ", xb, yb)
	}
	fmt.Println(xb * yb)
}

func main() {
	// Needs to pass a pointer to struct, so it can be modified.
	simplecli.Handle(&Calc{Base: 10, Verbose: false})
}

CLI Usage

thurahlaing @ simplecli > go build examples/calc.go && ./calc
Usage: ./calc [options] add (int, int)
                        multiply (int, int)
Options:
    --base          int   base (radix) of input numbers
    --verbose      bool   print verbose output
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc add 01 10
11
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc --verbose add 01 10
1 + 10 = 11
thurahlaing @ simplecli > go build examples/simple/calc.go && ./calc --verbose --base=2 add 01 10
1 + 2 = 3

Nested commands

package main

import (
	"fmt"
	"github.com/trhura/simplecli"
)

// Database ...
type Database struct {
	Path string `database url path`
}

// Create database
func (db Database) Create() {
	fmt.Println("Creating database.")
}

// Drop database
func (db Database) Drop() {
	fmt.Println("Dropping database.")
}

// App ...
type App struct {
	Database *Database	// needs to be a pointer
	Port     int `server port `
}

// Start the app
func (app App) Start() {
	fmt.Printf("Listening app at %d.\n", app.Port)
}

// Reload the app
func (app App) Reload() {
	fmt.Println("Reloading app.")
}

// Kill the app
func (app App) Kill() {
	fmt.Println("Stoping app.")
}

func main() {
	simplecli.Handle(&App{
		Database: &Database{},
		Port:     8080,
	})
}

CLI Usage

thurahlaing @ simplecli > go build examples/nested/app.go && ./app
Usage: ./app [options] kill ()
                       reload ()
                       start ()
                       database ...
Options:
        --port (int)		`server port`
thurahlaing @ simplecli >  go build examples/nested/app.go && ./app database
Usage: database [options] drop ()
                          create ()
Options:
        --path (string)		`database url path`
thurahlaing @ simplecli > go build examples/nested/app.go && ./app --port=80 start
Listening app at 80.
thurahlaing @ simplecli > go build examples/nested/app.go && ./app --port=80 database --path=dburl create
Creating database.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].