{"id":21975264,"url":"https://github.com/yaegashi/cobra-cmder","last_synced_at":"2025-04-28T15:51:30.797Z","repository":{"id":136842222,"uuid":"274499368","full_name":"yaegashi/cobra-cmder","owner":"yaegashi","description":"Useful command builder for spf13/cobra","archived":false,"fork":false,"pushed_at":"2020-06-29T12:11:46.000Z","size":12,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T11:02:46.691Z","etag":null,"topics":["cli","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yaegashi.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":"2020-06-23T20:10:57.000Z","updated_at":"2021-04-24T13:09:36.000Z","dependencies_parsed_at":"2024-06-20T21:59:38.833Z","dependency_job_id":"e44999e9-0aab-4edf-9806-bd82dff5d2de","html_url":"https://github.com/yaegashi/cobra-cmder","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/yaegashi%2Fcobra-cmder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaegashi%2Fcobra-cmder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaegashi%2Fcobra-cmder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yaegashi%2Fcobra-cmder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yaegashi","download_url":"https://codeload.github.com/yaegashi/cobra-cmder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251342700,"owners_count":21574242,"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","golang"],"created_at":"2024-11-29T15:50:37.024Z","updated_at":"2025-04-28T15:51:30.774Z","avatar_url":"https://github.com/yaegashi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cobra-cmder\n\n[![go.dev](https://img.shields.io/badge/go.dev-reference-000000?logo=go)](https://pkg.go.dev/github.com/yaegashi/cobra-cmder)\n\n## Introduction\n\nThis Go module contains `cmder` library package\nwhich is a useful command builder for\n[spf13/cobra](https://github.com/spf13/cobra).\n\nIt helps you to easily build a cobra.Command hierarchy\nin the non-invasive and test-friendly way without any global variables or init().\n\n## Basic usage\n\nDefine each command's sturct that implements `Cmder` interface:\n\n```go\ntype App struct {\n\tBool bool // storage for flag -b\n}\ntype AppAlpha struct {\n\t*App          // storage for parent Cmder (embedded)\n\tString string // storage for flag -s\n}\ntype AppAlphaOne struct {\n\t*AppAlpha     // storage for parent Cmder (embedded)\n\tInt       int // storage for flag -i\n}\n```\n\nDefine each Comder's `Cmd()` method that returns `*cobra.Command`:\n\n```go\nfunc (app *App) Cmd() *cobra.Command {\n\tcmd := \u0026cobra.Command{\n\t\tUse: \"app\",\n\t}\n\tcmd.PersistentFlags().BoolVarP(\u0026app.Bool, \"bool\", \"b\", false, \"Bool flag\")\n\treturn cmd\n}\n\nfunc (app *AppAlpha) Cmd() *cobra.Command {\n\tcmd := \u0026cobra.Command{\n\t\tUse: \"alpha\",\n\t}\n\tcmd.PersistentFlags().StringVarP(\u0026app.String, \"string\", \"s\", \"\", \"String flag\")\n\treturn cmd\n}\n\nfunc (app *AppAlphaOne) Cmd() *cobra.Command {\n\tcmd := \u0026cobra.Command{\n\t\tUse: \"one\",\n\t\tRun: app.Run,\n\t}\n\tcmd.Flags().IntVarP(\u0026app.Int, \"int\", \"i\", 0, \"Int flag\")\n\treturn cmd\n}\n\nfunc (app *AppAlphaOne) Run(cmd *cobra.Command, args []string) {\n\tfmt.Println(app.Bool, app.String, app.Int)\n}\n```\n\nAssociate Cmders each other by defining a method that returns a child Cmder:\n\n```go\nfunc (app *App) AppAlphaCmder() cmder.Cmder         { return \u0026AppAlpha{App: app} }\nfunc (app *AppAlpha) AppAlphaOneCmder() cmder.Cmder { return \u0026AppAlphaOne{AppAlpha: app} }\n```\n\nCall `cmder.Cmd()` to collect and associate all `cobra.Command` instances:\n\n```go\nfunc main() {\n\tapp := \u0026App{}\n\tcmd := cmder.Cmd(app)\n\terr := cmd.Execute()\n\tif err != nil {\n\t\tos.Exit(1)\n\t}\n}\n```\n\nVisit https://play.golang.org/p/zw4arxJfUkt to see and test the complete source code.\n\n## Unit test\n\nYou can easily perform the Go standard unit tests on CLI apps with cobra-cmder.\n\nSee https://play.golang.org/p/tijvjDzmwqW for another example.\n\n## Examples\n\nSee the sample CLI app in [cmd/sample](cmd/sample) for more comprehensive example.\n\nUsage in real applications:\n\n- https://github.com/yaegashi/contest.go\n- https://github.com/yaegashi/azbill\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaegashi%2Fcobra-cmder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyaegashi%2Fcobra-cmder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyaegashi%2Fcobra-cmder/lists"}