{"id":21215442,"url":"https://github.com/choueric/cmdmux","last_synced_at":"2025-10-20T09:42:14.003Z","repository":{"id":57517120,"uuid":"79004510","full_name":"choueric/cmdmux","owner":"choueric","description":"Package cmdmux implements a command parser and router for terminal programme. ","archived":false,"fork":false,"pushed_at":"2020-07-02T22:51:02.000Z","size":44,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-21T16:31:58.554Z","etag":null,"topics":["cli","commandparser"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/choueric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-15T06:14:41.000Z","updated_at":"2023-02-14T02:54:11.000Z","dependencies_parsed_at":"2022-09-15T21:23:32.128Z","dependency_job_id":null,"html_url":"https://github.com/choueric/cmdmux","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2Fcmdmux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2Fcmdmux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2Fcmdmux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choueric%2Fcmdmux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choueric","download_url":"https://codeload.github.com/choueric/cmdmux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243665625,"owners_count":20327683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","commandparser"],"created_at":"2024-11-20T21:38:45.997Z","updated_at":"2025-10-20T09:42:08.954Z","avatar_url":"https://github.com/choueric.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cmdmux\n\nPackage cmdmux implements a command parser and router for terminal program.\n\n\n[![Build Status](https://travis-ci.org/choueric/cmdmux.svg?branch=master)](https://travis-ci.org/choueric/cmdmux)\n[![GoDoc](https://godoc.org/github.com/choueric/cmdmux?status.svg)](https://godoc.org/github.com/choueric/cmdmux)\n\n## TODO\n- [ ] Build the handle routes from help nodes so that when adding a new command, just add a help node and no need to touch other parts.\n\n# Overview\n\nIn general, there are two styles a terminal program to interact with users.\n\n1. Use -o, -p to specify parameters. Most programmes are in this way.\n2. Use sub-commands, like git which uses only one level sub-command.\n\nThe first way can be implemented by the `flag` package of Golang, and the\nsecond this package.\n\nThe package can:\n\n1. Build a terminal program with various commands easily.\n2. Generate a shell completion file (Now only for bash) !\n3. Output commands tree.\n\n# Usage\n\n## Build command line\n\nA simple example:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\n\t\"github.com/choueric/cmdmux\"\n)\n\ntype Options struct {\n\tarch string\n}\n\nfunc rootHandler(args []string, data interface{}) (int, error) {\n\tfmt.Println(\"Usage:\")\n\tcmdmux.PrintTree(os.Stderr)\n\treturn 0, nil\n}\n\nfunc buildHandler(args []string, data interface{}) (int, error) {\n\topt := data.(*Options)\n\tfmt.Printf(\"invoke 'build' of %s\\n\", opt.arch)\n\treturn 1, nil\n}\n\nfunc buildKernelHandler(args []string, data interface{}) (int, error) {\n\tfmt.Printf(\"invoke 'build kernel', args = %v\\n\", args)\n\treturn 2, nil\n}\n\nfunc main() {\n\topt := \u0026Options{arch: \"arm\"}\n\n\tcmdmux.HandleFunc(\"/\", rootHandler)\n\tcmdmux.HandleFunc(\"/build\", buildHandler)\n\tcmdmux.HandleFunc(\"/build/kernel\", buildKernelHandler)\n\tcmdmux.HandleFunc(\"/build/kernel/image\", buildKernelHandler)\n\tcmdmux.HandleFunc(\"/build/uboot\", buildKernelHandler)\n\n\tcmdmux.Execute(opt)\n}\n```\n\nThe package uses `HandleFunc()` to add handler for specific sub-command, like\nthe package `http`. The sub-command is represented by a command-path, like \n\"build\", \"build/kernel\".\n\nAfter adding handlers, invoke `Execute()` to parse the command line, route to\nthe correct handler and execute it.\n\nThe only one parameter of `Execute()` is passed to the parameter `data` of\nthe handler function. The parameter `args` of handler function is the rest part\nof command line stripped off the command-path part.\n\nThe results of this example are like:\n\n```\n$ test build\ninvoke 'build' of arm\n\n$ test build kernel optoins one\ninvoke 'build kernel', args = [options one]\n\n$ test cmd\nUsage:\n/*\n└── build*\n    ├── kernel*\n    │   └── image*\n    └── uboot*\n```\n\n## Print Commands Tree\n\nIn the above example, the root handler `rootHandler()` invoke `PrintTree()` to\noutput the commands tree. If one node has handler, it is appended a '*' symbol\nafter its command name.\n\n## Generate Shell Completion File\n\nAfter building various commands with `HandleFunc()`, it's time to get a shell\n(bash actually) completion file which helps users of your program input\ncommands easily on terminal.\n\nBelow is a example to get such file for `test` program:\n\n```go\n// some HandleFunc codes ...\n\nfile, err := os.Create(\"test-completion\")\nif err != nil {\n\tfmt.Println(err)\n\treturn\n}\ndefer file.Close()\n\nif err = cmdmux.GenerateCompletion(\"test\", file); err != nil {\n\tfmt.Println(err)\n}\n```\n\nThen apply this file:\n\n```sh\n$ source ./test-completion\n```\n\n# License\n\nSee the LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoueric%2Fcmdmux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoueric%2Fcmdmux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoueric%2Fcmdmux/lists"}