{"id":16788592,"url":"https://github.com/mna/mainer","last_synced_at":"2025-04-10T23:13:57.801Z","repository":{"id":144270217,"uuid":"369315949","full_name":"mna/mainer","owner":"mna","description":"Package mainer defines types relevant to flag parsing and command entrypoint implementation.","archived":false,"fork":false,"pushed_at":"2023-01-27T02:12:39.000Z","size":57,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T23:13:49.996Z","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":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mna.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["mna"],"custom":["https://www.buymeacoffee.com/mna"]}},"created_at":"2021-05-20T19:24:41.000Z","updated_at":"2023-01-28T18:26:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"8f2dd24d-c9fb-4698-84a5-28ab39da5a6e","html_url":"https://github.com/mna/mainer","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mna%2Fmainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mna%2Fmainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mna%2Fmainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mna%2Fmainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mna","download_url":"https://codeload.github.com/mna/mainer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248312135,"owners_count":21082638,"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-10-13T08:18:23.341Z","updated_at":"2025-04-10T23:13:57.785Z","avatar_url":"https://github.com/mna.png","language":"Go","funding_links":["https://github.com/sponsors/mna","https://www.buymeacoffee.com/mna"],"categories":[],"sub_categories":[],"readme":"[![Go Reference](https://pkg.go.dev/badge/github.com/mna/mainer.svg)](https://pkg.go.dev/github.com/mna/mainer)\n[![Build Status](https://github.com/mna/mainer/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/mna/mainer/actions)\n\n# mainer\n\nPackage mainer defines types relevant to command entrypoint implementation.\nIt includes the `Stdio` struct that abstracts the I/O and working directory of\na command, and the `Parser` struct that implements a simple flag parser with\nsupport for struct tags and environment variables-based argument values.\n\n## Installation\n\n    $ go get github.com/mna/mainer\n\n## Description\n\nThe [code documentation](https://pkg.go.dev/github.com/mna/mainer) is the\ncanonical source for documentation.\n\nA typical main entrypoint looks like this, where the cmd struct could be\nimplemented in a distinct package so the main package is minimal (just\nthe main function at the end) and the command implementation is easily\ntestable:\n\n```go\ntype cmd struct {\n  Help    bool   `flag:\"h,help\"`\n  Version bool   `flag:\"v,version\"`\n  FooBar  string `flag:\"foo-bar\" env:\"FOO_BAR\"`\n}\n\nfunc (c *cmd) Validate() error {\n  // the struct may implement a Validate method, and if it does the\n  // Parser will call it once the flags are stored in the fields.\n  return nil\n}\n\nfunc (c *cmd) Main(args []string, stdio mainer.Stdio) mainer.ExitCode {\n  // parse the flags, using env var \u003cCMD\u003e_FOO_BAR for the --foo-bar\n  // flag if the flag is not set (where \u003cCMD\u003e defaults to the base name\n  // of the executable, in uppercase and without extension).\n  p := \u0026mainer.Parser{EnvVars: true}\n  if err := p.Parse(args, c); err != nil {\n    fmt.Fprintln(stdio.Stderr, err)\n    return mainer.InvalidArgs\n  }\n\n  // execute the command...\n  return mainer.Success\n}\n\nfunc main() {\n  var c cmd\n  os.Exit(int(c.Main(os.Args, mainer.CurrentStdio())))\n}\n```\n\n## Breaking changes\n\n### v0.3\n\n* Requires Go 1.19+.\n* Use `github.com/caarlos0/env/v6` as environment-variable parsing package instead of `github.com/kelseyhightower/envconfig` (`envconfig` is a great package but `env/v6` aligns better with the flags behaviour and has a more consistent struct tag usage).\n* `SetFlags` now reports set flags using a canonical flag name (the first flag defined on the field). Which of the various flag aliases was used should not matter (if it does, define distinct fields instead).\n* More data types are supported for flags (not a breaking change _per se_, but can mean that a `flag` struct tag that would've failed on older versions would now be valid).\n\n## License\n\nThe [BSD 3-Clause license](http://opensource.org/licenses/BSD-3-Clause).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmna%2Fmainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmna%2Fmainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmna%2Fmainer/lists"}