This repository was archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargv.js
More file actions
77 lines (67 loc) · 2.12 KB
/
argv.js
File metadata and controls
77 lines (67 loc) · 2.12 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var cli = require('../../index')
// create a program with the descriptor information
, prg = cli.load(require('./argv.json'));
/**
* @name argv
* @cli doc/example/argv.md
*/
function main(argv, cb) {
// we want to be able to accept arguments
// for testing purposes but when they are not specified
// it is ok becuase process.argv is used by default
if(typeof argv === 'function') {
cb = argv;
argv = null;
}
// target for parsed command line options
var scope = {}
// runtime configuration for program execution
, runtime = {
// resolve paths relative to this directory
base: __dirname,
// pass the scope
target: scope,
// give the argument parser some hints
hints: prg,
// configure the help plugin to show the help file
help: {
file: 'argv.txt'
},
// configure the version plugin
version: {
name: 'argv',
version: '1.0.0'
},
// configure plugins for program execution
plugins: [
require('../../plugin/hints'),
require('../../plugin/argv'),
require('../../plugin/help'),
require('../../plugin/version')
]
};
// run the program passing the program, raw arguments and the
// runtime configuration
cli.run(prg, argv, runtime, function parsed(err, req) {
// handle errors and aborted request
// the request will have been aborted if --help or
// the --version option was specified
if(err || req.aborted) {
return cb(err);
}
// `this` is the `scope` object passed as the `target`
// parsed arguments are available using `this`
// more information is available on the `req` object
// of particular interest is `req.args` which is the argument
// parser result object and `req.unparsed` which contains
// any unparsed arguments
// include the unparsed arguments in the output
this.unparsed = req.unparsed;
// respect the -e, --err option
if(this.err) {
return console.error(this);
}
console.log(this);
})
}
module.exports = main;