{"id":37127967,"url":"https://github.com/robotism/flagger","last_synced_at":"2026-01-14T14:53:46.309Z","repository":{"id":278698915,"uuid":"935859956","full_name":"robotism/flagger","owner":"robotism","description":"structured flags in go command;  命令行参数结构化;","archived":false,"fork":false,"pushed_at":"2025-04-05T03:50:55.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-05T04:24:51.290Z","etag":null,"topics":["cli","cmd","cobra","command","flagger","flags","go","golang","pfalg","pflag","structed","structure","viper"],"latest_commit_sha":null,"homepage":"","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/robotism.png","metadata":{"files":{"readme":"README.cn.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":"2025-02-20T06:14:49.000Z","updated_at":"2025-04-05T03:50:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"800144dd-0157-4ea8-b6de-41183a939106","html_url":"https://github.com/robotism/flagger","commit_stats":null,"previous_names":["robotism/flagger"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/robotism/flagger","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotism%2Fflagger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotism%2Fflagger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotism%2Fflagger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotism%2Fflagger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robotism","download_url":"https://codeload.github.com/robotism/flagger/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robotism%2Fflagger/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28424013,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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","cmd","cobra","command","flagger","flags","go","golang","pfalg","pflag","structed","structure","viper"],"created_at":"2026-01-14T14:53:45.658Z","updated_at":"2026-01-14T14:53:46.292Z","avatar_url":"https://github.com/robotism.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Flagger\n\n[English](README.md) | [中文](README.cn.md)\n\nFlagger 是一个基于 [`Viper`](https://github.com/spf13/viper) 和 [`Pflag`](https://github.com/spf13/pflag) 的用于结构化参数的库。\n\n## 使用\n\n```bash\ngo get github.com/robotism/flagger\n```\n\n## 使用示例 [example/cmd/root.go](example/cmd/root.go)\n\n- 参数结构体\n\n  ```go\n  type AppConfig struct {\n      Debug    bool   `mapstructure:\"debug\" short:\"d\" description:\"debug mode\" default:\"false\"`\n      Timezone string `mapstructure:\"timezone\" description:\"timezone\" default:\"UTC\"`\n      Server Server `mapstructure:\"server\" group:\"server\"`\n      Database map[string]Database `mapstructure:\"database\" group:\"database\" mapkey:\"\u003cdbkey\u003e\"`\n  }\n\n  type Server struct {\n      Port int `mapstructure:\"port\" description:\"port\" default:\"8080\"`\n  }\n\n  type Database struct {\n      Host string `mapstructure:\"host\" description:\"host\" default:\"localhost\"`\n      Port int    `mapstructure:\"port\" description:\"port\" default:\"3306\"`\n      User string `mapstructure:\"user\" description:\"user\" default:\"root\"`\n      Pass string `mapstructure:\"pass\" description:\"pass\"`\n  }\n  ```\n\n- 命令初始化\n\n  ```go\n  package cmd\n\n  import (\n      \"log\"\n      \"os\"\n\n      \"github.com/robotism/flagger\"\n      \"github.com/robotism/flagger/example/config\"\n      \"github.com/spf13/cobra\"\n  )\n\n  var (\n      f = flagger.New()\n      c = \u0026config.AppConfig{}\n  )\n\n  // rootCmd represents the base command when called without any subcommands\n  var rootCmd = \u0026cobra.Command{\n      Use:   \"example\",\n      Short: \"a flagger example\",\n      Long:  `a flagger example`,\n      Run: func(cmd *cobra.Command, args []string) {\n          log.Printf(\"%+v\\n\", c)\n      },\n  }\n\n  func Execute() {\n      err := rootCmd.Execute()\n      if err != nil {\n          os.Exit(1)\n      }\n  }\n\n  func init() {\n      f.UseFlags(rootCmd.Flags())\n      f.UseConfigFileArgDefault()\n      f.Parse(c)\n  }\n\n\n  ```\n\n- 运行: 打印帮助\n\n  ```bash\n  \u003e go run main.go -h\n\n  a flagger example\n\n  Usage:\n  example [flags]\n\n  Flags:\n  -c, --config string                  config file path\n      --database.\u003cdbkey\u003e.host string   host (default \"localhost\")\n      --database.\u003cdbkey\u003e.pass string   pass\n      --database.\u003cdbkey\u003e.port int      port (default 3306)\n      --database.\u003cdbkey\u003e.user string   user (default \"root\")\n  -d, --debug                          debug mode\n  -h, --help                           help for example\n      --server.port int                port (default 8080)\n      --timezone string                timezone (default \"UTC\")\n\n  ```\n\n- 运行: 使用环境变量和命令行参数\n\n  ```bash\n  \u003e set SERVER_PORT=9999\n  \u003e go run main.go -d=true --timezone=Asia/Shanghai --database.default.host=127.0.0.1\n\n  2025/02/20 14:07:31 \u0026{Debug:true Timezone:Asia/Shanghai Server:{Port:9999} Database:map[default:{Host:127.0.0.1 Port:3306 User:root Pass:}]}\n  ```\n\n- 运行: 使用配置文件\n\n  ```bash\n  go run main.go -d=true --timezone=Asia/Shanghai --database.default.host=127.0.0.1 -c config.yaml\n\n  2025/02/20 14:07:59 \u0026{Debug:true Timezone:Asia/Shanghai Server:{Port:9999} Database:map[default:{Host:127.0.0.1 Port:4000 User:root Pass:12345678}]}\n\n  go run main.go -c config.yaml\n\n  2025/02/20 14:08:14 \u0026{Debug:true Timezone:UTC Server:{Port:9999} Database:map[default:{Host:xxx.xxx.xxx.xxx Port:4000 User:root Pass:12345678}]}\n\n  ```\n\n## 加载优先级:\n\n- 命令行标志（Flags）：如果在命令行中提供了某个配置项的值，会优先使用该值。\n- 环境变量（Environment Variables）：如果命令行中未提供该配置项的值，会尝试从环境变量中获取。\n- 配置文件（Config File）：如果环境变量中也未找到该配置项的值，会从配置文件中加载。\n- 默认值（Default Values）：如果上述三者都未提供该配置项的值，会使用预先设置的默认值。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotism%2Fflagger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobotism%2Fflagger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobotism%2Fflagger/lists"}