{"id":21682326,"url":"https://github.com/beevik/cmd","last_synced_at":"2025-04-12T06:52:53.018Z","repository":{"id":57502807,"uuid":"126562457","full_name":"beevik/cmd","owner":"beevik","description":"Command tree parser in go","archived":false,"fork":false,"pushed_at":"2024-07-06T20:08:23.000Z","size":35,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T06:52:46.893Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/beevik.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-24T03:29:06.000Z","updated_at":"2024-07-06T20:08:19.000Z","dependencies_parsed_at":"2024-11-25T15:50:56.605Z","dependency_job_id":null,"html_url":"https://github.com/beevik/cmd","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fcmd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fcmd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fcmd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/beevik%2Fcmd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/beevik","download_url":"https://codeload.github.com/beevik/cmd/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248530594,"owners_count":21119595,"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":[],"created_at":"2024-11-25T15:35:37.634Z","updated_at":"2025-04-12T06:52:52.995Z","avatar_url":"https://github.com/beevik.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GoDoc](https://godoc.org/github.com/beevik/cmd?status.svg)](https://godoc.org/github.com/beevik/cmd)\n\ncmd\n===\n\nThe `cmd` package is a lightweight, hierarchical command processor. It's\nhandy when you have a number of text commands you wish to organize into\na hierarchy.\n\nFor example, suppose you have written an application that uses the following\ncommand hierarchy:\n\n* file\n   * open\n   * close\n   * read\n   * write\n* status\n* quit\n\nWith each of these commands you have associated a callback function that is\ncalled with user-supplied arguments whenever the command is matched.\n\nNow consider what would happen if the application user types the following\ncommand into the application:\n\n```\nfile open foo.txt rw\n```\n\nThis command string would be fed into a command tree's `Lookup` function,\nwhich would return the callback associated with the `file/open` command\nas well as a slice of string arguments `[]string{\"foo.txt\", \"rw\"}`.\n\nThe `cmd` package supports shortest unambiguous prefix matches, so the\nfollowing command would return the same results:\n\n```\nf o foo.txt rw\n```\n\n### Code examples\n\nThis code shows how the command tree used in the example above might be\ncreated:\n\n```go\ntree := cmd.NewTree(\"root\")\nfile := cmd.NewTree(\"file\")\n\ntree.AddCommand(cmd.Command{Name: \"file\", Subtree: file})\ntree.AddCommand(cmd.Command{Name: \"status\", Brief: \"Show status\", Data: (*app).onStatus})\ntree.AddCommand(cmd.Command{Name: \"quit\", Brief: \"Quit application\", Data: (*app).onQuit})\n\nfile.AddCommand(cmd.Command{Name: \"open\", Brief: \"Open file\", Data: (*app).onOpen})\nfile.AddCommand(cmd.Command{Name: \"close\", Brief: \"Close file\", Data: (*app).onClose})\nfile.AddCommand(cmd.Command{Name: \"read\", Brief: \"Read file\", Data: (*app).onRead})\nfile.AddCommand(cmd.Command{Name: \"write\", Brief: \"Write file\", Data: (*app).onWrite})\n```\n\nAnd here is how you might query the command tree:\n\n```go\nfunc (a *app) processCommand(s string) error {\n    sel, err := tree.Lookup(s)\n    switch {\n        case err == cmd.ErrAmbiguous:\n            fmt.Printf(\"Command '%s' is ambiguous.\\n\", s)\n            return err\n        case err == cmd.ErrNotFound:\n            fmt.Printf(\"Command '%s' not found.\\n\", s)\n            return err\n        default:\n            handler := sel.Command.Param.(func(a *app, args []string) error)\n            return handler(a, sel.Args)\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeevik%2Fcmd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeevik%2Fcmd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeevik%2Fcmd/lists"}