cli

package module
v0.0.0-...-20ff868 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2014 License: BSD-3-Clause Imports: 7 Imported by: 0

README

cli
===

Note: I don't really like this structure any more. I've opted for a more
simple, reflection-free structure. See https://github.com/pnelson/cli and
consider this package deprecated.

Package cli provides structure for command line applications with sub-commands.

This package uses a touch of reflection magic to dispatch to a method with
named arguments. Commands help and version are implemented by default. The
usage information is pretty printed in an opinionated format.

The most basic cli application is boring:

  app := cli.New("myapp", "0.0.1")
  app.Run()

Build and run your application:

  $ ./myapp
  Usage: myapp <cmd> [options] [<args>]
    help      Output this usage information.
    version   Output the application version

Add commands:

  type add struct {
    showExtra *bool
    example   *string
    number    *int
  }

  func (c *add) Flags(flags *flag.FlagSet) {
    c.showExtra = flags.Bool("show-extra", false, "Print extra arguments.")
    c.example = flags.String("example", "", "An example string option.")
    c.number = flags.Int("number", 0, "An example int option.")
  }

  func (c *add) Run(key, username string, extra []string) {
    fmt.Printf("%s => %s\n", key, username)
    if *c.showExtra {
      fmt.Printf("  %v\n", extra)
    }
  }

  func (c *add) String() string {
    return "Add record key with username."
  }

  func main() {
    app := cli.New("myapp", "0.0.1")
    app.Rule(&add{}, "add", "<key> <username> [<extra>]")
    app.Run()
  }

Try building and running your application again:

  $ ./myapp
  Usage: myapp <cmd> [options] [<args>]
    help                                       Output this usage information.
    version                                    Output the application version.
    add [options] <key> <username> [<extra>]   Add record key with username.
      -example=<value>                         An example string option.
      -number=<n>                              An example int option.
      -show-extra                              Print extra arguments.

Copyright (c) 2014 by Philip Nelson. See LICENSE for details.

Documentation

Overview

Package cli provides structure for command line applications with sub-commands.

This package uses a touch of reflection magic to dispatch to a method with named arguments. Commands help and version are implemented by default. The usage information is pretty printed in an opinionated format. That said, this package still attempts to embrace the standard library flag package.

This package assumes that any arguments will remain strings. Any non-string arguments are likely to be passed as optional flags in practice.

See the documentation of Rule for details and restrictions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Application

type Application struct {
	// contains filtered or unexported fields
}

An Application represents a command line application.

func New

func New(name, version string) *Application

New creates a basic Application with help and version commands.

func (*Application) Rule

func (a *Application) Rule(command command, name, arguments string) error

Rule registers a command with the Application.

The command being registered must meet the requirements of the fmt.Stringer interface. The command must also have a method Flags that accepts a new *flag.FlagSet. The Flags method is where you would define flags for this particular sub-command.

Additionally, the command must have a Run method. If the Run method has no return value, the program will end with a successful exit code. If the Run method has one or more return values, only the first is considered and must be of type int. The first return value will be used as the exit code.

The Run method may accept parameters of type string. If the Run method has more parameters than there are arguments, the extra parameters will just be empty strings. If the Run method has less parameters than there are arguments, they will silently be ignored. Optionally, the last parameter of the Run method can be of type []string. In this case, any extra parameters will be passed to the final argument.

func (*Application) Run

func (a *Application) Run()

Run will parse flags and dispatch to the command.

type NullFlags

type NullFlags struct{}

NullFlags is an embeddable struct providing an empty FlagSet.

func (*NullFlags) Flags

func (c *NullFlags) Flags(flags *flag.FlagSet)

Flags is a no-op on the command FlagSet.

Jump to

Keyboard shortcuts

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