Skip to content

Commit 5fb3f06

Browse files
authored
Merge pull request #904 from macbre/decamelize-options
CLI - make sure options are not "camelCased" but "have-dashes" instead
2 parents 580273e + 69bee57 commit 5fb3f06

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

bin/phantomas.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"use strict";
1111

1212
const { program } = require("commander"),
13+
{ decamelizeOptions } = require("./utils"),
1314
phantomas = require(".."),
1415
debug = require("debug")("phantomas:cli");
1516

@@ -157,7 +158,9 @@ program
157158

158159
// parse it
159160
program.parse(process.argv);
160-
var options = program.opts();
161+
162+
// make sure options are not "camelCased" but "have-dashes" instead (issue #863)
163+
var options = decamelizeOptions(program.opts());
161164

162165
debug("argv: %j", process.argv);
163166
debug("opts: %j", options);

bin/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const decamelize = require("decamelize");
2+
3+
function decamelizeOptions(options) {
4+
// decamelize option names as returned by commander (see issue #863)
5+
let decamelized = {};
6+
7+
for (const [key, value] of Object.entries(options)) {
8+
decamelized[decamelize(key, { separator: "-" })] = value;
9+
}
10+
11+
return decamelized;
12+
}
13+
14+
module.exports = {
15+
decamelizeOptions,
16+
};

package-lock.json

Lines changed: 17 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"commander": "^7.0.0",
3636
"csv-string": "^4.0.1",
3737
"debug": "^4.1.1",
38+
"decamelize": "^5.0.0",
3839
"js-yaml": "^4.0.0",
3940
"node-statsd": "0.1.1",
4041
"puppeteer": "^8.0.0"

test/bin-utils-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Integration tests using server-start.sh script
3+
*/
4+
const vows = require("vows"),
5+
assert = require("assert"),
6+
{ decamelizeOptions } = require("../bin/utils");
7+
8+
vows
9+
.describe("bin utils")
10+
.addBatch({
11+
decamelizeOptions: {
12+
topic: function () {
13+
return decamelizeOptions({
14+
url: "http://foo.com",
15+
userAgent: "foo/bar",
16+
});
17+
},
18+
"should decamelize options": function (opts) {
19+
assert.deepStrictEqual(opts, {
20+
url: "http://foo.com",
21+
"user-agent": "foo/bar",
22+
});
23+
},
24+
},
25+
})
26+
.export(module);

0 commit comments

Comments
 (0)