{"id":24083366,"url":"https://github.com/mitranim/cmd","last_synced_at":"2026-06-10T03:31:38.269Z","repository":{"id":57624559,"uuid":"398849718","full_name":"mitranim/cmd","owner":"mitranim","description":"Missing feature of the Go standard library: ability to define subcommands while using `flag`.","archived":false,"fork":false,"pushed_at":"2021-11-22T11:37:52.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-24T06:44:38.320Z","etag":null,"topics":["cli","cmd","go","golang"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/mitranim/cmd","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mitranim.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-22T16:43:18.000Z","updated_at":"2023-08-01T09:09:28.000Z","dependencies_parsed_at":"2022-08-26T22:21:26.546Z","dependency_job_id":null,"html_url":"https://github.com/mitranim/cmd","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fcmd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fcmd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fcmd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fcmd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitranim","download_url":"https://codeload.github.com/mitranim/cmd/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240959115,"owners_count":19884911,"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","cmd","go","golang"],"created_at":"2025-01-09T23:56:29.934Z","updated_at":"2026-06-10T03:31:38.264Z","avatar_url":"https://github.com/mitranim.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Overview\n\nMissing feature of the Go standard library: ability to define subcommands while using `flag`.\n\n  * Complements `flag` by adding subcommands.\n  * Does not reinvent flag parsing.\n  * Does not pollute your stacktraces.\n  * Tiny, no external dependencies.\n\nAPI docs: https://pkg.go.dev/github.com/mitranim/cmd.\n\n## TOC\n\n* [Guideline](#guideline)\n* [Usage Simple](#usage-simple)\n* [Usage Advanced](#usage-advanced)\n\n## Guideline\n\nStore commands in a global `cmd.Map{}`. The map may be modified by `init` functions defined in different files.\n\nUse global `flag.Parse()` and `cmd.Args()` to parse flags and obtain args. `flag.Parse()` may be called from `main` and from subcommands, multiple times, gradually consuming remaining `os.Args`.\n\n## Usage Simple\n\nCLI usage:\n\n```sh\ngo run . --help\ngo run . one\ngo run . two\n```\n\nGo code:\n\n```golang\npackage main\n\nimport (\n  \"fmt\"\n\n  \"github.com/mitranim/cmd\"\n)\n\nvar commands = cmd.Map{\n  `one`: cmdOne,\n  `two`: cmdTwo,\n}\n\nfunc main() {\n  defer cmd.Report()\n  commands.Get()()\n}\n\nfunc cmdOne() {\n  fmt.Println(`running command \"one\"`)\n}\n\nfunc cmdTwo() {\n  fmt.Println(`running command \"two\"`)\n}\n```\n\n## Usage Advanced\n\nCLI usage:\n\n```sh\ngo run . --help\ngo run . -a one -b three\ngo run . -a two -c three\n```\n\nGo code:\n\n```golang\npackage main\n\nimport (\n  \"flag\"\n  \"fmt\"\n\n  \"github.com/mitranim/cmd\"\n)\n\nvar (\n  commands = cmd.Map{}\n  flagA    = flag.Bool(`a`, false, `flag \"a\"`)\n)\n\nfunc main() {\n  flag.Parse()\n  fmt.Printf(`running with \"-a\" = %v`+\"\\n\", *flagA)\n\n  defer cmd.Report()\n  commands.Get()()\n}\n\nfunc init() { commands.Add(`one`, cmdOne) }\n\nfunc cmdOne() {\n  flagB := flag.Bool(`b`, false, `flag \"b\"`)\n\n  flag.Parse()\n\n  fmt.Printf(\n    `running command %q with \"-b\" = %v and args = %q`+\"\\n\",\n    `one`, *flagB, cmd.Args(),\n  )\n}\n\nfunc init() { commands.Add(`two`, cmdTwo) }\n\nfunc cmdTwo() {\n  flagC := flag.Bool(`c`, false, `flag \"c\"`)\n\n  flag.Parse()\n\n  fmt.Printf(\n    `running command %q with \"-c\" = %v and args = %q`+\"\\n\",\n    `two`, *flagC, cmd.Args(),\n  )\n}\n```\n\n## License\n\nhttps://unlicense.org\n\n## Misc\n\nI'm receptive to suggestions. If this library _almost_ satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Fcmd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitranim%2Fcmd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Fcmd/lists"}