{"id":16636800,"url":"https://github.com/takamin/hash-arg","last_synced_at":"2026-05-10T06:47:51.708Z","repository":{"id":6234129,"uuid":"55150308","full_name":"takamin/hash-arg","owner":"takamin","description":"node module: Get arguments as an object represented by specified keys and values","archived":false,"fork":false,"pushed_at":"2023-10-16T18:18:11.000Z","size":1113,"stargazers_count":1,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T22:12:58.779Z","etag":null,"topics":["argv-parser","nodejs","npm"],"latest_commit_sha":null,"homepage":"http://takamints.hatenablog.jp/entry/npm-hash-arg-introduction","language":"JavaScript","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/takamin.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":"2016-03-31T12:52:43.000Z","updated_at":"2024-06-19T01:35:25.333Z","dependencies_parsed_at":"2024-06-19T01:35:10.307Z","dependency_job_id":"8b406e73-c9a2-44a4-bf5e-c4c50fc201c0","html_url":"https://github.com/takamin/hash-arg","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takamin%2Fhash-arg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takamin%2Fhash-arg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takamin%2Fhash-arg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/takamin%2Fhash-arg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/takamin","download_url":"https://codeload.github.com/takamin/hash-arg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247271527,"owners_count":20911587,"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-parser","nodejs","npm"],"created_at":"2024-10-12T06:22:53.674Z","updated_at":"2026-05-10T06:47:51.641Z","avatar_url":"https://github.com/takamin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"hash-arg \n========\n\n\u003cspan class=\"display:inline-block;\"\u003e ![version](https://img.shields.io/npm/v/hash-arg)\n![license](https://img.shields.io/npm/l/hash-arg)\n[![Build Status](https://travis-ci.org/takamin/hash-arg.svg?branch=master)](https://travis-ci.org/takamin/hash-arg)\n[![Coverage Status](https://coveralls.io/repos/github/takamin/hash-arg/badge.svg?branch=master)](https://coveralls.io/github/takamin/hash-arg?branch=master)\n![node version](https://img.shields.io/node/v/hash-arg)\n\u003c/span\u003e\n\nThis is a CLI parameter parser to get the named and typed value.\nBut any options started with '-' or '--' never be parsed by this package.\nIf such options are needed, At first, use other option parser like 'node-getopt'\nand then process the rest parameters with this package.\n\n## Simple use with 'process.argv'\n\nSimply, get method names each elements in process.argv.\n\n__simple.js__\n\n```\nconst args = require(\"hash-arg\").get(\n        \"inputFilePath outputFilePath\");\n\nconsole.log(JSON.stringify(args, null, \"  \"));\n```\n\nOutputs:\n\n```\n$ node test/simple.js input.json output.json\n{\n  \"inputFilePath\": \"input.json\",\n  \"outputFilePath\": \"output.json\"\n}\n```\n\n## Using with an argv parser like 'node-getopt' module\n\nThe optional second parameter of the get method is normal array.\nSo, for instance, you can specify a `node-getopt`'s argv property for it.\n\n__with-node-argv.js__\n\n```\ngetopt = require(\"node-getopt\").create([\n    ['s', '', 'short option'],\n    ['l', 'long', 'long option'],\n    ['S', 'short-with-arg=ARG', 'option with argument']\n]).parseSystem();\n\nargs = require(\"hash-arg\").get([\n        \"inputFilePath\",\n        {\n            \"name\":\"outputFilePath\",\n            \"default\": \"out.json\"\n        }\n        ], getopt.argv);\n\nconsole.log(JSON.stringify(args, null, \"  \"));\n```\n\nOutputs:\n\n```\n$ node test/with-node-getopt.js -S DUMMY input.json -sl output.json\n{\n  \"inputFilePath\": \"input.json\",\n  \"outputFilePath\": \"output.json\"\n}\n```\n\n## METHOD GET\n\n__prototype__\n\n`HashArg.get(\u003cargument-def\u003e [, \u003cargv-source-array\u003e]);`\n\n### argument-def\n\nThis can be specified as a string, an array of string,\nor an array of definition object.\n\n__1) string__\n\nThe string that contains parameter names separated by space.\n\n```\n\"inputFilePath outputFilePath\"\n```\n\nIf the string contains ';' character, each elements splited by the character declare the type and name.\n\n```\n\"string inputFilePath; number countOfFile\"\n```\n\nOr, following type specification is also available.\nIt is used in a UML class diagram.\n\n```\n\"inputFilePath:string; countOfFile:number\"\n```\n\nWhen the type is not specified,\nit is regarded for `var`.\n\n__2) Array of string__\n\nEach element represents the parameter name.\n\n```\n[\"inputFilePath\", \"outputFilePath\"]\n```\n\n_type declaration_:\n\nYou can specify the type of the value.\nThe available type is 'string' or 'number'.\n\nWhen the declaration is separated by space,\nit represents the type and its name.\n\nAnd, when it is separated by a colon,\nthose are the name and its type.\n\n```\n[\"string inputFilePath\", \"number countOfFile\"]\n```\n\nAnd, Following is available too.\n\n```\n[\"inputFilePath:string\", \"countOfFile:number\"]\n```\n\n_specify default value_:\n\nYou can specify the default value, If the value is not specified.\n\n```\n['inputFilePath:string=\"foo.txt\"', \"countOfFile:number=1234\"]\n```\nA string value must be quoted by double quotation rather\nthan single, or the parsing will fail.\nThis is a specification of JSON.parse.\n\nWhen the default value is not declared, null will be used.\n\n__3) Array of definition object__\n\nFollowing declaration is available.\n\n```\n[\n    {\"name\":\"inputFilePath\"},\n    {\n        \"name\"      : \"outputFilePath\",\n        \"type\"      : \"string\" // 'string' or 'number'\n        \"default\"   : \"out.json\"\n    }\n]\n```\n\n### Type Specification\n\nTo specify the type to a named parameter.\nFollowing two styles are available.\n\n1. \"_`\u003ctype\u003e \u003cname\u003e`_\" - ( C/C++ style )\n2. \"_`\u003cname\u003e : \u003ctype\u003e`_\" - ( UML style )\n\n\n### Array Type Specification\n\nThe last argument can be set as an array.\nThe rest arguments in the list will be contained to the parameter.\n\nTo specify, pair of square brackets could be put after the type name.\nThe brackets must be empty.\n\nFollowings are all now available.\n\n```\n[\"string inputFilePath\", \"number[] countOfFile\"]\n```\n\n```\n[\"inputFilePath:string\", \"countOfFile:number[]\"]\n```\n\n```\n[\n    {\"name\":\"inputFilePath\"},\n    {\n        \"name\"      : \"outputFilePath\",\n        \"type\"      : \"string[]\"\n    }\n]\n```\n\n### argv-source-array (optional)\n\nAn array of string to parse as command line parameters.\n\nThe `process.argv` is used by default, when it is not specified,\n\nLICENSE\n-------\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakamin%2Fhash-arg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftakamin%2Fhash-arg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftakamin%2Fhash-arg/lists"}