{"id":13410411,"url":"https://github.com/jaffee/commandeer","last_synced_at":"2025-10-04T19:49:38.314Z","repository":{"id":49481279,"uuid":"106636480","full_name":"jaffee/commandeer","owner":"jaffee","description":"Automatically sets up command line flags based on struct fields and tags.","archived":false,"fork":false,"pushed_at":"2022-09-20T17:55:00.000Z","size":89,"stargazers_count":175,"open_issues_count":5,"forks_count":16,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-09T18:18:50.192Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jaffee.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-10-12T02:51:05.000Z","updated_at":"2025-04-07T18:04:21.000Z","dependencies_parsed_at":"2022-08-26T03:11:46.421Z","dependency_job_id":null,"html_url":"https://github.com/jaffee/commandeer","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaffee%2Fcommandeer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaffee%2Fcommandeer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaffee%2Fcommandeer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jaffee%2Fcommandeer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jaffee","download_url":"https://codeload.github.com/jaffee/commandeer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085325,"owners_count":21045139,"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-07-30T20:01:06.717Z","updated_at":"2025-10-04T19:49:38.206Z","avatar_url":"https://github.com/jaffee.png","language":"Go","readme":"## Commandeer\n[![Go Report Card](https://goreportcard.com/badge/github.com/jaffee/commandeer)](https://goreportcard.com/report/github.com/jaffee/commandeer)\n[![GoDoc](https://godoc.org/github.com/jaffee/commandeer?status.svg)](https://godoc.org/github.com/jaffee/commandeer)\n[![Coverage](http://gocover.io/_badge/github.com/jaffee/commandeer)](https://gocover.io/github.com/jaffee/commandeer)\n\n![Image](https://i.imgur.com/y6GmOGE.png)\n\nCommandeer sets up command line flags based on struct fields and tags.\n\nDo you...\n * like to develop Go apps as libraries with tiny main packages?\n * get frustrated keeping your flags up to date as your code evolves?\n * feel irked by the overlap between comments on struct fields and help strings for flags?\n * hate switching between your app's main and library packages?\n\nYou might like Commandeer. See the [godoc](https://godoc.org/github.com/jaffee/commandeer) for detailed usage, or just...\n\n## Try It!\nHere's how it works, define your app like so:\n```go\npackage myapp\n\nimport \"fmt\"\n\ntype Main struct {\n\tNum     int    `help:\"How many does it take?\"`\n\tVehicle string `help:\"What did they get?\"`\n}\n\nfunc NewMain() *Main { return \u0026Main{Num: 5, Vehicle: \"jeep\"} }\n\nfunc (m *Main) Run() error {\n\tif m.Num \u003c 2 || m.Vehicle == \"\" {\n\t\treturn fmt.Errorf(\"Need more gophers and/or vehicles.\")\n\t}\n\tfmt.Printf(\"%d gophers stole my %s!\\n\", m.Num, m.Vehicle)\n\treturn nil\n}\n```\n\nand your main package:\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/jaffee/commandeer\"\n\t\"github.com/jaffee/commandeer/examples/myapp\"\n)\n\nfunc main() {\n\terr := commandeer.Run(myapp.NewMain())\n\tif err != nil {\n\t\tfmt.Println(err)\n\t}\n}\n```\n\nNow...\n```bash\n$ ./myapp -h\nUsage of ./myapp:\n  -num int\n    \tHow many does it take? (default 5)\n  -vehicle string\n    \tWhat did they get? (default \"jeep\")\n\n$ ./myapp\n5 gophers stole my jeep!\n$ ./myapp -num 3 -vehicle horse\n3 gophers stole my horse!\n```\n\nNotice that Commandeer set up the default values for each flag based on the\nvalues in the struct passed to `Run`.\n\nCommandeer is set up for minimal dependency pollution - it uses only stdlib\ndependencies and is a few hundred lines of code itself. You need only import it\nfrom a tiny `main` package (as in the example), and shouldn't need to reference\nit anywhere else.\n\nIf you aren't allergic to external dependencies, you can also try\n`github.com/jaffee/commandeer/cobrafy` which pulls in the excellent [Cobra](https://github.com/spf13/cobra) and\n[pflag](https://github.com/spf13/pflag) packages giving you GNU/POSIX style flags and some other nice features\nshould you care to use them. See the [godoc](https://godoc.org/github.com/jaffee/commandeer/cobrafy), or the [myapp-cobrafy example](https://github.com/jaffee/commandeer/blob/master/examples/myapp/cmd/myapp-cobrafy/main.go).\n\n## Features\nIn addition to the `help` struct tag, you can use `flag` to override the computed flag name, e.g.\n\n```go\ntype Main struct {\n\tNum     int    `flag:\"number\"`\n}\n```\n\nYou can also use `flag:\"-\"` to explicitly ignore fields from being used as flags, e.g.\n```go\ntype Main struct {\n\tNum     int    `flag:\"-\"`\n}\n```\n\nNested structs are supported, by default the field names will be joined with \".\" to create the flag name, e.g.\n\n```go\ntype Main struct {\n\tVehicle struct {\n\t\tColor string\n\t\tWeight int\n\t}\n}\n```\n\nproduces:\n\n```\n  -vehicle.color string\n    \t\n  -vehicle.weight int\n```\n\nIf you wish to avoid this prefix behavior (e.g. if you have an embedded struct field and you want to elevate its fields to the top level) you can use `flag:\"!embed\"`, e.g.\n\n```go\ntype Main struct {\n\tVehicle struct {\n\t\tColor string\n\t\tWeight int\n\t} `flag:\"!embed\"`\n}\n```\n\nwhich will produce:\n```\n  -color string\n    \t\n  -weight int\n```\n\n\n## Contributing\nYes please!\n\nFor small stuff, feel free to submit a PR directly. For larger things,\nespecially API changes, it's best to make an issue first so it can be discussed.\n\n## Similar projects\n\n * https://github.com/anacrolix/tagflag\n * https://github.com/octago/sflags\n","funding_links":[],"categories":["Command Line","Build Automation","命令行","命令行工具### 标准 CLI`用于创建一个标准命令行应用程序的库`","命令行工具"],"sub_categories":["Standard CLI","标准CLI","标准 CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaffee%2Fcommandeer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaffee%2Fcommandeer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaffee%2Fcommandeer/lists"}