KO
|
EN
gitlite — search
Search
#python
#golang
#docker
#javascript
#python3
#node
#hacktoberfest
#nodejs
#deep-learning
#pytorch
#api-client
#css
gofh
★ 36
Open GitHub ↗
Go flags handler
Download README (.md)
Explore Similar Repositories
mab_bookmark_extension
:
Example code for my article on building a simple Google Chrome extension.
jiraview
:
Extract data from JIRA through REST and create charts.
Media-Query-Sync
:
Ⓜ️ A cross-browser solution for syncing Javascript functionality with CSS media queries
orca.js
:
Open Real-Time Communications API
angular-drag-drop
:
Drag and drop with angular using directives and HTML5
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
gofh
?
Download (.md)
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](http://golang.org/pkg/flag/), 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: ```go 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: ```go 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 --------------------------------------- ```go 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.