{"id":23453313,"url":"https://github.com/jonathonwebb/getopt","last_synced_at":"2026-01-12T12:48:31.150Z","repository":{"id":257820049,"uuid":"861015638","full_name":"jon-codes/getopt","owner":"jon-codes","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-23T14:18:18.000Z","size":133,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-23T15:26:31.215Z","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":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jon-codes.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":"2024-09-21T19:14:27.000Z","updated_at":"2024-12-23T14:18:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"0f4bc2d0-c73e-45d1-a2cb-733a246b3998","html_url":"https://github.com/jon-codes/getopt","commit_stats":null,"previous_names":["jon-codes/getopt"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jon-codes%2Fgetopt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jon-codes%2Fgetopt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jon-codes%2Fgetopt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jon-codes%2Fgetopt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jon-codes","download_url":"https://codeload.github.com/jon-codes/getopt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231038822,"owners_count":18318700,"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-12-24T02:01:07.157Z","updated_at":"2025-08-26T12:32:20.164Z","avatar_url":"https://github.com/jon-codes.png","language":"Go","readme":"# getopt\n[![Go Reference](https://pkg.go.dev/badge/github.com/jonathonwebb/getopt.svg)](https://pkg.go.dev/github.com/jonathonwebb/getopt)\n[![CI](https://github.com/jonathonwebb/getopt/actions/workflows/ci.yml/badge.svg)](https://github.com/jonathonwebb/getopt/actions/workflows/ci.yml)\n[![Go Report Card](https://goreportcard.com/badge/github.com/jonathonwebb/getopt)](https://goreportcard.com/report/github.com/jonathonwebb/getopt)\n[![codecov](https://codecov.io/github/jonathonwebb/getopt/graph/badge.svg?token=CF7WDJOFVY)](https://codecov.io/github/jonathonwebb/getopt)\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n\nPackage `getopt` provides a zero-dependency Go implementation of the Unix getopt function for parsing command-line options.\n\nThe `getopt` package supports parsing options using the POSIX convention, supporting short options (e.g., `-a`) and option arguments. It also supports GNU extensions, including support for long options (e.g., `--option`), options with optional arguments, and permuting non-option parameters. \n\n## Install\n\n```\ngo get github.com/jonathonwebb/getopt\n```\n\n## Usage\n\nThis package emulates the C `getopt` function, but uses a state machine to encapsulate variables (instead of the global `optind`, `optopt`, `optarg` used in C). Rather than implement a high-level interface for defining CLI flags, it aims to implement an accurate emulation of C `getopt` that can be used by higher-level tools.\n\nCollect all options into a slice:\n\n```go\nstate := getopt.NewState(os.Args)\nconfig := getopt.Config{Opts: getopt.OptStr(`ab:c::`)}\nopts, err := state.Parse(config)\n```\n\nIterate over each option for finer control:\n\n```go\nstate := getopt.NewState(os.Args)\nconfig := getopt.Config{Opts: getopt.OptStr(`ab:c::`)}\n\nfor opt, err := range state.All(config) {\n    if err != nil {\n        break\n    }\n    switch opt.Char {\n    case 'a':\n        fmt.Printf(\"Found opt a\\n\")\n    case 'b':\n        fmt.Printf(\"Found opt b with arg %s\\n\", opt.OptArg)\n    case 'c':\n        fmt.Printf(\"Found opt c\")\n        if opt.OptArg != \"\" {\n            fmt.Printf(\" with arg %s\", opt.OptArg)\n        }\n        fmt.Printf(\"\\n\")\n    }\n}\n```\n# Behavior\n\nThis package uses [GNU libc](https://www.gnu.org/software/libc/) as a reference for behavior, since many expect the\nnon-standard features it provides. This is accomplished via a C test generator that runs getopt for all functions and parsing modes.\n\nIt supports the same configuration options as the GNU options via [Mode](https://pkg.go.dev/github.com/jonathonwebb/getopt#Mode):\n  - [ModeGNU](https://pkg.go.dev/github.com/jonathonwebb/getopt#ModeGNU): enables default behavior.\n  - [ModePosix](https://pkg.go.dev/github.com/jonathonwebb/getopt#ModePosix): enables the '+' compatibility mode, disabling permuting arguments and terminating parsing on the first parameter.\n  - [ModeInOrder](https://pkg.go.dev/github.com/jonathonwebb/getopt#ModeInOrder): enables the '-' optstring prefix mode, treating all parameters as though they were arguments to an option with character code 1.\n\nThe specific libc function that is emulated can be configured via [Func](https://pkg.go.dev/github.com/jonathonwebb/getopt#Func):\n  - [FuncGetOpt](https://pkg.go.dev/github.com/jonathonwebb/getopt#FuncGetOpt): parse only traditional POSIX short options (e.g., -a).\n  - [FuncGetOptLong](https://pkg.go.dev/github.com/jonathonwebb/getopt#FuncGetOptLong): parse short options, and GNU extension long options (e.g.,\n    --option).\n  - [FuncGetOptLongOnly](https://pkg.go.dev/github.com/jonathonwebb/getopt#FuncGetOptLongOnly): parse short and long options, but allow long options to begin with a single dash (like [pkg/flag](https://pkg.go.dev/flag)).\n\nThe parser differs from GNU libc's getopt in the following ways:\n  - It accepts multi-byte runes in short and long option definitions.\n  - This package does not implement the same argument permutation as GNU libc.\n    The value of OptInd and order of arguments mid-parsing may differ, and only\n    the final order is validated against the GNU implementation.\n\n## API Documentation\n\nThe full API documentation can be found at [pkg.go.dev](https://pkg.go.dev/github.com/jonathonwebb/getopt). The API for major version `1.x.x` is stable -- any breaking changes to the API will require a new major version.\n\n## Acknowledgements\n\nThe algorithm for permuting arguments is from [musl-libc](https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT), and is used under the linked MIT License:\n\n | Copyright © 2005-2020 Rich Felker, et al.\n","funding_links":[],"categories":["Command Line","命令行","Recently Updated"],"sub_categories":["Standard CLI","标准CLI","[Dec 24, 2024](/content/2024/12/24/README.md)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathonwebb%2Fgetopt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonathonwebb%2Fgetopt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathonwebb%2Fgetopt/lists"}