{"id":25161592,"url":"https://github.com/zamicol/jsonflag","last_synced_at":"2025-04-30T12:37:12.089Z","repository":{"id":57509861,"uuid":"73895415","full_name":"zamicol/jsonflag","owner":"zamicol","description":"JSON configs and environmental variables in addition to Go's flag package.","archived":false,"fork":false,"pushed_at":"2022-12-15T21:01:19.000Z","size":32,"stargazers_count":4,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-21T09:14:58.597Z","etag":null,"topics":["configuration-management"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/zamicol/jsonflag","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/zamicol.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}},"created_at":"2016-11-16T07:33:53.000Z","updated_at":"2024-03-14T18:46:42.000Z","dependencies_parsed_at":"2023-01-29T04:31:08.773Z","dependency_job_id":null,"html_url":"https://github.com/zamicol/jsonflag","commit_stats":null,"previous_names":["zamicol/jsonflags"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zamicol%2Fjsonflag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zamicol%2Fjsonflag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zamicol%2Fjsonflag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zamicol%2Fjsonflag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zamicol","download_url":"https://codeload.github.com/zamicol/jsonflag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237897680,"owners_count":19383727,"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":["configuration-management"],"created_at":"2025-02-09T02:35:56.597Z","updated_at":"2025-04-30T12:37:12.082Z","avatar_url":"https://github.com/zamicol.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsonflag\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/zamicol/jsonflag)](https://goreportcard.com/report/github.com/zamicol/jsonflag)\n[![GoDoc](https://godoc.org/github.com/zamicol/jsonflag?status.svg)](https://godoc.org/github.com/zamicol/jsonflag)\n\n\njsonflag is an almost drop in replacement for Go's flag package that seamlessly\nadds support for configs (JSON/JSON5), environmental variables, and CLI options.\n\nValues set by a higher precedence overwrite values set by a lower precedence,\n**CLI \u003e Env \u003e JSON \u003e Defaults**. This makes testing using CLI or Env variables\neasy.\n\nOrder of precedence:\n\n 1. Command line flags         (CLI example: `--flag1=Flag1Value`)\n 2. Environmental variables    (Env example: FLAG2=Flag2value)\n 3. JSON config values         (JSON example: `{\"flag3\": \"Flag3Value\"}`)\n 4. Default values set on flag (Go example: `flag.StringVar(\u0026config.Flag4,\n    \"Flag4Name\", \"Flag4DefaultValue\", \"Flag4Description\")`)\n\nTo overwrite a value, a CLI parameter may be set `go run main.go\n--flag1=flag1CliValue`.  \n\nEnvironmental variables may also be set via CLI, for example:\n`FLAG1=Flag1EnvValue go run main.go`, but they are lower priority than CLI\nflags.\n\n## Config Path\n\nIf not specified, the default path is `config.json5` in the current working\ndirectory. The config path can be specified in two ways:\n\n1. Via command line argument (which takes precedence):\n```bash\ngo run main.go --config=test_config.json\n```\n\n2. Programmatically in your Go code, add the line `jsonflag.Path = \"test_config.json\"`:\n```go\ntype Config struct {\n\tFlag1 string\n}\nfunc main(){\n\tvar config Config\n\tflag.StringVar(\u0026config.Flag1, \"Flag1Name\", \"Flag1DefaultValue\", \"Flag1Description\")\n\tjsonflag.Path = \"test_config.json\"\n\tjsonflag.Parse(\u0026config)\n}\n```\n\n\n## Installation\n\nGo get\n\n```bash\ngo get github.com/zamicol/jsonflag\n```\nand import:\n\n```go\nimport \"github.com/zamicol/jsonflag\"\n```\n\n\n\n## Quick Example\nExample `config.json5` file:\n```json5\n{\n\"flag1\": \"jsonFlag1\",\n\"flag2\": \"jsonFlag2\",\n\"flag3\": 3,  // Comments and trailing commas are supported in JSON5 configs and encouraged for readability. \n}\n```\n\nExample Go setup:\n```go\n// Config struct name (tag should) match the json key.  \ntype Config struct {\n\tFlag1 string\n\tFlag2 string\n\tFlag3 int\n}\n\nfunc main(){\n\tvar config Config\n\t// `flag` is still from the standard library.\n\tflag.StringVar(\u0026config.Flag1, \"Flag1Name\", \"Flag1DefaultValue\", \"Flag1Description\")\n\tflag.StringVar(\u0026config.Flag2, \"Flag2Name\", \"Flag2DefaultValue\", \"Flag2Description\")\n\tflag.IntVar(\u0026config.Flag3, \"Flag3Name\", 1, \"Flag3Description\")\n\n\t// Instead of `flag.parse`, use `jsonflag.Parse(\u0026config)` which is the only line that must be different from using `flag` normally.  \n\t\n\tjsonflag.Parse(\u0026config)\n}\n```\n\nThe default values will be overwritten by the values in the JSON config, which may further be overwritten by environmental variables and CLI parameters.\n\nExample overwriting values using an environmental variable and command line flag:\n\n```\nFLAG2=Flag2EnvValue go run main.go --flag1=Flag1CLIValue\n\n```\n\nWhich will result in the final values being used:\n\n```\nFlag1 = Flag1CLIValue   // From command line flag. \nFlag2 = Flag2EnvValue   // From environmental variable. \nFlag3 = 3               // From JSON file. \n```\n\n# Letter Casing For Flag Names\nFlag naming conventions vary by input type. \n\nCLI flag names (not values) must start with lowercase letters (e.g., --flag1).\n\nFor environmental variables, the flag’s name is converted to all uppercase, meaning environmental flag names (not values) are case insensitive.\n\nFor JSON names, this package uses Go’s json package for decoding. The JSON decoder only has access to exported fields of structs and follows its own precedence for JSON decoding:\n 1. Tags\n 2. Exact case\n 3. Case insensitive\n\nThe name set by the Go flag package may be upper or lower case, but uppercase is recommended to align with Go naming conventions for exported fields.\n\n[See also the godocs for more complete documentation and a working example.](https://godoc.org/github.com/zamicol/jsonflag)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzamicol%2Fjsonflag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzamicol%2Fjsonflag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzamicol%2Fjsonflag/lists"}