CLI subcommands for Go

Overview

command

Build Status

command is a tiny package that helps you to add cli subcommands to your Go program with no effort, and prints a pretty guide if needed.

Usage: program <command>

where <command> is one of:
  version   prints the version
  command1  some description about command1
  command2  some description about command2

available flags:
  -exec-path="": a custom path to executable

program <command> -h for subcommand help

Usage

In order to start, go get this repository:

go get github.com/rakyll/command

This package allows you to use flags package as you used to do, and provides additional parsing for subcommands and subcommand flags.

import "github.com/rakyll/command"

// register any global flags
var flagExecPath = flag.String("exec-path", "", "a custom path to executable")

type VersionCommand struct{
	flagVerbose *bool
}

func (cmd *VersionCommand) Flags(fs *flag.FlagSet) *flag.FlagSet {
	// define subcommand's flags
	cmd.flagVerbose = fs.Bool("v", false, "provides verbose output")
	return fs
}

func (cmd *VersionCommand) Run(args []string) {
	// implement the main body of the subcommand here
  // required and optional arguments are found in args
}

// register version as a subcommand
command.On("version", "prints the version", &VersionCommand{}, []string{"<required-arg>"})
command.On("command1", "some description about command1", ..., []string{})
command.On("command2", "some description about command2", ..., []string{})
command.Parse()
// ...
command.Run()

The program above will handle the registered commands and invoke the matching command's Run or print subcommand help if -h is set.

$ program -exec-path=/home/user/bin/someexec version -v=true history

will output the version of the program in a verbose way requring an argument (history), and will set the exec path to the provided path. If arguments doesn't match any subcommand or illegal arguments are provided, it will print the usage guide.

License

Copyright 2013 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

You might also like...
Go-file-downloader-ftctl - A file downloader cli built using golang. Makes use of cobra for building the cli and go concurrent feature to download files
Cli-algorithm - A cli program with A&DS in go!

cli-algorithm Objectives The objective of this cli is to implement 4 basic algorithms to sort arrays been Merge Sort Insertion Sort Bubble Sort Quick

Nebulant-cli - Nebulant's CLI
Nebulant-cli - Nebulant's CLI

Nebulant CLI Website: https://nebulant.io Documentation: https://nebulant.io/docs.html The Nebulant CLI tool is a single binary that can be used as a

Cf-cli-find-app-plugin - CF CLI plugin to find applications containing a search string

Overview This cf cli plugin allows users to search for application names that co

Alertmanager-cli is a cli writtin in golang to silence alerts in AlertManager

Alertmanager-cli is a cli writtin in golang to silence alerts in AlertManager

News-parser-cli - Simple CLI which allows you to receive news depending on the parameters passed to it
News-parser-cli - Simple CLI which allows you to receive news depending on the parameters passed to it

news-parser-cli Simple CLI which allows you to receive news depending on the par

Go-api-cli - Small CLI to fetch data from an API sync and async

Async API Cli CLI to fetch data on "todos" from a given API in a number of ways.

Syno-cli - Synology unofficial API CLI and library

Synology CLI Unofficial wrapper over Synology API in Go. Focus on administrative

Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

Nebula Diagnosis CLI Tool is an information diagnosis cli tool for the nebula service and the node to which the service belongs.

bcrypt-cli is the CLI tool for hashing passwords with bcrypt.

bcrypt-cli bcrypt-cli is the CLI tool for hashing passwords with bcrypt. Install go install github.com/ryicoh/bcrypt-cli Usage It can be used like bas

Gobby-cli - CLI application to debug gobby applications

go(bby) Interactive debugging tool for gobby applications Usage Coming soon™ Ins

GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework
GTDF-CLI - The official CLI tool to operate with Getting Things Done Framework

This is the official CLI tool to operate with Getting Things Done Framework. How

Trakt-cli - A CLI for trakt.tv using the trakt.tv API
Trakt-cli - A CLI for trakt.tv using the trakt.tv API

trakt-cli ████████╗██████╗ █████╗ ██╗ ██╗████████╗ ██████╗██╗ ██╗ ╚══

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

A Simple and Clear CLI library. Dependency free.
A Simple and Clear CLI library. Dependency free.

A Simple and Clear CLI library. Dependency free. Features Nested Subcommands Uses the standard library flag package Auto-generated help Custom banners

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.
Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

cmdr cmdr is a POSIX-compliant, command-line UI (CLI) library in Golang. It is a getopt-like parser of command-line options, be compatible with the ge

A Commander for modern Go CLI interactions
A Commander for modern Go CLI interactions

Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files. Cobra is used i

A collection of CLI argument types for the Go `flag` package.

flagvar A collection of CLI argument types for the flag package. import "github.com/sgreben/flagvar" Or just copy & paste what you need. It's public d

Go library to simplify CLI workflow

This is a simple Go library to manage commands for your CLI tool. Easy to use and now you can focus on Business Logic instead of building the command

Comments
  • Allow required arguments for subcommands

    Allow required arguments for subcommands

    Simple enforcement that additional arguments are passed to a subcommand. If they are not then the subcommand's usage is displayed.

    command.On("subcommand", "prints the version", &SubCommand{}, []string{"<requiredArg1>", "<requiredArg2"})
    
    $ program -mainflags subcommand -subflags requiredArg1 requiredArg2
    
    command.On("version", "prints the version", &VersionCommand{}, []string{"<detail-level>"})
    
    $ program version full
    
    • BC break in On() function.
    • Arguments are fetched in cmd.Run() the same way as today (and there is no reference to the text passed to On() - that is currently used in usage output)
    • Addition of more whitespace is usage output
    opened by JustAdam 0
  • git / kubectl like pugin system.

    git / kubectl like pugin system.

    It would be nice to be able to specify a prefix and if a subcommand is not found look for an executable in the PATH that matches prefix-subcommand and execute it if found, passing any args and flags to that executable. Like git and kubectl do.

    Maybe this could be implemented through #4 .

    opened by luisdavim 0
  • Pr upstream

    Pr upstream

    This PR provides improvements to the original code:

    • help.Usage:
      • This is necessary to match the behaviour of common Unix usage where programs can take: prog -h, prog --help, prog help, prog -help, prog --h. Otherwise fla.Parse exits upon encountering -h, could define flag.Usage but we need to maintain the behaviour or passing arguments to the help function.
    • stdout is now the outfile descriptor instead of stderr: This is allow for users to pipe content into filters as well as to search for strings e.g using grep <a_prog_using_command> -h | grep -i keyword [keywords....]
    • help: stable sort order for subcommands: Makes the program help output more familiar and more easily indexed without minimal differences in case it changes, otherwise everytime there will be changes.
    opened by odeke-em 0
  • added a Commander to allow sub command nesting.

    added a Commander to allow sub command nesting.

    The main interface is unchanged, but it is now possible to nest subcommands.

    a nested subcommand is also a Cmd,

    this package provides a help object "Commander", that you can use to write your own Cmd with nested subcommands.

    for example

    type myCmd struct { // anonymous field to use Commander.Run() method command.Commander }

    func (cmd *myCmd) Flags(fs *flag.FlagSet) *flag.FlagSet { // to make a nested command, just build a new commander cmd.Commander = command.NewCommander("ci", fs) // and call the same methods as the usual top level ones cmd.On("log", "print remote log", &cicmd.LogCmd{}, nil) return fs }

    to avoid duplicating the code, I've also replaced the top level On, Parse, and Run() function to use a Commander.

    And I've also updated the Usage() to print subcommand in alphabetical order.

    opened by ericaro 0
A simple way for CLI command to have many subcommands

subcommands This is a modified fork of google/subcommands that uses lucasepe/pflag Subcommands is a Go package that implements a simple way for a sing

Luca Sepe 0 Oct 12, 2021
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.

Sensible and fast command-line flag parsing with excellent support for subcommands and positional values. Flags can be at any position. Flaggy has no

Eric Greer 815 Jan 1, 2023
Permits a Go application to implement subcommands support similar to what is supported by the 'go' tool.

subcommands golang library This package permits a Go application to implement subcommands support similar to what is supported by the 'go' tool. The l

M-A 21 Jul 28, 2022
Elegant CLI wrapper for kubeseal CLI

Overview This is a wrapper CLI ofkubeseal CLI, specifically the raw mode. If you just need to encrypt your secret on RAW mode, this CLI will be the ea

Elm 4 Jan 8, 2022
Network cli based on urfave/cli package

go_network_cli network cli based on urfave/cli package available on Working for ip, cname and mx. Use --hel

pipo7 0 Nov 28, 2021
Traefik config validator: a CLI tool to (syntactically) validate your Traefik configuration filesTraefik config validator: a CLI tool to (syntactically) validate your Traefik configuration files

Traefik Config Validator Note This is currently pre-release software. traefik-config-validator is a CLI tool to (syntactically) validate your Traefik

Thomas Klinger 0 Dec 16, 2021
CLI to run a docker image with R. CLI built using cobra library in go.

BlueBeak Installation Guide Task 1: Building the CLI The directory structure looks like Fastest process: 1)cd into bbtools 2)cd into bbtools/bin 3)I h

Aniruddha Chattopadhyay 0 Dec 20, 2021
A wrapper of aliyun-cli subcommand alidns, run aliyun-cli in Declarative mode.

aliyun-dns A wrapper of aliyun-cli subcommand alidns, run aliyun-cli in Declarative mode. Installation Install aliyun-cli. Usage $ aliyun-dns -h A wra

许嘉华 0 Dec 21, 2021
Symfony-cli - The Symfony CLI tool For Golang

Symfony CLI Install To install Symfony CLI, please download the appropriate vers

Symfony CLI 394 Dec 28, 2022
Go-file-downloader-ftctl - A file downloader cli built using golang. Makes use of cobra for building the cli and go concurrent feature to download files.

ftctl This is a file downloader cli written in Golang which uses the concurrent feature of go to download files. The cli is built using cobra. How to

Dipto Chakrabarty 2 Jan 2, 2022