gofh

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

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

Go to latest
Published: Feb 5, 2014 License: MIT Imports: 1 Imported by: 1

README

Go Flags Handler

Go flags handler allows you to handle console commands for your application in similar way as you do it for web applications. You just register handlers and then if pattern of command matches pattern specified for handler, it is called. Go has flags package, but it is different and doesn't provide routing to different functions based on arguments.

Why would I need it?

Let's say you want to implement console utility that will interact with user similarly to git. And you want to start with couple of commands: project init and project deploy. Here is how your go code would look like:

package main

import (
	"github.com/romanoff/gofh"
	"fmt"
	"os"
)

func main() {
	fh := gofh.Init()
	fh.HandleCommand("init", initProject)
	fh.HandleCommand("deploy", deployProject)
	fh.SetDefaultHandler(showUsage)
	fh.Parse(os.Args[1:])
}

func showUsage() {
	fmt.Println("Please, use 'project init' or 'project deploy' command")
}

func initProject(options map[string]string) {
	fmt.Println("Your init project code goes here")
}

func deployProject(options map[string]string) {
	fmt.Println("Your deploy project code goes here")
}

Command options

You can add a handler with options. Here is an example:

options := []*gofh.Option{
  &gofh.Option{Name: "no-css", Boolean: true}
  &gofh.Option{Name: "db"}
}

gofh.HandleCommandWithOptions("init", options, initHandler)

In this example, no-css is boolean option. So, if you want to supply this option, you just have to add --no-css to you console command. It will look like this: project init --no-css. After this initHandler will get map that will have no-css key set to true. If --no-css option won't be supplied, no-css key will be empty.

There is also value option in the above example. to add db option value, you would have to use following console command project init --db mysql. In following example db key for initHandler options would be set to mysql.

Specify named argument in HandleCommand

	fh.HandleCommand("deploy :location", deployProject)

Above code requires you to have location specified. Location can be any text and it will be supplied in options map to deployProject function.

Summary

I think that this package can be useful to many people. And even though it doesn't cover all possible scenarios for command line arguments, it simplifies creation of git- like command line utilities.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(map[string]string)

type Command

type Command struct {
	Pattern  string
	Callback func(map[string]string)
	Options  []*Option
}

func (*Command) Matches

func (self *Command) Matches(args []string) map[string]string

type CommandArguments

type CommandArguments []string

type Flags

type Flags struct {
	Commands       []*Command
	DefaultHandler func()
}

func Init

func Init() *Flags

func (*Flags) HandleCommand

func (self *Flags) HandleCommand(pattern string, callback Callback)

func (*Flags) HandleCommandWithOptions

func (self *Flags) HandleCommandWithOptions(pattern string, options []*Option, callback Callback)

func (*Flags) Parse

func (self *Flags) Parse(args []string)

func (*Flags) SetDefaultHandler

func (self *Flags) SetDefaultHandler(handler func())

type Option

type Option struct {
	Name    string
	Boolean bool
}

type ParsedOptions

type ParsedOptions map[string]string

Jump to

Keyboard shortcuts

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