omcli

package module
v0.0.0-...-5094f97 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2017 License: MIT Imports: 3 Imported by: 0

README

omcli

Oh My! CLI is a small package for creating command line apps in go

This package only sports one level of sub commands, and flags may only be set on sub commands.

Commands are looked up by their Name, ie 'cmd run' looks for run a command with the name field being "run".

A help command is automatically added and runing 'cmd [subcommand] help' will print the help for [subcommand].

Example

package main

import (
	"fmt"
	"strconv"
	"strings"

	"bitbucket.org/gdl_iam/omcli"
)

var root = &omcli.Command{
	Name:  "test_cli",
	Usage: "test_cli",
	Short: "test_cli tests the cli package",
	Long:  "blah blah blah",

	Run: func(cmd *omcli.Command, args []string) {
		fmt.Println("hello world")
	},
}

var concat = &omcli.Command{
	Name:  "concat",
	Usage: "concat [flags]... [string]...",
	Short: "concat concatenates its args and prints them to stdout",
	Long:  `A much longer concat description`,

	Run: func(cmd *omcli.Command, args []string) {
		fmt.Println(strings.Join(args, ""))
	},
}

var add = &omcli.Command{
	Name:  "add",
	Usage: "add [flags]... [int]...",
	Short: "add gives the sum of the args passed",
	Long:  "Add does some hard core addition work",

	Run: func(cmd *omcli.Command, args []string) {
		var total int

		for _, arg := range args {
			val, err := strconv.Atoi(arg)
			if err != nil {
				panic(err)
			}
			total += val
		}
		fmt.Println(total)
	},
}

func init() {
	concat.Flags.String("blah", "test", "a string flag")
	root.AddCommand(concat)
	root.AddCommand(add)
}

func main() {
	root.Execute()
}


Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {

	// Name is the name of the command, if empty,
	// fallback to trying first word of usage, then short, then long
	Name string

	// Usage is the single line usage information
	Usage string

	// Short is a short description of the command used when help is called
	Short string

	// Long is the long description of the command used when help [command] is called
	Long string

	// Flags are the command line flags for the command
	// The root command will not have it's flags parsed, flags are reserved
	// for subcommands
	Flags flag.FlagSet

	// Run runs the command
	Run func(*Command, []string)
	// contains filtered or unexported fields
}

Command is a command to be run from the command line

func (*Command) AddCommand

func (c *Command) AddCommand(command *Command)

AddCommand ands a subcommand to the command

func (*Command) Execute

func (c *Command) Execute()

Execute runs the command

Jump to

Keyboard shortcuts

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