Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/leehuk/go-clicommand

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/leehuk/go-clicommand

  • v0.0.0-20180527141332-b81063b586aa
  • Source
  • Go
  • Socket score

Version published
Created
Source

go-clicommand

clicommand provides Go CLI applications with subcommand/api-style interfaces and option/parameter handling

GoDoc Go Report Card Build Status

Installation

go get -u github.com/leehuk/go-clicommand

Overview

The clicommand library makes the creation of Go CLI applications using a subcommand interface easier. The subcommand interface is structured as a parent/child tree so the application can mimic an api, with edges of the tree running custom Handler functions and the tree providing a structured way of grouping commands, attaching option arguments and finding additional parameters.

Command Tree

Command objects are chained together to build a tree which has arbitrary depth, providing it follows the tree rules:

  • Each parent command within the tree may have any number of children.
  • Each child command object within the tree has a single parent.
  • Every child Command object within the tree can have its own children, except when it has a Handler function.

CLI Application Pseudo Example

This allows building a CLI application which can mimic an API, e.g.:

./clicommand                         // parent, has children
./clicommand http                    // child of clicommand, has children itself
./clicommand http get => Handler()   // child of clicommand->http, calls Handler() when run.
                                     // Cannot have children.
./clicommand http post => Handler()  // child of clicommand->http, calls Handler() when run.
                                     // Cannot have children.

CLI Options

Options can be attached to the tree at any point and these are inherited along the tree, so child commands also have options from their parent commands. Options are defined as either having or not having parameters, options with parameters use double dashes and options without parameters use single dashes as selectors.

CLI Parameters

Anything the parser doesnt recognise is stored as a parameter, alloowing applications to accept things as a simple generic parameter, rather than requiring its specified as an option.

Autogenerated Help

As each command and option is added to the tree with a name and description, the parser can automatically construct help information and display it when the program is run without parameters, or the 'help' command is used. The following example uses the sample helloworld program from https://git.io/vNDug

[golang@908222b2e8aa helloworld]$ ./helloworld help

helloworld - Sample hello world program
helloworld [-u] [-lower]

helloworld options:
   -u                    Uppercase output
   -lower                Lowercase output

Available subcommands:
  hello        Says hello world
  say          Says something

For help information run:
  'helloworld help' .. 'helloworld <commands>* help' .. 'helloworld [commands]* help [subcommand]*'

[golang@908222b2e8aa helloworld]$ 

Sample Program

A sample helloworld.go program can be found under examples.

FAQs

Package last updated on 27 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc