{"id":1784,"date":"2012-11-20T14:21:48","date_gmt":"2012-11-20T05:21:48","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=1784"},"modified":"2013-12-29T23:27:15","modified_gmt":"2013-12-29T14:27:15","slug":"post-1784","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/1784","title":{"rendered":"node.js\u3067\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u5f15\u6570\u306e\u51e6\u7406\u3092\u884c\u3046\u306a\u3089\u300cargv\u300d\u304c\u4fbf\u5229"},"content":{"rendered":"<p>\u8907\u96d1\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u6307\u5b9a\u3055\u308c\u305f\u5f15\u6570\u306a\u3069\u3082\u3001\u3068\u3066\u3082\u7c21\u5358\u306b\u53d6\u5f97\u3067\u304d\u308b\u306e\u3067\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/npmjs.org\/package\/argv\">argv<\/a><\/p>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306fnpm\u3067<\/p>\n<pre class=\"lang:sh decode:true \" >$ npm install argv<\/pre>\n<p>argv\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u3066run\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u5f15\u6570\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"\u5f15\u6570\u306e\u53d6\u5f97\" >var argv = require('argv');\r\nconsole.log(argv.run());\r\n<\/pre>\n<pre class=\"lang:sh decode:true \" title=\"\u5b9f\u884c\u7d50\u679c\" >$ node sample.js test aaa\r\n{ targets: [ 'test', 'aaa' ], options: {} }\r\n<\/pre>\n<p>\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u6307\u5b9a\u3055\u308c\u305f\u5f15\u6570\u3092\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u3001option\u30e1\u30bd\u30c3\u30c9\u3067\u53d6\u5f97\u3059\u308b\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u8a2d\u5b9a\u3092\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u53d6\u5f97\" >var argv = require('argv');\r\n\r\nargv.option({\r\n\tname: 'option',\r\n\tshort: 'o',\r\n\ttype : 'string',\r\n\tdescription :'\u3042\u306a\u305f\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5b9a\u7fa9\u3057\u307e\u3059',\r\n\texample: &quot;'script --option=value' or 'script -o value'&quot;\r\n});\r\nconsole.log(argv.run());\r\n<\/pre>\n<pre class=\"lang:sh decode:true \" title=\"\u5b9f\u884c\u7d50\u679c\" >$ node sample test -o aaa\r\n{ targets: [ 'test' ], options: { option: 'aaa' } }\r\n<\/pre>\n<p>\u30d8\u30eb\u30d7\u3082\u81ea\u52d5\u7684\u306b\u4f5c\u6210\u3055\u308c\u307e\u3059<\/p>\n<pre class=\"lang:sh decode:true \" >$ node sample -h\r\nUsage: sample [options]\r\n\r\n        --help, -h\r\n                Displays help information about this script\r\n                'sample -h' or 'sample --help'\r\n\r\n        --option, -o\r\n                \u3042\u306a\u305f\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u30aa\u30d7\u30b7\u30e7\u30f3\u3092\u5b9a\u7fa9\u3057\u307e\u3059\r\n                'script --option=value' or 'script -o value'\r\n<\/pre>\n<p>\u30aa\u30d7\u30b7\u30e7\u30f3\u306e\u5024\u306b\u306f\u578b\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"\u578b\u6307\u5b9a\" >var argv = require('argv');\r\n\r\nargv.option([\r\n    {\r\n        name: 'option',\r\n        type: 'csv,int'\r\n    },\r\n    {\r\n        name: 'path',\r\n        short: 'p',\r\n        type: 'list,path'\r\n    }\r\n]);\r\n\r\nconsole.log(argv.run());\r\n<\/pre>\n<pre class=\"lang:sh decode:true \" title=\"\u5b9f\u884c\u7d50\u679c\" >$ node sample test --option=123,456.001,789.01\r\n{ targets: [ 'test' ], options: { option: [ 123, 456, 789 ] } }\r\n\r\n$ node sample test -p \/path\/to\/file1 -p \/path\/to\/file2\r\n{ targets: [ 'test' ], options: { path: [ '\/path\/to\/file1', '\/path\/to\/file2' ] } }\r\n<\/pre>\n<p>\u72ec\u81ea\u306e\u578b\u3092\u4f5c\u6210\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" title=\"\u30ab\u30b9\u30bf\u30e0\u30bf\u30a4\u30d7\" >var argv = require('argv');\r\n\r\nargv.type( 'squared', function( value ) { \/\/squared\u578b\u3092\u5b9a\u7fa9\r\n    value = parseFloat( value );\r\n    return value * value;\r\n});\r\n\r\nargv.option({\r\n    name: 'square',\r\n    short: 's',\r\n    type: 'squared'\r\n});\r\nconsole.log(argv.run());\r\n<\/pre>\n<p>\u4e0a\u8a18\u3067\u4f5c\u6210\u3057\u305fsquared\u578b\u306f\u3001&#8211;square or -s \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u6307\u5b9a\u3055\u308c\u305f\u5f15\u6570\u306e2\u4e57\u3055\u308c\u305f\u5024\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" title=\"\u5b9f\u884c\u7d50\u679c\" >$ node sample test -s 2\r\n{ targets: [ 'test' ], options: { square: 4 } }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8907\u96d1\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u304c\u6307\u5b9a\u3055\u308c\u305f\u5f15\u6570\u306a\u3069\u3082\u3001\u3068\u3066\u3082\u7c21\u5358&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1784","post","type-post","status-publish","format-standard","hentry","category-node-js"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1784","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=1784"}],"version-history":[{"count":13,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1784\/revisions"}],"predecessor-version":[{"id":3268,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/1784\/revisions\/3268"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=1784"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=1784"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=1784"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}