{"id":15497315,"url":"https://github.com/jwerle/kargv","last_synced_at":"2025-03-28T17:22:19.944Z","repository":{"id":148540523,"uuid":"174743481","full_name":"jwerle/kargv","owner":"jwerle","description":"Minimal command line options parser for Kotlin/Native","archived":false,"fork":false,"pushed_at":"2019-03-09T22:32:05.000Z","size":8,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T17:56:15.740Z","etag":null,"topics":["argv","kargv","kotlin","native","parser"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","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/jwerle.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":"2019-03-09T20:36:52.000Z","updated_at":"2019-03-09T23:48:01.000Z","dependencies_parsed_at":"2023-05-20T11:15:29.242Z","dependency_job_id":null,"html_url":"https://github.com/jwerle/kargv","commit_stats":{"total_commits":6,"total_committers":2,"mean_commits":3.0,"dds":"0.33333333333333337","last_synced_commit":"7e239bdb1f9a61db18b366e63d234d49ecaabaf6"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fkargv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fkargv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fkargv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwerle%2Fkargv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwerle","download_url":"https://codeload.github.com/jwerle/kargv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246068281,"owners_count":20718503,"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":["argv","kargv","kotlin","native","parser"],"created_at":"2024-10-02T08:32:50.333Z","updated_at":"2025-03-28T17:22:19.928Z","avatar_url":"https://github.com/jwerle.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"kargv\n=====\n\nMinimal command line options parser for Kotlin/Native\n\n## Installation\n\n```sh\n$ npm install kargv\n```\n\n## Usage\n\n```kotlin\n// declare model for options container\ndata class Opts(var arg: String? = null)\n// create container for `parse()`\nval opts = Opts()\ntry {\n  // parse args in `argv` into `opts` in the callback\n  parse(argv, opts} { o, node -\u003e\n    // set `node.value` based on `node.name?`\n    // on `o` which points back to `opts`\n  }\n} catch (err: Error) {\n  println(\"error: ${err.message}\")\n}\n```\n\n## Example\n\n```kotlin\nimport kotlin.system.exitProcess\nimport kargv.parse\n\ndata class Options(\n  var help: Boolean = false,\n  var port: Int? = 3000,\n  var config: String? = null,\n  var version: Boolean = false,\n  var `--`: Array\u003cString?\u003e = emptyArray()\n)\n\nval VERSION = \"0.0.1\"\nval USAGE = \"usage: example.kexe [-hV] [options]\"\nval OPTIONS = \"\"\"\nwhere options can be:\n  -h, --help          Show this message\n  -V, --version       Output program version\n  -p, --port \u003cport\u003e   Port that program should listen on\n  -c, --config \u003cpath\u003e Path to a configuration file\n\"\"\"\n\nfun main(argv: Array\u003cString\u003e) {\n  val opts = Options()\n\n  try {\n    parse(argv, opts) { o, node -\u003e\n      val value = node.value\n      when (node.name) {\n        \"h\", \"help\" -\u003e o.help = true\n        \"p\", \"port\" -\u003e o.port = value?.toInt()\n        \"c\", \"config\" -\u003e o.config = value\n        \"V\", \"version\" -\u003e o.version = true\n        // handle rest arguments and args after `--`\n        else -\u003e\n          if (null == node.name) {\n            o.`--` += value\n          } else {\n            throw Error(\"unknown option: -${node.name}\")\n          }\n      }\n    }\n  } catch (err: Error) {\n    println(\"error: ${err.message}\")\n    println(USAGE)\n    exitProcess(1)\n  }\n\n  if (true == opts.help) {\n    println(USAGE)\n    println(OPTIONS)\n    exitProcess(0)\n  }\n\n  if (true == opts.version) {\n    println(VERSION)\n    exitProcess(0)\n  }\n\n  println(\"\"\"\n    port = ${opts.port}\n    config = ${opts.config}\n    arguments = ${opts.`--`.joinToString(\" \")}\n  \"\"\")\n}\n```\n\n## API\n\n### `fun \u003cT : Any\u003e parse(argv: Array\u003cString\u003e, out: T, cb: (T, Node) -\u003e Any?): T`\n\nParse command line arguments in `argv` int `out: T` by calling `cb(out, node)`\nfor each parsed argument with output `out: T` and `node: Node` (see below)\nthat contains the `name`, `source`, and `value` of a parsed command line\nargument node.\n\n* `argv: Array\u003cString\u003e` - An array of `String` command line arguments\n  likely passed in fun `main(argv: Array\u003cString\u003e)`.\n* `out: T` - A non-nullable type instance container that stores parsed\n  command line values.\n* `cb: (T, Node) -\u003e Any?` - A callback function that is called for each\n  parsed command line node.\n\n#### Rest Arguments\n\nCommand line arguments that fall after the `--` node will be consider\n_nameless_ nodes and can be captured by the callback function as such as\n`node.name` will be `null`\n\n### `data class Node(val name: String?, val source: String, val value: String?)`\n\nA data class container that represents a parsed command line argument\nnode. This can be a key-value pair, a flag, or a scalar value.\n\n* `val name: String? = null` - The name of the parsed command line\n  argument. A parsed command line argument node may be nameless, and\n  therefore this value will be `null`.\n* `val source: String` - The literal source of the parsed command line\n  argument.\n* `val value: String? = null` - The value of the parsed command line\n  argument. A parsed command line argument node may be valueless, and\n  therefore this value will be `null`.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fkargv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwerle%2Fkargv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwerle%2Fkargv/lists"}