cmdns

package module
v0.0.0-...-f20f630 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

README

cmdns GoDoc Build Status

cmdns is a go library for Cobra for name-spacing a command's immediate children. Command name-spacing is an alternative way to structure sub commands, similar to rake db:migrate and ovrclk apps:create.

Cobra is a popular library for creating powerful modern CLI applications used by Kubernetes, Docker, rkt, Parse, and many more widely used Go projects.

Rationale

Name spacing improves a command line's usability by limiting the depth of its subcommands tree and establishes a clear separation between the command, subcommand and its arguments. Complex commands that have over three levels tend to be harder to remember, especially with arguments. For example, the command to edit a data bag item in chef, a popular infrastructure tool, is knife data bag edit dogs tibetanspaniel. There is really no way to tell where the command stops and the arguments begin. This leads to confusion, and generally is hard to repeat. A name-spaced representation could be knife databag:edit dogs tibetanspaniel. Its clear here that knife databag:edit edits the tibetanspaniel item in the data bag dogs.

Usage Example

ovrclk := &cobra.Command{Use: "ovrclk"}
apps := &cobra.Command{Use: "apps"}
apps.AddCommand(&cobra.Command{Use: "info", Run: runFunc})
ovrclk.AddCommand(apps)

// Enable namespacing
cmdns.Namespace(ovrclk)

ovrclk.Execute()

The above example will name-space info with apps. It lets you run:

$ ovrclk apps:info

And, updates the usage function for ovrclk:apps -h:

Available Commands:
  apps:info

Use "ovrclk [command] --help" for more information about a command.

To disable overriding usage function:

cmdns.SetOverrideUsageFunc(false)

Documentation

Overview

Package cmdns provides namespacing to Cobra command's subcommands Command namespacing is an alternative way to structure sub commands, similar to `rake db:migrate` and `ovrclk apps:create`.

Example
package main

import (
	"fmt"

	"github.com/gosuri/cmdns"
	"github.com/spf13/cobra"
)

var runFunc = func(cmd *cobra.Command, args []string) { fmt.Println("run", cmd.Name()) }

func main() {
	ovrclk := &cobra.Command{Use: "ovrclk"}
	apps := &cobra.Command{Use: "apps"}
	apps.AddCommand(&cobra.Command{Use: "info", Run: runFunc})
	ovrclk.AddCommand(apps)

	// Enable namespacing
	cmdns.Namespace(ovrclk)

	ovrclk.Execute()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultNamespacer is the default namespacer for the package
	DefaultNamespacer = New()

	// DefaultNamespaceSeperator is the char that seperates commands
	DefaultNamespaceSeperator = ':'
)

Functions

func Namespace

func Namespace(cmd *cobra.Command) error

Namespace enables namespacing for the command using the DefaultCmdNS

Types

type CobraNamespace

type CobraNamespace struct {
	OverrideUsageFunc bool
	// contains filtered or unexported fields
}

CobraNamespace represents a namespace for a command. This is usually then second level command.

func NewCobraNamespace

func NewCobraNamespace() *CobraNamespace

NewCobraNamespace returns a new Namespace

func (*CobraNamespace) AvailableCommands

func (n *CobraNamespace) AvailableCommands() []*cobra.Command

AvailableCommands returns the namespaced commands that are available

func (*CobraNamespace) Command

func (n *CobraNamespace) Command() *cobra.Command

Command returns the command for the namespace

func (*CobraNamespace) Namespace

func (n *CobraNamespace) Namespace(cmd *cobra.Command) error

Namespace enables namespacing for a sub-commmand and its immediated children. It returns an error if the command does not have a parent.

func (*CobraNamespace) UsageFunc

func (n *CobraNamespace) UsageFunc() (f func(*cobra.Command) error)

UsageFunc returns the usage function for the command that renders namespaces

type CobraNamespacer

type CobraNamespacer struct {
	// Namespaces is the collection of cobra namespaces
	Namespaces        []*CobraNamespace
	OverrideUsageFunc bool
}

CobraNamespacer is the struct represting the component that namespaces cobra's sucommands

func New

func New() *CobraNamespacer

New returns a new instance of the CobraNamespacer

func SetOverrideUsageFunc

func SetOverrideUsageFunc(v bool) *CobraNamespacer

SetOverrideUsageFunc when set to true will overide the command's usage function with the package's usage function that displays namespaces using the default namespacer

func (*CobraNamespacer) Namespace

func (c *CobraNamespacer) Namespace(cmd *cobra.Command) error

Namespace enables namespacing for the command's subcommands

func (*CobraNamespacer) SetOverrideUsageFunc

func (c *CobraNamespacer) SetOverrideUsageFunc(v bool) *CobraNamespacer

SetOverrideUsageFunc when set to true will overide the command's usage function with the package's usage function that displays namespaces

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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