Examples
在官方例子中有更多例子,详见 Example Folder。
plunder.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
if (argv.ships > 3 && argv.distance < 53.5) {
console.log('Plunder more riffiwobbles!');
} else {
console.log('Retreat from the xupptumblers!');
}
$ ./plunder.js --ships=4 --distance=22 Plunder more riffiwobbles!
$ ./plunder.js --ships 12 --distance 98.7 Retreat from the xupptumblers!
你可以使用缩略模式的 option。
short.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
console.log('(%d,%d)', argv.x, argv.y);
$ ./short.js -x 10 -y 21 (10,21)
甚至可以和布尔值组合使用,如下:
bool.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
if (argv.s) {
process.stdout.write(argv.fr ? 'Le perroquet dit: ' : 'The parrot says: ');
}
console.log(
(argv.fr ? 'couac' : 'squawk') + (argv.p ? '!' : '')
);
$ ./bool.js -s The parrot says: squawk
$ ./bool.js -sp The parrot says: squawk!
$ ./bool.js -sp --fr Le perroquet dit: couac!
也可以使用没有连字符的 option ,用 argv._
即可!
nonopt.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2)).argv;
console.log('(%d,%d)', argv.x, argv.y);
console.log(argv._);
$ ./nonopt.js -x 6.82 -y 3.35 rum (6.82,3.35) [ 'rum' ]
$ ./nonopt.js "me hearties" -x 0.54 yo -y 1.12 ho (0.54,1.12) [ 'me hearties', 'yo', 'ho' ]
Yargs 也可以对 option 计数:
count.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.count('verbose')
.alias('v', 'verbose')
.argv;
VERBOSE_LEVEL = argv.verbose;
function WARN() { VERBOSE_LEVEL >= 0 && console.log.apply(console, arguments); }
function INFO() { VERBOSE_LEVEL >= 1 && console.log.apply(console, arguments); }
function DEBUG() { VERBOSE_LEVEL >= 2 && console.log.apply(console, arguments); }
WARN("Showing only important stuff");
INFO("Showing semi-important stuff too");
DEBUG("Extra chatty mode");
$ node count.js Showing only important stuff
$ node count.js -v Showing only important stuff Showing semi-important stuff too
$ node count.js -vv Showing only important stuff Showing semi-important stuff too Extra chatty mode
$ node count.js -v --verbose Showing only important stuff Showing semi-important stuff too Extra chatty mode
Yargs 提供 usage
和 demandOption
可以告诉用户如何使用你的 options ,并对 options 的格式提出要求。
area.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 -w [num] -h [num]')
.demandOption(['w','h'])
.argv;
console.log("The area is:", argv.w * argv.h);
$ ./area.js -w 55 -h 11 The area is: 605
$ node ./area.js -w 4.91 -w 2.51 Usage: area.js -w [num] -h [num]
Options: -w [required] -h [required]
Missing required arguments: h
demandCommand
可以要求最多或最少拥有几个参数,并可以在没有满足条件时提供相应的错误信息。
demand_count.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.demandCommand(2)
.argv;
console.dir(argv);
$ ./demand_count.js a
Not enough non-option arguments: got 1, need at least 2
$ ./demand_count.js a b { _: [ 'a', 'b' ], '$0': 'demand_count.js' }
$ ./demand_count.js a b c { _: [ 'a', 'b', 'c' ], '$0': 'demand_count.js' }
default
可以提供默认值:
default_singles.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.default('x', 10)
.default('y', 10)
.argv
;
console.log(argv.x + argv.y);
$ ./default_singles.js -x 5 15
default_hash.js:
#!/usr/bin/env node
var argv = require('yargs/yargs')(process.argv.slice(2))
.default({ x : 10, y : 10 })
.argv
;
console.log(argv.x + argv.y);
$ ./default_hash.js -y 7 17