{"id":16933574,"url":"https://github.com/yah01/cyflag","last_synced_at":"2025-03-21T03:43:38.535Z","repository":{"id":57517901,"uuid":"232373428","full_name":"yah01/cyflag","owner":"yah01","description":"flag and CLI arguments parser by go","archived":false,"fork":false,"pushed_at":"2020-04-13T17:32:52.000Z","size":1474,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-26T00:25:12.402Z","etag":null,"topics":[],"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/yah01.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":"2020-01-07T17:01:10.000Z","updated_at":"2020-04-13T17:32:55.000Z","dependencies_parsed_at":"2022-09-06T08:03:03.086Z","dependency_job_id":null,"html_url":"https://github.com/yah01/cyflag","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yah01%2Fcyflag","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yah01%2Fcyflag/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yah01%2Fcyflag/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yah01%2Fcyflag/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yah01","download_url":"https://codeload.github.com/yah01/cyflag/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244734095,"owners_count":20501014,"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-13T20:49:48.884Z","updated_at":"2025-03-21T03:43:38.503Z","avatar_url":"https://github.com/yah01.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cyflag\n\n## README.md\n- [English](README.md)\n- [中文](README.zh_CN.md)\n\n*cyflag* is a lib for parsing flags(CLI arguments, or other arguments represented by string), which being used frequently in CLI(Command-Line Interface). it's easier to use and has less limitation than the go standard lib *flag*.\n\n## QuickStart\n```go\nvar (\n\t\tparser cyflag.Parser\n\t\targs   = `i love \"cyber flag\" -best -times 95`\n\n\t\tbest  bool\n\t\ttimes int\n\t\tlove  string\n\t)\n\n\tparser.Bind(\u0026best, \"-best\", false, \"whether the best\")\n\tparser.Bind(\u0026times, \"-times\", 0, \"-times [int]\")\n\tparser.Bind(\u0026love, \"love\", \"something\", \"love [string]\")\n\n\tparser.ParseString(args)\n\n\tfmt.Printf(\"best: %+v\\n\"+\n\t\t\"times: %+v\\n\"+\n\t\t\"love: %+v\\n\",\n\t\tbest, times, love)\n```\nThe parameters of the methods above are:\n1. the address of the variable\n2. the flag name\n3. default value\n4. usage information\n\nSample above will output\n```yaml\nbest: true\ntimes: 95\nlove: cyber flag\n```\n\n## Usage\n\n*cyflag* looks like go standard package [flag](https://golang.org/pkg/flag/), as well the usage.\n\n### Bind variable with flag\n*cyflag* could bind 5 kinds of variable:\n- bool\n- int (and int8, int16, ...)\n- uint (and uint8,uint16, ...)\n- float64 (and float32)\n- string\n\nThey are all similar, and there are 5 methods for binding:\n```go\nparser.BoolVar(\u0026boolVariable,\"-bool\",false,\"it's a bool flag\")\nparser.IntVar(\u0026intVariable,\"-int\",0,\"it's a int flag\")\nparser.UintVar(\u0026uintVariable,\"-uint\",0,\"it's a uint flag\")\nparser.FloatVar(\u0026float64Variable,\"-float64\",0.0,\"it's a float64 flag\")\nparser.StringVar(\u0026stringVariable,\"-string\",\"empty\",\"it's a string flag\")\n```\n\nFor convenience, cyflag provides a method for binding variable of every type, and it's the only way to bind the variable of type which is not default type of cyflog(int32, int64, float32, ...).\n```go\nparser.Bind(\u0026variable,\"-anytype\",defaultValue,\"the usage\")\n```\nBind() method would panic when the types of variable and default value don't match, they needn't be completely same, int and int64 is accepted, but signed type and unsigned type aren't accepted.\n\n**The flag does not have to start with character '-'**, it's different with go standard lib *flag* that adding '-' automatically.\n\n### Parse\n**after all bindings having finished**, just call\n```go\nparser.Parse(args)\n```\nIf args == nil, the parser will try to parse parser.LeftArgs, so you can store arguments into parser.LeftArgs and call Parse with a parameter nil.\n\n*cyflag* will parse the arguments and store the value into the binding variables, then store the arguments it can't parse into parser.LeftArgs. For parsing CLI arguments, the arguments that can't be parsed would be stored into the cyflag.Args. At the beginning, cyflag.Args==os.Args[1:]\n\n**Notice:** The arguments format rule is a little different with go standard lib *flag*:\n- *flagname*: only way to parse bool variable\n- *flagname value*: only way to parse non-bool variable\nFor string flag, quotes the argument which contains space with ' ' or \" \". \n\n**There is no restriction in the order of arguments and flags**, which is totally different with go standard lib *flag*, the latter parses until meet first non-flag arguments.\n\nFor parsing `os.Args[1:]`, just call cyflag.BoolVar(...) (or any other binding function), and then call cyflag.Parse().\n\n## TODO\n- Now cyflag supports such many types for using reflect, reflect is slow, there may should be some fast binding and parsing methods that only use type assertion.\n- string parser should support string with space.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyah01%2Fcyflag","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyah01%2Fcyflag","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyah01%2Fcyflag/lists"}