-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
61 lines (58 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const yargs = require("yargs");
const core = require("./core");
const log = require("util").debuglog("zeroarg");
/**
* zeroarg a code-free argument parser
*
* Functions that you provide to zeroarg follow certain rules:
*
* Their arguments work like:
*
* [positionalArgument],
* [positionalArgument2],
* [variadicPositionalArgument],
* [optionsArgument]
*
* - The first arguments can be positional arguments.
* - The last argument to the method can be an object that receives the
* flag arguments (like --option). Only the last argument can be an options
* object.
* - Options can have JSDoc types of string, number, array, and boolean.
* - If you specify a default in JSDoc-land, it becomes a default in yargs
* - If you specify an argument as an enum in JSDoc-land, it becomes choices in yargs
*
* @param {Function} fn a function that returns a function, and has at least
* one JSDoc comment preceding that function.
* @example
* zeroarg(() =>
* /**
* * Hello world
* *\/
* function helloworld() {
* console.log('Hello world');
* }
* );
*/
function zeroarg(fn) {
return core(fn)
.then(yargsConfig => {
yargs.reset();
yargs.help();
yargs.epilogue(yargsConfig.epilogue);
yargs.options(yargsConfig.options);
log(yargsConfig);
var argv = yargs.command(yargsConfig.command).argv;
var args = yargsConfig.positionalArgumentNames
.map(name => {
return argv[name];
})
.concat(argv);
log(args);
fn().apply(undefined, args);
})
.catch(err => {
console.log(err.stack);
throw err;
});
}
module.exports = zeroarg;