{"id":19900529,"url":"https://github.com/fredbi/gflag","last_synced_at":"2026-01-26T12:42:29.800Z","repository":{"id":64299329,"uuid":"568769793","full_name":"fredbi/gflag","owner":"fredbi","description":"go-generics implementation of CLI flags","archived":false,"fork":false,"pushed_at":"2023-09-29T14:27:58.000Z","size":55,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T04:11:24.259Z","etag":null,"topics":["cli","flags"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fredbi.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":".github/CODEOWNERS","security":null,"support":null,"governance":null}},"created_at":"2022-11-21T11:23:17.000Z","updated_at":"2024-07-20T16:46:00.000Z","dependencies_parsed_at":"2023-01-15T09:15:19.697Z","dependency_job_id":"a94d08bf-0f5c-485f-95e5-103655c3582b","html_url":"https://github.com/fredbi/gflag","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/fredbi/gflag","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredbi%2Fgflag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredbi%2Fgflag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredbi%2Fgflag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredbi%2Fgflag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fredbi","download_url":"https://codeload.github.com/fredbi/gflag/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fredbi%2Fgflag/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275857828,"owners_count":25541040,"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","status":"online","status_checked_at":"2025-09-18T02:00:09.552Z","response_time":77,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","flags"],"created_at":"2024-11-12T20:12:34.818Z","updated_at":"2025-09-19T00:32:39.106Z","avatar_url":"https://github.com/fredbi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Lint](https://github.com/fredbi/gflag/actions/workflows/01-golang-lint.yaml/badge.svg)\n![CI](https://github.com/fredbi/gflag/actions/workflows/02-test.yaml/badge.svg)\n[![Coverage Status](https://coveralls.io/repos/github/fredbi/gflag/badge.svg)](https://coveralls.io/github/fredbi/gflag)\n![Vulnerability Check](https://github.com/fredbi/gflag/actions/workflows/03-govulncheck.yaml/badge.svg)\n[![Go Report Card](https://goreportcard.com/badge/github.com/fredbi/gflag)](https://goreportcard.com/report/github.com/fredbi/gflag)\n\n![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/fredbi/gflag)\n[![Go Reference](https://pkg.go.dev/badge/github.com/fredbi/gflag.svg)](https://pkg.go.dev/github.com/fredbi/gflag)\n[![license](http://img.shields.io/badge/license/License-Apache-yellow.svg)](https://raw.githubusercontent.com/fredbi/go-cli/master/LICENSE.md)\n\n# gflag\n\n`pflags` with generic types.\n\n\u003e OK so this is yet another CLI flags library...\n\u003e\n\u003e We do not reinvent the wheel: this module reuses the great package `github.com/spf13/pflag`,\n\u003e with an interface built on go generics.\n\u003e\n\u003e The main idea is to simplify the `pflag` interface, with less things to remember about flag types.\n\u003e\n\u003e So this is not a fork or drop-in replacement, but rather an extension of the `pflag` functionality.\n\n## Usage\n\nThis package is designed to be used together with `github.com/spf13/pflag`.\nAll types created by `gflag` implement the `pflag.Value` interface.\n\nUse-case: build idiomatic `cobra` CLIs with a simpler declaration of flags.\nSee how [go-cli](https://github.com/fredbi/go-cli) achieves this.\n\n```go\nimport (\n\t\"fmt\"\n\n\t\"github.com/fredbi/gflag\"\n\t\"github.com/spf13/pflag\"\n)\n\nvar flagVal int\n\nfs := pflag.NewFlagSet(\"\", pflag.ContinueOnError)\nintFlag := gflag.NewFlagValue(\u0026flagVal, 1)      // infer flag from underlying type int, with a default value\n\nfs.Var(intFlag, \"integer\",  \"integer value\")    // register the flag in pflag flagset\n\n_ = fs.Parse([]string{\"--integer\", 10})         // parse command line arguments\n\nfmt.Println(intFlag.GetValue())                 // the flag knows the type of the value\n```\n\nWith `pflag` this piece of code would look very much similar.\n\n```go\nimport (\n\t\"fmt\"\n\n\t\"github.com/spf13/pflag\"\n)\n\nvar flagVal int\n\nfs := pflag.NewFlagSet(\"\", pflag.ContinueOnError)\n\nfs.IntVar(\u0026flagVal, \"integer\",  \"integer value\") // register the flag in pflag flagset\n\n_ = fs.Parse([]string{\"--integer\", 10})         // parse command line arguments\n\nfmt.Println(fs.GetInt(\"integer\"))               // has to know an integer type is expected\n```\n\nYou may take a look at [more examples](example_values_test.go), with slices and maps.\n\n## What does this bring to the table?\n\nThis package provides a unified approach to strongly typed flags, using go generics.\n\nThis way, we no longer have to register CLI flags using dozen of type-specific methods, just three:\n* `NewFlagValue()`: for scalar values\n* `NewFlagSliceValue()`: for flags as lists\n* `NewFlagMapValue()`: for flags as map (e.g. key=value pairs)\n\nThe flag building logic is **consistent** for single values, slices and maps of all types.\nDifferences in semantics are handled with a set of `Option`s.\n\n* **All primitive types** (yes, complex too!) are supported.\n* All common types handled by `pflag` (`time.Duration`, `net.IP`, etc..) are also supported.\n* I have also added `time.Time`\n* Support any extension built for `pflag` (arbitrary types with the `pflag.Value` interface)\n\n\nVariations in the semantics for a flag with a given underlying type may be fine-tuned with options.\n\nThere is an `extensions` sub-package to contribute some custom extra flag types. \nIt is now populated with a `byte-size` flag. See [the example](extensions/example_test.go).\n\n## [About CLI flags](./docs/about.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredbi%2Fgflag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffredbi%2Fgflag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffredbi%2Fgflag/lists"}