{"id":29019026,"url":"https://github.com/mozzzzy/arguments","last_synced_at":"2025-07-03T17:36:21.155Z","repository":{"id":57505309,"uuid":"230724662","full_name":"mozzzzy/arguments","owner":"mozzzzy","description":"Argument option parser for go. This is object base wrapper of the standard flag package.","archived":false,"fork":false,"pushed_at":"2020-08-09T13:58:57.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2023-08-12T07:18:24.200Z","etag":null,"topics":["args","argument-parser","flag","go","golang","option-parser","options","parse"],"latest_commit_sha":null,"homepage":null,"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/mozzzzy.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":"2019-12-29T08:39:54.000Z","updated_at":"2020-08-09T13:58:59.000Z","dependencies_parsed_at":"2022-08-22T08:50:26.617Z","dependency_job_id":null,"html_url":"https://github.com/mozzzzy/arguments","commit_stats":null,"previous_names":[],"tags_count":3,"template":null,"template_full_name":null,"purl":"pkg:github/mozzzzy/arguments","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozzzzy%2Farguments","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozzzzy%2Farguments/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozzzzy%2Farguments/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozzzzy%2Farguments/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mozzzzy","download_url":"https://codeload.github.com/mozzzzy/arguments/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mozzzzy%2Farguments/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261973724,"owners_count":23238585,"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":["args","argument-parser","flag","go","golang","option-parser","options","parse"],"created_at":"2025-06-26T00:07:11.920Z","updated_at":"2025-06-26T00:07:12.754Z","avatar_url":"https://github.com/mozzzzy.png","language":"Go","readme":"# arguments\nArgument option parser for go.  \n\n## What is this\nI always had to write codes for following operations.  \n1. Parse argument options.\n2. Handle following situations.\n\t- Our required options are not provided.\n\t- The specified options are invalid to use.\n\nSo I aggregate codes to provide above operations to this package.  \n\n## Install\nWe can get this package by `go get` command.\n```sh\n$ go get github.com/mozzzzy/arguments/v2\n```\nOr if we use `go.mod`, we can write like following snippet.\n```sh\nmodule \u003cyour module name\u003e\n\ngo 1.13\n\nrequire(\n  github.com/mozzzzy/arguments/v2 v2.0.1\n)\n```\nAnd then execute `go get`.\n```sh\n$ go get\n```\n\n## Usage\nWe can see an example code in [examples](https://github.com/mozzzzy/arguments/tree/master/examples) directory.\n\n### Import\nFirst, we have to import some modules.\n```go\nimport (\n  \"github.com/mozzzzy/arguments/v2\"\n  \"github.com/mozzzzy/arguments/v2/argumentOption\"  // if you parse options (like -h --help)\n  \"github.com/mozzzzy/arguments/v2/argumentOperand\" // if you parse operands\n)\n```\n\n### Handle options (like --opt VALUE or -o VALUE)\n#### Add an option rule\nTo parse command line options, we have to create `arguments.Args`\n and then add an option rule by `AddOption()` method.\n```go\nvar args arguments.Args\n\nopt := argumentOption.Option{\n\tLongKey:        \"long-key\",\n\tShortKey:       \"s\",\n\tValueType:      \"string\",\n\tDescription:    \"some option.\",\n}\nif err := args.AddOption(opt); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n```\n##### LongKey and ShortKey\n`LongKey` and `ShortKey` is keys of the option.  \nFor example, above code add a rule for `--long-key` and `-s`.  \n\n##### ValueType\n`int` and `string` are available for `ValueType`.  \n  \nIf we have to parse option that doesn't have value (like --help),  \nwe should skip configuring `ValueType` field.\n```go\nvar args arguments.Args\n\nopt := argumentOption.Option{\n\tLongKey:        \"help\",\n\tShortKey:       \"h\",\n\tDescription:    \"show help message and exit.\",\n}\nif err := args.AddOption(opt); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n```\n\n##### Description\n`Description` is the description of the option. This is used in usage message.\n\n##### Required\n`Required: true` specifies the option is required.  \nIf required option is not set, `args.Parse()` method returns error.\n\n##### DefaultValue\n`DefaultValue` specifies the default value of the option.  \nIf the option is not specified, the default value is used.\n\n##### Validator and ValidatorParam\nWe often have to validate option values.  \nWe can validate them easily.  \n```go\nopt := argumentOption.Option{\n\tLongKey:        \"long-key\",\n\tShortKey:       \"s\",\n\tValueType:      \"int\",\n\tDescription:    \"some option.\",\n\tDefaultValue:   80,\n\tValidator:      validator.ValidateInt,\n\tValidatorParam: validator.ParamInt{Min: 0, Max: 100},\n}\n```\nValidator function should be `func (interface{}, interface{}) error`.  \nThe first parameter is the `argumentOption.Option` data. The second parameter is `ValidatorParam`.\n\n#### Add multiple option rules at once\nwe can add multiple option rules at once by `AddOptions()` method.\n```go\nopt1 := argumentOption.Option{\n\tLongKey:        \"string\",\n\tShortKey:       \"s\",\n\tValueType:      \"string\",\n\tDescription:    \"some option.\",\n}\nopt2 := argumentOption.Option{\n\tLongKey:        \"int\",\n\tShortKey:       \"i\",\n\tValueType:      \"int\",\n\tDescription:    \"some option.\",\n}\n\nif err := args.AddOptions([]argumentOption.Option{opt1, opt2}); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n```\n\n#### Parse options\nAfter adding option rules, we can parse command line options using `Parse()` method.\n```go\nvar args arguments.Args\n\nopt := argumentOption.Option{\n\tLongKey:        \"long-key\",\n\tShortKey:       \"s\",\n\tValueType:      \"string\",\n\tDescription:    \"some option.\",\n}\nif err := args.AddOption(opt); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n\nif err := args.Parse(); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n```\n\n#### Get option's value\nTo get value of parsed options, we use `GetIntOpt()` `GetStringOpt()` and `GetOpt()` method.  \nThe parameter is the long key or short key.\n```go\n// GetStringOpt()\nif val, err := args.GetStringOpt(\"long-key\"); err != nil {\n\tfmt.Println(err.Error())\n\tfmt.Println(args)\n} else {\n\tfmt.Println(val)\n}\n\n// GetIntOpt()\nif val, err := args.GetIntOpt(\"long-key\"); err != nil {\n\tfmt.Println(err.Error())\n\tfmt.Println(args)\n} else {\n\tfmt.Println(val)\n}\n\n// GetOpt()\nif val, err := args.GetOpt(\"long-key\"); err != nil {\n\tfmt.Println(err.Error())\n\tfmt.Println(args)\n} else {\n\tvalInt, ok := val.(int)\n\tif ok {\n\t\tfmt.Println(valInt)\n\t} else {\n\t\tfmt.Println(\"value of --long-key is not integer.\")\n\t}\n}\n```\n\n#### Check option is set\nTo check an option is set, we use `OptIsSet()` method.\n```\nif args.OptIsSet(\"--help\") {\n\tfmt.Println(\"--help -h option is specified.\")\n}\n```\n\n#### Print Usage\n`arguments.Args` has `String()` method.  \nWe can print usage message just by printing `arguments.Args`.\n```go\nfmt.Println(args)\n```\n\n### Handle Operands\nWe can handle operands by almost the same way with options.  \nThe differences are following points.  \n* Use `argumentOperand.Operand` instead of `argumentOption.Option`\n* In `argumentOperand.Operand`, use `Key` instead of `LongKey` and `ShortKey`\n* Add operand rule using `AddOperand()` and `AddOperands()` methods.\n* Get operand value using `GetStringOperand()` `GetIntOperand()` and `GetOperand()`\n* To check an operand is set, use `OperandIsSet()` instead of `OptionIsSet()`\n  \nFollowing code is an example.\n```go\nope1 := argumentOperand.Operand{\n\tKey: \"operand1\",\n\tValueType: \"string\",\n\tDescription: \"some operand\",\n\tRequired: true,\n}\nope2 := argumentOperand.Operand{\n\tKey: \"operand2\",\n\tValueType: \"int\",\n\tDescription: \"some operand\",\n}\nope3 := argumentOperand.Operand{\n\tKey: \"operand3\",\n\tValueType: \"int\",\n\tDescription: \"some operand\",\n}\n\nif err := args.AddOperand(ope1); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\nif err := args.AddOperands([]argumentOperand.Operand {ope2, ope3}); err != nil {\n\tfmt.Println(err.Error())\n\treturn\n}\n\nif err := args.Parse(); err != nil {\n\tfmt.Println(err.Error())\n\tfmt.Println(args)\n\treturn\n}\n\nif str2, err := args.GetStringOperand(\"operand1\"); err != nil {\n\tfmt.Println(err.Error())\n\tfmt.Println(args)\n\treturn\n} else {\n\tfmt.Println(str2)\n}\n\nif integer2, err := args.GetIntOperand(\"operand2\"); err != nil {\n\tfmt.Println(err.Error())\n} else {\n\tfmt.Println(integer2)\n}\n\nif ope3, err := args.GetOperand(\"operand3\"); err != nil {\n\tfmt.Println(err.Error())\n} else {\n\tinteger3, ok := ope3.(int)\n\tif ok {\n\t\tfmt.Println(integer3)\n\t} else {\n\t\tfmt.Println(\"value of operand3 is not integer.\")\n\t}\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmozzzzy%2Farguments","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmozzzzy%2Farguments","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmozzzzy%2Farguments/lists"}