Skip to content

Improvements to CLI parsers #3034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions spec/parsers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
numberParser,
numberOrBoolParser,
booleanParser,
} from '../src/cli/utils/parsers';

describe('parsers', () => {
it('parses correctly with numberParser', () => {
const parser = numberParser('key');
expect(parser(2)).toEqual(2);
expect(parser('2')).toEqual(2);
expect(() => {parser('string')}).toThrow();
});

it('parses correctly with numberOrBoolParser', () => {
const parser = numberOrBoolParser('key');
expect(parser(true)).toEqual(true);
expect(parser(false)).toEqual(false);
expect(parser('true')).toEqual(true);
expect(parser('false')).toEqual(false);
expect(parser(1)).toEqual(1);
expect(parser('1')).toEqual(1);
});

it('parses correctly with booleanParser', () => {
const parser = booleanParser;
expect(parser(true)).toEqual(true);
expect(parser(false)).toEqual(false);
expect(parser('true')).toEqual(true);
expect(parser('false')).toEqual(false);
expect(parser(1)).toEqual(true);
expect(parser(2)).toEqual(false);
});
});
1 change: 1 addition & 0 deletions src/cli/definitions/parse-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export default {
action: booleanParser
},
"cluster": {
env: PARSE_SERVER_CLUSTER,
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
action: numberOrBoolParser("cluster")
},
Expand Down
16 changes: 11 additions & 5 deletions src/cli/utils/parsers.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export function numberParser(key) {
return function(opt) {
opt = parseInt(opt);
if (!Number.isInteger(opt)) {
throw new Error(`The ${key} is invalid`);
const intOpt = parseInt(opt);
if (!Number.isInteger(intOpt)) {
throw new Error(`Key ${key} has invalid value ${opt}`);
}
return opt;
return intOpt;
}
}

Expand All @@ -13,6 +13,12 @@ export function numberOrBoolParser(key) {
if (typeof opt === 'boolean') {
return opt;
}
if (opt === 'true') {
return true;
}
if (opt === 'false') {
return false;
}
return numberParser(key)(opt);
}
}
Expand Down Expand Up @@ -45,7 +51,7 @@ export function moduleOrObjectParser(opt) {
}

export function booleanParser(opt) {
if (opt == true || opt == "true" || opt == "1") {
if (opt == true || opt == 'true' || opt == '1') {
return true;
}
return false;
Expand Down