• Stars
    star
    66
  • Rank 468,167 (Top 10 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Go library for building command line applications.

gocmd

Godoc Release Build Report

A Go library for building command line applications.

Features

  • Advanced command line arguments handling
    • Subcommand handling
    • Short and long command line arguments
    • Multiple arguments (repeated or delimited)
    • Support for environment variables
    • Well formatted usage printing
    • Auto usage and version printing
    • Unknown argument handling
  • Output tables in the terminal
  • Template support for config files
  • No external dependency

Installation

go get github.com/devfacet/gocmd/v3

Usage

A basic app

For the full code click here.

func main() {
	flags := struct {
		Help      bool `short:"h" long:"help" description:"Display usage" global:"true"`
		Version   bool `short:"v" long:"version" description:"Display version"`
		VersionEx bool `long:"vv" description:"Display version (extended)"`
		Echo      struct {
			Settings bool `settings:"true" allow-unknown-arg:"true"`
		} `command:"echo" description:"Print arguments"`
		Math struct {
			Sqrt struct {
				Number float64 `short:"n" long:"number" required:"true" description:"Number"`
			} `command:"sqrt" description:"Calculate square root"`
			Pow struct {
				Base     float64 `short:"b" long:"base" required:"true" description:"Base"`
				Exponent float64 `short:"e" long:"exponent" required:"true" description:"Exponent"`
			} `command:"pow" description:"Calculate base exponential"`
		} `command:"math" description:"Math functions" nonempty:"true"`
	}{}

	// Echo command
	gocmd.HandleFlag("Echo", func(cmd *gocmd.Cmd, args []string) error {
		fmt.Printf("%s\n", strings.Join(cmd.FlagArgs("Echo")[1:], " "))
		return nil
	})

	// Math commands
	gocmd.HandleFlag("Math.Sqrt", func(cmd *gocmd.Cmd, args []string) error {
		fmt.Println(math.Sqrt(flags.Math.Sqrt.Number))
		return nil
	})
	gocmd.HandleFlag("Math.Pow", func(cmd *gocmd.Cmd, args []string) error {
		fmt.Println(math.Pow(flags.Math.Pow.Base, flags.Math.Pow.Exponent))
		return nil
	})

	// Init the app
	gocmd.New(gocmd.Options{
		Name:        "basic",
		Description: "A basic app",
		Version:     fmt.Sprintf("%s (%s)", version, gitCommit),
		Flags:       &flags,
		ConfigType:  gocmd.ConfigTypeAuto,
	})
}

Test

# Run tests
make test

# Continuous testing
make test-ui

# Benchmarks
make test-benchmarks

Contributing

See CONTRIBUTING.md

License

Licensed under The MIT License (MIT)
For the full copyright and license information, please view the LICENSE.txt file.

More Repositories

1

natsboard

Dashboard for monitoring NATS (an open source messaging system)
JavaScript
378
star
2

weather

A module for obtaining weather information
JavaScript
65
star
3

mws-product

A module for retrieving product information via Amazon MWS API
JavaScript
40
star
4

amazon-costs

A module for retrieving Amazon product information and calculating costs for fulfillment and merchant channels
JavaScript
23
star
5

amazon-seller

A module for retrieving Amazon seller information
JavaScript
19
star
6

knapsack

A module for resource allocation solving
JavaScript
15
star
7

seneca-nats-transport

Seneca NATS transport
JavaScript
10
star
8

iptocountry

A module for detecting country information of IP addresses without any API call
JavaScript
8
star
9

money

A module for obtaining currency rate and stock market information
JavaScript
5
star
10

goweb

A Go library for building tiny web applications such as dashboards, SPAs, etc.
Go
5
star
11

memcing

An in-memory key-value caching application with RESTful API
JavaScript
4
star
12

yubikey

A Go library that provides PIV smart card interface for YubiKey security keys.
Go
4
star
13

publicalerts

A module for obtaining emergency messages about hurricanes, storm warnings and earthquakes
JavaScript
4
star
14

byteman

A Go library that provides functions for bytes and bits.
Go
3
star
15

asqs-mdb

An application for handling Amazon Simple Queue Service (SQS) messages and saving into a MongoDB database
JavaScript
3
star
16

nntbn

A neural network library
C
2
star
17

streamy

A Go library that provides functions for streams.
Go
2
star
18

cipherman

A Golang library that provides various cipher implementations.
Go
2
star
19

worker

A Go library that provides worker pools.
Go
2
star
20

gorate

A Go library for rate limiting
Go
2
star
21

composedql

A query language that aims URI friendly queries for RESTful APIs
JavaScript
1
star
22

random

A Go library that provides functions for generating random values.
Go
1
star
23

ginman

A Golang library that provides wrapper for Gin Web Framework.
Go
1
star
24

utilex

A module that provides extra functions
JavaScript
1
star
25

zlog

A Golang library that provides abstraction layer for zerolog logging library.
Makefile
1
star
26

dotp

Duel of the Paddles. Yet another pong game.
TypeScript
1
star