Last change
on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Line | |
---|
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, processArgv} = require('./build/index.cjs');
|
---|
6 |
|
---|
7 | Argv(processArgv.hideBin(process.argv));
|
---|
8 |
|
---|
9 | module.exports = Argv;
|
---|
10 |
|
---|
11 | function Argv(processArgs, cwd) {
|
---|
12 | const argv = Yargs(processArgs, cwd, require);
|
---|
13 | singletonify(argv);
|
---|
14 | // TODO(bcoe): warn if argv.parse() or argv.argv is used directly.
|
---|
15 | return argv;
|
---|
16 | }
|
---|
17 |
|
---|
18 | /* Hack an instance of Argv with process.argv into Argv
|
---|
19 | so people can do
|
---|
20 | require('yargs')(['--beeble=1','-z','zizzle']).argv
|
---|
21 | to parse a list of args and
|
---|
22 | require('yargs').argv
|
---|
23 | to get a parsed version of process.argv.
|
---|
24 | */
|
---|
25 | function singletonify(inst) {
|
---|
26 | [
|
---|
27 | ...Object.keys(inst),
|
---|
28 | ...Object.getOwnPropertyNames(inst.constructor.prototype),
|
---|
29 | ].forEach(key => {
|
---|
30 | if (key === 'argv') {
|
---|
31 | Argv.__defineGetter__(key, inst.__lookupGetter__(key));
|
---|
32 | } else if (typeof inst[key] === 'function') {
|
---|
33 | Argv[key] = inst[key].bind(inst);
|
---|
34 | } else {
|
---|
35 | Argv.__defineGetter__('$0', () => {
|
---|
36 | return inst.$0;
|
---|
37 | });
|
---|
38 | Argv.__defineGetter__('parsed', () => {
|
---|
39 | return inst.parsed;
|
---|
40 | });
|
---|
41 | }
|
---|
42 | });
|
---|
43 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.