1
|
'use strict'
|
2
|
// classic singleton yargs API, to use yargs
|
3
|
// without running as a singleton do:
|
4
|
// require('yargs/yargs')(process.argv.slice(2))
|
5
|
const yargs = require('./yargs')
|
6
|
|
7
|
Argv(process.argv.slice(2))
|
8
|
|
9
|
module.exports = Argv
|
10
|
|
11
|
function Argv (processArgs, cwd) {
|
12
|
const argv = yargs(processArgs, cwd, require)
|
13
|
singletonify(argv)
|
14
|
return argv
|
15
|
}
|
16
|
|
17
|
/* Hack an instance of Argv with process.argv into Argv
|
18
|
so people can do
|
19
|
require('yargs')(['--beeble=1','-z','zizzle']).argv
|
20
|
to parse a list of args and
|
21
|
require('yargs').argv
|
22
|
to get a parsed version of process.argv.
|
23
|
*/
|
24
|
function singletonify (inst) {
|
25
|
Object.keys(inst).forEach((key) => {
|
26
|
if (key === 'argv') {
|
27
|
Argv.__defineGetter__(key, inst.__lookupGetter__(key))
|
28
|
} else {
|
29
|
Argv[key] = typeof inst[key] === 'function' ? inst[key].bind(inst) : inst[key]
|
30
|
}
|
31
|
})
|
32
|
}
|