Skip to content

Commit c2f1e62

Browse files
dougwilsonErisDS
authored andcommitted
Switch cmd parser to latest minimist
1 parent 08e9a11 commit c2f1e62

File tree

5 files changed

+365
-401
lines changed

5 files changed

+365
-401
lines changed

bin/.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
rules: {
3+
'no-console': 0,
4+
'no-var': 0
5+
}
6+
};

bin/handlebars

+160-108
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,176 @@
11
#!/usr/bin/env node
22

3-
const yargs = require('yargs')
4-
.usage('Precompile handlebar templates.\nUsage: $0 [template|directory]...')
5-
.option('f', {
6-
type: 'string',
7-
description: 'Output File',
8-
alias: 'output'
9-
})
10-
.option('map', {
11-
type: 'string',
12-
description: 'Source Map File'
13-
})
14-
.option('a', {
15-
type: 'boolean',
16-
description: 'Exports amd style (require.js)',
17-
alias: 'amd'
18-
})
19-
.option('c', {
20-
type: 'string',
21-
description: 'Exports CommonJS style, path to Handlebars module',
22-
alias: 'commonjs',
23-
default: null
24-
})
25-
.option('h', {
26-
type: 'string',
27-
description: 'Path to handlebar.js (only valid for amd-style)',
28-
alias: 'handlebarPath',
29-
default: ''
30-
})
31-
.option('k', {
32-
type: 'string',
33-
description: 'Known helpers',
34-
alias: 'known'
35-
})
36-
.option('o', {
37-
type: 'boolean',
38-
description: 'Known helpers only',
39-
alias: 'knownOnly'
40-
})
41-
.option('m', {
42-
type: 'boolean',
43-
description: 'Minimize output',
44-
alias: 'min'
45-
})
46-
.option('n', {
47-
type: 'string',
48-
description: 'Template namespace',
49-
alias: 'namespace',
50-
default: 'Handlebars.templates'
51-
})
52-
.option('s', {
53-
type: 'boolean',
54-
description: 'Output template function only.',
55-
alias: 'simple'
56-
})
57-
.option('N', {
58-
type: 'string',
59-
description:
60-
'Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates.',
61-
alias: 'name'
62-
})
63-
.option('i', {
64-
type: 'string',
65-
description:
66-
'Generates a template from the passed CLI argument.\n"-" is treated as a special value and causes stdin to be read for the template value.',
67-
alias: 'string'
68-
})
69-
.option('r', {
70-
type: 'string',
71-
description:
72-
'Template root. Base value that will be stripped from template names.',
73-
alias: 'root'
74-
})
75-
.option('p', {
76-
type: 'boolean',
77-
description: 'Compiling a partial template',
78-
alias: 'partial'
79-
})
80-
.option('d', {
81-
type: 'boolean',
82-
description: 'Include data when compiling',
83-
alias: 'data'
84-
})
85-
.option('e', {
86-
type: 'string',
87-
description: 'Template extension.',
88-
alias: 'extension',
89-
default: 'handlebars'
90-
})
91-
.option('b', {
92-
type: 'boolean',
93-
description:
94-
'Removes the BOM (Byte Order Mark) from the beginning of the templates.',
95-
alias: 'bom'
96-
})
97-
.option('v', {
98-
type: 'boolean',
99-
description: 'Prints the current compiler version',
100-
alias: 'version'
101-
})
102-
.option('help', {
103-
type: 'boolean',
104-
description: 'Outputs this message'
105-
})
106-
.wrap(120);
107-
108-
const argv = yargs.argv;
3+
var argv = parseArgs({
4+
'f': {
5+
'type': 'string',
6+
'description': 'Output File',
7+
'alias': 'output'
8+
},
9+
'map': {
10+
'type': 'string',
11+
'description': 'Source Map File'
12+
},
13+
'a': {
14+
'type': 'boolean',
15+
'description': 'Exports amd style (require.js)',
16+
'alias': 'amd'
17+
},
18+
'c': {
19+
'type': 'string',
20+
'description': 'Exports CommonJS style, path to Handlebars module',
21+
'alias': 'commonjs',
22+
'default': null
23+
},
24+
'h': {
25+
'type': 'string',
26+
'description': 'Path to handlebar.js (only valid for amd-style)',
27+
'alias': 'handlebarPath',
28+
'default': ''
29+
},
30+
'k': {
31+
'type': 'string',
32+
'description': 'Known helpers',
33+
'alias': 'known'
34+
},
35+
'o': {
36+
'type': 'boolean',
37+
'description': 'Known helpers only',
38+
'alias': 'knownOnly'
39+
},
40+
'm': {
41+
'type': 'boolean',
42+
'description': 'Minimize output',
43+
'alias': 'min'
44+
},
45+
'n': {
46+
'type': 'string',
47+
'description': 'Template namespace',
48+
'alias': 'namespace',
49+
'default': 'Handlebars.templates'
50+
},
51+
's': {
52+
'type': 'boolean',
53+
'description': 'Output template function only.',
54+
'alias': 'simple'
55+
},
56+
'N': {
57+
'type': 'string',
58+
'description': 'Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates.',
59+
'alias': 'name'
60+
},
61+
'i': {
62+
'type': 'string',
63+
'description': 'Generates a template from the passed CLI argument.\n"-" is treated as a special value and causes stdin to be read for the template value.',
64+
'alias': 'string'
65+
},
66+
'r': {
67+
'type': 'string',
68+
'description': 'Template root. Base value that will be stripped from template names.',
69+
'alias': 'root'
70+
},
71+
'p': {
72+
'type': 'boolean',
73+
'description': 'Compiling a partial template',
74+
'alias': 'partial'
75+
},
76+
'd': {
77+
'type': 'boolean',
78+
'description': 'Include data when compiling',
79+
'alias': 'data'
80+
},
81+
'e': {
82+
'type': 'string',
83+
'description': 'Template extension.',
84+
'alias': 'extension',
85+
'default': 'handlebars'
86+
},
87+
'b': {
88+
'type': 'boolean',
89+
'description': 'Removes the BOM (Byte Order Mark) from the beginning of the templates.',
90+
'alias': 'bom'
91+
},
92+
'v': {
93+
'type': 'boolean',
94+
'description': 'Prints the current compiler version',
95+
'alias': 'version'
96+
},
97+
'help': {
98+
'type': 'boolean',
99+
'description': 'Outputs this message'
100+
}
101+
});
102+
109103
argv.files = argv._;
110104
delete argv._;
111105

112-
const Precompiler = require('../dist/cjs/precompiler');
106+
var Precompiler = require('../dist/cjs/precompiler');
113107
Precompiler.loadTemplates(argv, function(err, opts) {
114108

115109
if (err) {
116110
throw err;
117111
}
118112

119113
if (opts.help || (!opts.templates.length && !opts.version)) {
120-
yargs.showHelp();
114+
printUsage(argv._spec, 120);
121115
} else {
122116
Precompiler.cli(opts);
123117
}
124118
});
119+
120+
function pad(n) {
121+
var str = '';
122+
while (str.length < n) {
123+
str += ' ';
124+
}
125+
return str;
126+
}
127+
128+
function parseArgs(spec) {
129+
var opts = { alias: {}, boolean: [], default: {}, string: [] };
130+
131+
Object.keys(spec).forEach(function (arg) {
132+
var opt = spec[arg];
133+
opts[opt.type].push(arg);
134+
if ('alias' in opt) opts.alias[arg] = opt.alias;
135+
if ('default' in opt) opts.default[arg] = opt.default;
136+
});
137+
138+
var argv = require('minimist')(process.argv.slice(2), opts);
139+
argv._spec = spec;
140+
return argv;
141+
}
142+
143+
function printUsage(spec, wrap) {
144+
var wordwrap = require('wordwrap');
145+
146+
console.log('Precompile handlebar templates.');
147+
console.log('Usage: handlebars [template|directory]...');
148+
149+
var opts = [];
150+
var width = 0;
151+
Object.keys(spec).forEach(function (arg) {
152+
var opt = spec[arg];
153+
154+
var name = (arg.length === 1 ? '-' : '--') + arg;
155+
if ('alias' in opt) name += ', --' + opt.alias;
156+
157+
var meta = '[' + opt.type + ']';
158+
if ('default' in opt) meta += ' [default: ' + JSON.stringify(opt.default) + ']';
159+
160+
opts.push({ name: name, desc: opt.description, meta: meta });
161+
if (name.length > width) width = name.length;
162+
});
163+
164+
console.log('Options:');
165+
opts.forEach(function (opt) {
166+
var desc = wordwrap(width + 4, wrap + 1)(opt.desc);
167+
168+
console.log(' %s%s%s%s%s',
169+
opt.name,
170+
pad(width - opt.name.length + 2),
171+
desc.slice(width + 4),
172+
pad(wrap - opt.meta.length - desc.split(/\n/).pop().length),
173+
opt.meta
174+
);
175+
});
176+
}

0 commit comments

Comments
 (0)