{"id":13410459,"url":"https://github.com/integrii/flaggy","last_synced_at":"2025-05-15T07:06:16.000Z","repository":{"id":39614006,"uuid":"123871580","full_name":"integrii/flaggy","owner":"integrii","description":"Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.","archived":false,"fork":false,"pushed_at":"2023-04-03T05:02:33.000Z","size":277,"stargazers_count":860,"open_issues_count":19,"forks_count":32,"subscribers_count":16,"default_branch":"master","last_synced_at":"2025-04-14T12:58:47.542Z","etag":null,"topics":["cli","flags","golang","input","subcommands"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/integrii.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-05T05:55:05.000Z","updated_at":"2025-03-28T16:56:45.000Z","dependencies_parsed_at":"2024-06-18T12:28:51.851Z","dependency_job_id":"39fd87b6-d7c4-4342-aa92-34dc39fbcba4","html_url":"https://github.com/integrii/flaggy","commit_stats":{"total_commits":210,"total_committers":15,"mean_commits":14.0,"dds":0.3857142857142857,"last_synced_commit":"7f802351f0f4a4d7b6646af52302245fb0638d78"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/integrii%2Fflaggy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/integrii%2Fflaggy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/integrii%2Fflaggy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/integrii%2Fflaggy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/integrii","download_url":"https://codeload.github.com/integrii/flaggy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254292042,"owners_count":22046426,"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":["cli","flags","golang","input","subcommands"],"created_at":"2024-07-30T20:01:07.073Z","updated_at":"2025-05-15T07:06:10.991Z","avatar_url":"https://github.com/integrii.png","language":"Go","readme":"\u003cp align=\"center\"\u003e\n\n\u003cimg src=\"https://raw.githubusercontent.com/integrii/flaggy/master/logo.png\" /\u003e\n\u003cbr /\u003e\n\u003ca href=\"https://goreportcard.com/report/github.com/integrii/flaggy\"\u003e\u003cimg src=\"https://goreportcard.com/badge/github.com/integrii/flaggy\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.org/integrii/flaggy\"\u003e\u003cimg src=\"https://travis-ci.org/integrii/flaggy.svg?branch=master\"\u003e\u003c/a\u003e \n\u003ca href=\"https://pkg.go.dev/github.com/integrii/flaggy\"\u003e \u003cimg src=\"https://img.shields.io/badge/go.dev-reference-007d9c?logo=go\u0026logoColor=white\"\u003e\u003c/a\u003e\n\u003ca href=\"http://unlicense.org/\"\u003e\u003cimg src=\"https://img.shields.io/badge/license-Unlicense-blue.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://github.com/avelino/awesome-go\"\u003e\u003cimg src=\"https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://gophers.slack.com/messages/CBMUGQYRH\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/badge/slack-@gophers/flaggy-blue.svg?logo=slack\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nSensible and _fast_ command-line flag parsing with excellent support for **subcommands** and **positional values**. Flags can be at any position. Flaggy has no required project or package layout like [Cobra requires](https://github.com/spf13/cobra/issues/641), and **no external dependencies**!\n\nCheck out the [go doc](http://pkg.go.dev/github.com/integrii/flaggy), [examples directory](https://github.com/integrii/flaggy/tree/master/examples), and [examples in this readme](https://github.com/integrii/flaggy#super-simple-example) to get started quickly. You can also read the Flaggy introduction post with helpful examples [on my weblog](https://ericgreer.info/post/a-better-flags-package-for-go/).\n\n# Installation\n\n`go get -u github.com/integrii/flaggy`\n\n# Key Features\n\n- Very easy to use ([see examples below](https://github.com/integrii/flaggy#super-simple-example))\n- 35 different flag types supported\n- Any flag can be at any position\n- Pretty and readable help output by default\n- Positional subcommands\n- Positional parameters\n- Suggested subcommands when a subcommand is typo'd\n- Nested subcommands\n- Both global and subcommand specific flags\n- Both global and subcommand specific positional parameters\n- [Customizable help templates for both the global command and subcommands](https://github.com/integrii/flaggy/blob/master/examples/customTemplate/main.go)\n- Customizable appended/prepended help messages for both the global command and subcommands\n- Simple function that displays help followed by a custom message string\n- Flags and subcommands may have both a short and long name\n- Unlimited trailing arguments after a `--`\n- Flags can use a single dash or double dash (`--flag`, `-flag`, `-f`, `--f`)\n- Flags can have `=` assignment operators, or use a space (`--flag=value`, `--flag value`)\n- Flags support single quote globs with spaces (`--flag 'this is all one value'`)\n- Flags of slice types can be passed multiple times (`-f one -f two -f three`)\n- Optional but default version output with `--version`\n- Optional but default help output with `-h` or `--help`\n- Optional but default help output when any invalid or unknown parameter is passed\n- It's _fast_. All flag and subcommand parsing takes less than `1ms` in most programs.\n\n# Example Help Output\n\n```\ntestCommand - Description goes here.  Get more information at http://flaggy.\nThis is a prepend for help\n\n  Usage:\n    testCommand [subcommandA|subcommandB|subcommandC] [testPositionalA] [testPositionalB]\n\n  Positional Variables:\n    testPositionalA   Test positional A does some things with a positional value. (Required)\n    testPositionalB   Test positional B does some less than serious things with a positional value.\n\n  Subcommands:\n    subcommandA (a)   Subcommand A is a command that does stuff\n    subcommandB (b)   Subcommand B is a command that does other stuff\n    subcommandC (c)   Subcommand C is a command that does SERIOUS stuff\n\n  Flags:\n       --version        Displays the program version string.\n    -h --help           Displays help with available flag, subcommand, and positional value parameters.\n    -s --stringFlag     This is a test string flag that does some stringy string stuff.\n    -i --intFlg         This is a test int flag that does some interesting int stuff. (default: 5)\n    -b --boolFlag       This is a test bool flag that does some booly bool stuff. (default: true)\n    -d --durationFlag   This is a test duration flag that does some untimely stuff. (default: 1h23s)\n\nThis is an append for help\nThis is a help add-on message\n```\n\n# Super Simple Example\n\n`./yourApp -f test`\n\n```go\n// Declare variables and their defaults\nvar stringFlag = \"defaultValue\"\n\n// Add a flag\nflaggy.String(\u0026stringFlag, \"f\", \"flag\", \"A test string flag\")\n\n// Parse the flag\nflaggy.Parse()\n\n// Use the flag\nprint(stringFlag)\n```\n\n\n# Example with Subcommand\n\n`./yourApp subcommandExample -f test`\n\n```go\n// Declare variables and their defaults\nvar stringFlag = \"defaultValue\"\n\n// Create the subcommand\nsubcommand := flaggy.NewSubcommand(\"subcommandExample\")\n\n// Add a flag to the subcommand\nsubcommand.String(\u0026stringFlag, \"f\", \"flag\", \"A test string flag\")\n\n// Add the subcommand to the parser at position 1\nflaggy.AttachSubcommand(subcommand, 1)\n\n// Parse the subcommand and all flags\nflaggy.Parse()\n\n// Use the flag\nprint(stringFlag)\n```\n\n# Example with Nested Subcommands, Various Flags and Trailing Arguments\n\n`./yourApp subcommandExample --flag=5 nestedSubcommand -t test -y -- trailingArg`\n\n```go\n// Declare variables and their defaults\nvar stringFlagF = \"defaultValueF\"\nvar intFlagT = 3\nvar boolFlagB bool\n\n// Create the subcommands\nsubcommandExample := flaggy.NewSubcommand(\"subcommandExample\")\nnestedSubcommand := flaggy.NewSubcommand(\"nestedSubcommand\")\n\n// Add a flag to both subcommands\nsubcommandExample.String(\u0026stringFlagF, \"t\", \"testFlag\", \"A test string flag\")\nnestedSubcommand.Int(\u0026intFlagT, \"f\", \"flag\", \"A test int flag\")\n\n// add a global bool flag for fun\nflaggy.Bool(\u0026boolFlagB, \"y\", \"yes\", \"A sample boolean flag\")\n\n// attach the nested subcommand to the parent subcommand at position 1\nsubcommandExample.AttachSubcommand(nestedSubcommand, 1)\n// attach the base subcommand to the parser at position 1\nflaggy.AttachSubcommand(subcommandExample, 1)\n\n// Parse everything, then use the flags and trailing arguments\nflaggy.Parse()\nprint(stringFlagF)\nprint(intFlagT)\nprint(boolFlagB)\nprint(flaggy.TrailingArguments[0])\n```\n\n# Supported Flag Types\n\nFlaggy has specific flag types for all basic types included in go as well as a slice of any of those types.  This includes all of the following types:\n\n- string and []string\n- bool and []bool\n- all int types and all []int types\n- all float types and all []float types\n- all uint types and all []uint types\n\nOther more specific types can also be used as flag types.  They will be automatically parsed using the standard parsing functions included with those types in those packages.  This includes:\n\n- net.IP\n- []net.IP\n- net.HardwareAddr\n- []net.HardwareAddr\n- net.IPMask\n- []net.IPMask\n- time.Duration\n- []time.Duration\n\n# An Example Program\n\nBest practice when using flaggy includes setting your program's name, description, and version (at build time) as shown in this example program.\n\n```go\npackage main\n\nimport \"github.com/integrii/flaggy\"\n\n// Make a variable for the version which will be set at build time.\nvar version = \"unknown\"\n\n// Keep subcommands as globals so you can easily check if they were used later on.\nvar mySubcommand *flaggy.Subcommand\n\n// Setup the variables you want your incoming flags to set.\nvar testVar string\n\n// If you would like an environment variable as the default for a value, just populate the flag\n// with the value of the environment by default.  If the flag corresponding to this value is not\n// used, then it will not be changed.\nvar myVar = os.Getenv(\"MY_VAR\")\n\n\nfunc init() {\n  // Set your program's name and description.  These appear in help output.\n  flaggy.SetName(\"Test Program\")\n  flaggy.SetDescription(\"A little example program\")\n\n  // You can disable various things by changing bools on the default parser\n  // (or your own parser if you have created one).\n  flaggy.DefaultParser.ShowHelpOnUnexpected = false\n\n  // You can set a help prepend or append on the default parser.\n  flaggy.DefaultParser.AdditionalHelpPrepend = \"http://github.com/integrii/flaggy\"\n  \n  // Add a flag to the main program (this will be available in all subcommands as well).\n  flaggy.String(\u0026testVar, \"tv\", \"testVariable\", \"A variable just for testing things!\")\n\n  // Create any subcommands and set their parameters.\n  mySubcommand = flaggy.NewSubcommand(\"mySubcommand\")\n  mySubcommand.Description = \"My great subcommand!\"\n  \n  // Add a flag to the subcommand.\n  mySubcommand.String(\u0026myVar, \"mv\", \"myVariable\", \"A variable just for me!\")\n\n  // Set the version and parse all inputs into variables.\n  flaggy.SetVersion(version)\n  flaggy.Parse()\n}\n\nfunc main(){\n    if mySubcommand.Used {\n      ...\n    }\n}\n```\n\nThen, you can use the following build command to set the `version` variable in the above program at build time.\n\n```bash\n# build your app and set the version string\n$ go build -ldflags='-X main.version=1.0.3-a3db3'\n$ ./yourApp version\nVersion: 1.0.3-a3db3\n$ ./yourApp --help\nTest Program - A little example program\nhttp://github.com/integrii/flaggy\n```\n\n# Contributions\n\nPlease feel free to open an issue if you find any bugs or see any features that make sense. Pull requests will be reviewed and accepted if they make sense, but it is always wise to submit a proposal issue before any major changes.\n","funding_links":[],"categories":["命令行工具","Misc","开源类库","Command Line","Build Automation","Open source library","命令行","Go","命令行工具### 标准 CLI`用于创建一个标准命令行应用程序的库`"],"sub_categories":["标准 CLI","命令行","Standard CLI","Command Line","标准CLI"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintegrii%2Fflaggy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fintegrii%2Fflaggy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fintegrii%2Fflaggy/lists"}