{"id":13410655,"url":"https://github.com/bobg/subcmd","last_synced_at":"2026-03-03T04:55:36.751Z","repository":{"id":41244088,"uuid":"283529901","full_name":"bobg/subcmd","owner":"bobg","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-03T15:50:20.000Z","size":47,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-07-31T20:43:36.208Z","etag":null,"topics":[],"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/bobg.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-07-29T15:04:00.000Z","updated_at":"2024-07-03T15:49:19.000Z","dependencies_parsed_at":"2024-01-08T14:30:52.888Z","dependency_job_id":"14351f36-b82d-409a-ba9a-cffc4dadcc11","html_url":"https://github.com/bobg/subcmd","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobg%2Fsubcmd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobg%2Fsubcmd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobg%2Fsubcmd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bobg%2Fsubcmd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bobg","download_url":"https://codeload.github.com/bobg/subcmd/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243610324,"owners_count":20318941,"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:08.244Z","updated_at":"2026-03-03T04:55:31.713Z","avatar_url":"https://github.com/bobg.png","language":"Go","readme":"# Subcmd - command-line interfaces with subcommands and flags\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/bobg/subcmd/v2.svg)](https://pkg.go.dev/github.com/bobg/subcmd/v2)\n[![Go Report Card](https://goreportcard.com/badge/github.com/bobg/subcmd/v2)](https://goreportcard.com/report/github.com/bobg/subcmd/v2)\n[![Tests](https://github.com/bobg/subcmd/actions/workflows/go.yml/badge.svg)](https://github.com/bobg/subcmd/actions/workflows/go.yml)\n[![Coverage Status](https://coveralls.io/repos/github/bobg/subcmd/badge.svg?branch=master)](https://coveralls.io/github/bobg/subcmd?branch=master)\n\nThis is subcmd,\na Go package for writing command-line programs that require flag parsing\nand that have “subcommands” that also require flag parsing.\n\nUse it when you want your program to parse command lines that look like this:\n\n```\ncommand -globalopt subcommand -subopt1 FOO -subopt2 ARG1 ARG2\n```\n\nSubcommands may have sub-subcommands and so on.\nSubcommands may also be implemented as separate executables.\n\nThis is a layer on top of the standard Go `flag` package.\n\n## Usage\n\n```go\nimport (\n  \"context\"\n  \"database/sql\"\n  \"flag\"\n\n  \"github.com/bobg/subcmd/v2\"\n)\n\nfunc main() {\n  // Parse global flags normally.\n  dbname := flag.String(\"db\", \"\", \"database connection string\")\n  flag.Parse()\n\n  db, err := sql.Open(dbdriver, *dbname)\n  if err != nil { ... }\n\n  // Stash global options in a top-level command object.\n  c := command{db: db}\n\n  // Run the subcommand given in the remainder of the command line.\n  err = subcmd.Run(context.Background(), c, flag.Args())\n  if err != nil { ... }\n}\n\n// The top-level command object.\ntype command struct {\n  db *sql.DB\n}\n\n// To be used in subcmd.Run above, `command` must implement this method.\nfunc (c command) Subcmds() subcmd.Map {\n  return subcmd.Commands(\n    // The \"list\" subcommand takes one flag, -reverse.\n    \"list\", c.list, \"list employees\", subcmd.Params(\n      \"-reverse\", subcmd.Bool, false, \"reverse order of list\",\n    ),\n\n    // The \"add\" subcommand takes no flags but one positional argument.\n    \"add\", c.add, \"add new employee\", subcmd.Params(\n      \"name\", subcmd.String, \"\", \"employee name\",\n    )\n  )\n}\n\n// Implementation of the \"list\" subcommand.\n// The value of the -reverse flag is passed as an argument.\nfunc (c command) list(ctx context.Context, reverse bool, _ []string) error {\n  query := \"SELECT name FROM employees ORDER BY name\"\n  if reverse {\n    query += \" DESC\"\n  }\n  rows, err := c.db.QueryContext(ctx, query)\n  if err != nil { ... }\n  defer rows.Close()\n  for rows.Next() { ... }\n  return rows.Err()\n}\n\n// Implementation of the \"add\" subcommand.\nfunc (c command) add(ctx context.Context, name string, _ []string) error {\n  _, err := c.db.ExecContext(ctx, \"INSERT INTO employees (name) VALUES ($1)\", name)\n  return err\n}\n```\n","funding_links":[],"categories":["Command Line","命令行","Build Automation"],"sub_categories":["Standard CLI","标准CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobg%2Fsubcmd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbobg%2Fsubcmd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbobg%2Fsubcmd/lists"}