Skip to content

romanoff/gofh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Go flags handler

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages