commando

package module
v0.0.0-...-78cbc3d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2015 License: MIT Imports: 6 Imported by: 1

README

Commando

A CLI Parser for Go

Build Status

GoDoc

Overview

Commando is a cli parser that handles nested commands, usage / help output, flags, and output formatting for you.

Define a root command, and attach subcommands to it. Tell your new commands what function to execute, and that's it.

Why

Because I don't like the UX of Flags. The goal here is a clean API to define complex cli programs.

Example

package main

import (
        "github.com/danielscottt/commando"
)

var root, cmd1 *commando.Command

func runCmd1() {
        commando.PrintFields(false, 0, "HI", "DOOD")
        commando.PrintFields(false, 0, "for path you said:", cmd1.Options["path"].Value)
        commando.PrintFields(false, 0, "jkfdhfkjdkljdshfkjhds", "fdkjkdfhjkdsfkjdsfh")
}

func main() {

        root = &commando.Command{
                Name: "main.go",
                Description: "Testing Commando, a CLI parser",
        }

        cmd1 = &commando.Command{
                Name: "cmd1",
                Description: "Command 1",
                Execute: runCmd1,
        }
        cmd1.AddOption("path", "Path to a thing", true, "-p", "--path")
        root.AddSubCommand(cmd1)

        root.Parse()
}

See the godocs for further documentation.

Documentation

Overview

A CLI Parser for Go

Overview

Commando is a cli parser that handles nested commands, usage / help output, flags, and output _formatting_ for you.

Define a root command, and attach subcommands to it. Tell your new commands what function to execute, and that's it.

Why

Because I don't like the UX of Flags. The goal here is a clean API to define complex cli programs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PrintFields

func PrintFields(indent bool, width int, fields ...interface{})

PrintFields is a wrapper for an IO Writer / Formatter. Using commando.PrintFields evenly spaces output into columns.

Types

type Command

type Command struct {
	Name        string              // Name of command, typically how a command is called from the cli.
	Description string              // A Description of the command, printed in usage.
	Options     map[string]*Option  // A map of the flags attached to this command, they are looked up by their name.
	Children    map[string]*Command // A map of all the subcommands, looked up by their name.
	Parent      *Command            // A pointer to the command's parent.  not set in root command.
	Execute     func()              // The function to run when executing a command.

}

Command is the base type for all commands.

func (*Command) AddOption

func (c *Command) AddOption(name string, descrip string, req bool, flags ...string)

AddOption is used to add an option (Flag) to a command.

func (*Command) AddSubCommand

func (c *Command) AddSubCommand(child *Command)

AddSubcommand attaches a command to a parent, as well as sets the parent property on the child command. Commands can be limitlessly nested (though, I don't recommend it).

func (*Command) Parse

func (c *Command) Parse()

Parse is the entry point into Commando. It recurses all the children of a command, finally executing the last command in the chain.

func (*Command) PrintHelp

func (c *Command) PrintHelp()

PrintHelp is used to print info and usage for any command. It knows if a command is the last in the chain, and if so, prints usage with just Options (Flags)

type Option

type Option struct {
	Name        string      // Name of Option, its name is used to retrieve its value.
	Description string      // A Description of the option, used when printing usage.
	Flags       []string    // The flags associated with the option.
	Value       interface{} // Where the value of a given flag is scanned into.
	Present     bool        // Used to determine whether or not a flag is present, typically for a bool type flag.
	Required    bool        // If a flag is required and not present, usage for owning command is printed.
}

Option is the type for flag options like "-p" or "--path"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL