{"id":13365315,"url":"https://github.com/urfave/sflags","last_synced_at":"2025-08-18T04:04:46.296Z","repository":{"id":48448129,"uuid":"75543672","full_name":"urfave/sflags","owner":"urfave","description":"Generate flags by parsing structures","archived":false,"fork":false,"pushed_at":"2025-07-18T03:30:27.000Z","size":322,"stargazers_count":162,"open_issues_count":4,"forks_count":37,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-07-18T07:24:13.822Z","etag":null,"topics":["cli-library","flag","go"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/urfave.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-12-04T14:49:27.000Z","updated_at":"2025-07-18T03:30:31.000Z","dependencies_parsed_at":"2024-11-11T21:29:49.755Z","dependency_job_id":"c845b3f6-36ca-44e4-9a52-616fe51443ea","html_url":"https://github.com/urfave/sflags","commit_stats":null,"previous_names":["urfave/sflags","octago/sflags"],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/urfave/sflags","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urfave%2Fsflags","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urfave%2Fsflags/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urfave%2Fsflags/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urfave%2Fsflags/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/urfave","download_url":"https://codeload.github.com/urfave/sflags/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/urfave%2Fsflags/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270940719,"owners_count":24671687,"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-08-18T02:00:08.743Z","response_time":89,"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-library","flag","go"],"created_at":"2024-07-30T00:01:04.651Z","updated_at":"2025-08-18T04:04:46.262Z","avatar_url":"https://github.com/urfave.png","language":"Go","funding_links":[],"categories":["命令行","Command Line"],"sub_categories":["标准命令行交互","Standard CLI","標準命令行交互"],"readme":"# Flags based on structures [![GoDoc](https://godoc.org/github.com/urfave/sflags?status.svg)](http://godoc.org/github.com/urfave/sflags)  [![Build Status](https://img.shields.io/github/check-runs/urfave/sflags/main?label=build%20status)](https://github.com/urfave/sflags/actions?query=branch%3Amain)  [![codecov](https://codecov.io/gh/urfave/sflags/branch/master/graph/badge.svg)](https://codecov.io/gh/urfave/sflags)  [![Go Report Card](https://goreportcard.com/badge/github.com/urfave/sflags)](https://goreportcard.com/report/github.com/urfave/sflags)\n\nThe sflags package uses structs, reflection and struct field tags\nto allow you specify command line options. It supports [different types](#supported-types-in-structures) and [features](#features).\n\nAn example:\n\n```golang\ntype HTTPConfig struct {\n\tHost    string        `desc:\"HTTP host\"`\n\tPort    int           `flag:\"port p\" desc:\"some port\"`\n\tSSL     bool          `env:\"HTTP_SSL_VALUE\"`\n\tTimeout time.Duration `flag:\",deprecated,hidden\"`\n}\n\ntype Config struct {\n\tHTTP  HTTPConfig\n\tStats StatsConfig\n}\n```\n\nAnd you can use your favorite flag or cli library!\n\n## Supported libraries and features:\n\n|     |     | Hidden | Deprecated | Short | Env | Required |\n| --- | --- |:------:|:----------:|:-----:|:---:|:--------:|\n| \u003cul\u003e\u003cli\u003e[x] [flag]\u003c/li\u003e\u003cul\u003e | [example](./examples/flag/main.go) | `-` | `-` | `-` | `-` | `-` |\n| \u003cul\u003e\u003cli\u003e[x] [kingpin]\u003c/li\u003e\u003c/ul\u003e | [example](./examples/kingpin/main.go) | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[ ] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e |\n| \u003cul\u003e\u003cli\u003e[x] [spf13/pflag]\u003c/li\u003e\u003c/ul\u003e | [example](./examples/pflag/main.go) | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | `-` | `-` |\n| \u003cul\u003e\u003cli\u003e[x] [spf13/cobra]\u003c/li\u003e\u003c/ul\u003e | [example](./examples/cobra/main.go) | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | `-` | `-` |\n| \u003cul\u003e\u003cli\u003e[x] [urfave/cli]\u003c/li\u003e\u003c/ul\u003e | [example](./examples/urfave_cli/main.go) | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | `-` | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e | \u003cul\u003e\u003cli\u003e[x] \u003c/li\u003e\u003c/ul\u003e |\n\n- [x] - feature is supported and implemented\n\n`-` - feature can't be implemented for this cli library\n\n\n[flag]: https://golang.org/pkg/flag/\n[spf13/pflag]: https://github.com/spf13/pflag\n[spf13/cobra]: https://github.com/spf13/cobra\n[spf13/viper]: https://github.com/spf13/viper\n[urfave/cli]: https://github.com/urfave/cli\n[kingpin]: https://github.com/alecthomas/kingpin\n\n## Features:\n\n - [x] Set environment name\n - [x] Set usage\n - [x] Long and short forms\n - [x] Skip field\n - [x] Required\n - [ ] Placeholders (by `name`)\n - [x] Deprecated and hidden options\n - [x] Multiple ENV names\n - [x] Interface for user types.\n - [x] [Validation](https://godoc.org/github.com/urfave/sflags/validator/govalidator#New) (using [govalidator](https://github.com/asaskevich/govalidator) package)\n - [x] Anonymous nested structure support (anonymous structures flatten by default)\n\n## Supported types in structures:\n\n - [x] `int`, `int8`, `int16`, `int32`, `int64`\n - [x] `uint`, `uint8`, `uint16`, `uint32`, `uint64`\n - [x] `float32`, `float64`\n - [x] slices for all previous numeric types (e.g. `[]int`, `[]float64`)\n - [x] `bool`\n - [x] `[]bool`\n - [x] `string`\n - [x] `[]string`\n - [x] nested structures\n - [x] net.TCPAddr\n - [x] net.IP\n - [x] time.Duration\n - [x] regexp.Regexp\n - [x] map for all previous types (e.g. `map[int64]bool`, `map[string]float64`)\n\n## Custom types:\n - [x] HexBytes\n\n - [x] count\n - [ ] ipmask\n - [ ] enum values\n - [ ] enum list values\n - [ ] file\n - [ ] file list\n - [ ] url\n - [ ] url list\n - [ ] units (bytes 1kb = 1024b, speed, etc)\n\n## Example:\n\nThe code below shows how to use `sflags` with the [flag] library. Examples for other\nflag libraries are available from [./examples](./examples) dir.\n\n```golang\npackage main\n\nimport (\n\t\"flag\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/urfave/sflags/gen/gflag\"\n)\n\ntype httpConfig struct {\n\tHost    string `desc:\"HTTP host\"`\n\tPort    int\n\tSSL     bool\n\tTimeout time.Duration\n}\n\ntype config struct {\n\tHTTP httpConfig\n}\n\nfunc main() {\n\tcfg := \u0026config{\n\t\tHTTP: httpConfig{\n\t\t\tHost:    \"127.0.0.1\",\n\t\t\tPort:    6000,\n\t\t\tSSL:     false,\n\t\t\tTimeout: 15 * time.Second,\n\t\t},\n\t}\n\terr := gflag.ParseToDef(cfg)\n\tif err != nil {\n\t\tlog.Fatalf(\"err: %v\", err)\n\t}\n\tflag.Parse()\n}\n```\n\nThat code generates next output:\n```sh\n$ go run ./main.go --help\nUsage of _obj/exe/main:\n  -http-host value\n    \tHTTP host (default 127.0.0.1)\n  -http-port value\n    \t (default 6000)\n  -http-ssl\n\n  -http-timeout value\n    \t (default 15s)\nexit status 2\n```\n\n## Options for flag tag\n\nThe flag default key string is the struct field name but can be specified in the struct field's tag value.\nThe \"flag\" key in the struct field's tag value is the key name, followed by an optional comma and options. Examples:\n```golang\n// Field is ignored by this package.\nField int `flag:\"-\"`\n\n// Field appears in flags as \"myName\".\nField int `flag:\"myName\"`\n\n// If this field is from nested struct, prefix from parent struct will be ingored.\nField int `flag:\"~myName\"`\n\n// You can set short name for flags by providing it's value after a space\n// Prefixes will not be applied for short names.\nField int `flag:\"myName a\"`\n\n// this field will be removed from generated help text.\nField int `flag:\",hidden\"`\n\n// this field will be marked as deprecated in generated help text\nField int `flag:\",deprecated\"`\n```\n\n## Options for desc tag\nIf you specify description in description tag (`desc` by default) it will be used in USAGE section.\n\n```golang\nAddr string `desc:\"HTTP host\"`\n````\nThis description produces something like:\n```sh\n  -addr value\n    \tHTTP host (default 127.0.0.1)\n```\n\n## Options for env tag\nIf you specify environment variable name in `env` tag, it will be used to set the value of the field.\n```golang\nSSL     bool          `env:\"HTTP_SSL_VALUE\"`\n```\n\n## Options for Parse function:\n\n```golang\n// DescTag sets custom description tag. It is \"desc\" by default.\nfunc DescTag(val string)\n\n// FlagTag sets custom flag tag. It is \"flag\" be default.\nfunc FlagTag(val string)\n\n// Prefix sets prefix that will be applied for all flags (if they are not marked as ~).\nfunc Prefix(val string)\n\n// EnvPrefix sets prefix that will be applied for all environment variables (if they are not marked as ~).\nfunc EnvPrefix(val string)\n\n// FlagDivider sets custom divider for flags. It is dash by default. e.g. \"flag-name\".\nfunc FlagDivider(val string)\n\n// EnvDivider sets custom divider for environment variables.\n// It is underscore by default. e.g. \"ENV_NAME\".\nfunc EnvDivider(val string)\n\n// Validator sets validator function for flags.\n// Check existed validators in sflags/validator package.\nfunc Validator(val ValidateFunc)\n\n// Set to false if you don't want anonymous structure fields to be flattened.\nfunc Flatten(val bool)\n\n// InheritHidden sets if fields should inherit the value of the hidden tag from parent structs.\nfunc InheritHidden()\n\n// InheritDeprecated sets if fields should inherit the value of the deprecated tag from parent structs.\nfunc InheritDeprecated()\n```\n\n\n## Known issues\n\n - kingpin doesn't pass value for boolean arguments. Counter can't get initial value from arguments.\n \n## Similar projects\n\n * https://github.com/jaffee/commandeer\n * https://github.com/anacrolix/tagflag\n * https://github.com/jessevdk/go-flags\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Furfave%2Fsflags","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Furfave%2Fsflags","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Furfave%2Fsflags/lists"}