Skip to content

Commit 773208c

Browse files
committed
chore(cli): throw errors on unknown flags
clsoes angular#3216
1 parent c194af8 commit 773208c

File tree

1 file changed

+88
-40
lines changed

1 file changed

+88
-40
lines changed

lib/cli.ts

Lines changed: 88 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,97 @@ process.argv.slice(2).forEach(function(arg: string) {
3131
}
3232
});
3333

34-
optimist
35-
.usage(
36-
'Usage: protractor [configFile] [options]\n' +
37-
'configFile defaults to protractor.conf.js\n' +
38-
'The [options] object will override values from the config file.\n' +
39-
'See the reference config for a full list of options.')
40-
.describe('help', 'Print Protractor help menu')
41-
.describe('version', 'Print Protractor version')
42-
.describe('browser', 'Browsername, e.g. chrome or firefox')
43-
.describe('seleniumAddress', 'A running selenium address to use')
44-
.describe('seleniumSessionId', 'Attaching an existing session id')
45-
.describe('seleniumServerJar', 'Location of the standalone selenium jar file')
46-
.describe('seleniumPort', 'Optional port for the selenium standalone server')
47-
.describe('baseUrl', 'URL to prepend to all relative paths')
48-
.describe('rootElement', 'Element housing ng-app, if not html or body')
49-
.describe('specs', 'Comma-separated list of files to test')
50-
.describe('exclude', 'Comma-separated list of files to exclude')
51-
.describe('verbose', 'Print full spec names')
52-
.describe('stackTrace', 'Print stack trace on error')
53-
.describe('params', 'Param object to be passed to the tests')
54-
.describe('framework', 'Test framework to use: jasmine, mocha, or custom')
55-
.describe('resultJsonOutputFile', 'Path to save JSON test result')
56-
.describe('troubleshoot', 'Turn on troubleshooting output')
57-
.describe('elementExplorer', 'Interactively test Protractor commands')
58-
.describe('debuggerServerPort', 'Start a debugger server at specified port instead of repl')
59-
.alias('browser', 'capabilities.browserName')
60-
.alias('name', 'capabilities.name')
61-
.alias('platform', 'capabilities.platform')
62-
.alias('platform-version', 'capabilities.version')
63-
.alias('tags', 'capabilities.tags')
64-
.alias('build', 'capabilities.build')
65-
.alias('grep', 'jasmineNodeOpts.grep')
66-
.alias('invert-grep', 'jasmineNodeOpts.invertGrep')
67-
.alias('explorer', 'elementExplorer')
68-
.string('capabilities.tunnel-identifier')
69-
.check(function(arg: any) {
70-
if (arg._.length > 1) {
71-
throw new Error('Error: more than one config file specified');
72-
}
73-
});
34+
export interface Option {
35+
key: string;
36+
value?: string;
37+
}
38+
39+
let describes: Option[] = [
40+
{key: 'help', value: 'Print Protractor help menu'},
41+
{key: 'version', value: 'Print Protractor version'},
42+
{key: 'browser', value: 'Browsername, e.g. chrome or firefox'},
43+
{key: 'seleniumAddress', value: 'A running selenium address to use'},
44+
{key: 'seleniumSessionId', value: 'Attaching an existing session id'},
45+
{key: 'seleniumServerJar', value: 'Location of the standalone selenium jar file'},
46+
{key: 'seleniumPort', value: 'Optional port for the selenium standalone server'},
47+
{key: 'baseUrl', value: 'URL to prepend to all relative paths'},
48+
{key: 'rootElement', value: 'Element housing ng-app, if not html or body'},
49+
{key: 'specs', value: 'Comma-separated list of files to test'},
50+
{key: 'exclude', value: 'Comma-separated list of files to exclude'},
51+
{key: 'verbose', value: 'Print full spec names'},
52+
{key: 'stackTrace', value: 'Print stack trace on error'},
53+
{key: 'params', value: 'Param object to be passed to the tests'},
54+
{key: 'framework', value: 'Test framework to use: jasmine, mocha, or custom'},
55+
{key: 'resultJsonOutputFile', value: 'Path to save JSON test result'},
56+
{key: 'troubleshoot', value: 'Turn on troubleshooting output'},
57+
{key: 'elementExplorer', value: 'Interactively test Protractor commands'},
58+
{key: 'debuggerServerPort', value: 'Start a debugger server at specified port instead of repl'}
59+
];
60+
61+
let aliases: Option[] = [
62+
{key: 'browser', value: 'capabilities.browserName'}, {key: 'name', value: 'capabilities.name'},
63+
{key: 'platform', value: 'capabilities.platform'},
64+
{key: 'platform-version', value: 'capabilities.version'},
65+
{key: 'tags', value: 'capabilities.tags'}, {key: 'build', value: 'capabilities.build'},
66+
{key: 'grep', value: 'jasmineNodeOpts.grep'},
67+
{key: 'invert-grep', value: 'jasmineNodeOpts.invertGrep'},
68+
{key: 'explorer', value: 'elementExplorer'}
69+
];
70+
71+
let strings: Option[] = [{key: 'capabilities.tunnel-identifier'}];
72+
73+
optimist.usage(
74+
'Usage: protractor [configFile] [options]\n' +
75+
'configFile defaults to protractor.conf.js\n' +
76+
'The [options] object will override values from the config file.\n' +
77+
'See the reference config for a full list of options.');
78+
for (let desc of describes) {
79+
optimist.describe(desc.key, desc.value);
80+
}
81+
for (let alias of aliases) {
82+
optimist.alias(alias.key, alias.value);
83+
}
84+
for (let str of strings) {
85+
optimist.string(str.key);
86+
}
87+
optimist.check(function(arg: any) {
88+
if (arg._.length > 1) {
89+
throw new Error('Error: more than one config file specified');
90+
}
91+
});
7492

7593
let argv: any = optimist.parse(args);
7694

95+
// Check to see if additional flags were used.
96+
let unknownKeys: string[] = [];
97+
for (let key of Object.keys(argv).slice(2)) {
98+
if (key === '$0') {
99+
continue;
100+
}
101+
let found = false;
102+
for (let desc of describes) {
103+
if (desc.key === key) {
104+
found = true;
105+
break;
106+
}
107+
}
108+
for (let alias of aliases) {
109+
if (found) {
110+
break;
111+
}
112+
if (alias.key === key) {
113+
found = true;
114+
break;
115+
}
116+
}
117+
if (!found) {
118+
unknownKeys.push(key);
119+
}
120+
}
121+
if (unknownKeys.length > 0) {
122+
throw new Error('Found extra flags: ' + unknownKeys.join(', '));
123+
}
124+
77125
if (argv.help) {
78126
optimist.showHelp();
79127
process.exit(0);

0 commit comments

Comments
 (0)