Skip to content

Commit

Permalink
Show help on unknown option 837 (#1615)
Browse files Browse the repository at this point in the history
* cli: if user passed unknown option, show help and exit, close #837

* cli: test unknown option output

* linting
  • Loading branch information
bahmutov authored and jennifer-shehane committed May 15, 2018
1 parent eaa182b commit 1f1935b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
81 changes: 81 additions & 0 deletions cli/__snapshots__/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,84 @@ exports['cli -v no binary version 1'] = `
Cypress package version: 1.2.3
Cypress binary version: not installed
`

exports['shows help for open --foo 1'] = `
command: bin/cypress open --foo
code: 1
failed: true
killed: false
signal: null
timedOut: false
stdout:
-------
error: unknown option: --foo
Usage: open [options]
Opens Cypress in the interactive GUI.
Options:
-p, --port <port> runs Cypress on a specific port. overrides any value in cypress.json.
-e, --env <env> sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json
-c, --config <config> sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.
-d, --detached [bool] runs Cypress application in detached mode
-P, --project <project path> path to the project
--global force Cypress into global mode as if its globally installed
--dev runs cypress in development and bypasses binary check
-h, --help output usage information
-------
stderr:
-------
-------
`

exports['shows help for run --foo 1'] = `
command: bin/cypress run --foo
code: 1
failed: true
killed: false
signal: null
timedOut: false
stdout:
-------
error: unknown option: --foo
Usage: run [options]
Runs Cypress tests from the CLI without the GUI
Options:
--record [bool] records the run. sends test results, screenshots and videos to your Cypress Dashboard.
--headed displays the Electron browser instead of running headlessly
-k, --key <record-key> your secret Record Key. you can omit this if you set a CYPRESS_RECORD_KEY environment variable.
-s, --spec <spec> runs a specific spec file. defaults to "all"
-r, --reporter <reporter> runs a specific mocha reporter. pass a path to use a custom reporter. defaults to "spec"
-o, --reporter-options <reporter-options> options for the mocha reporter. defaults to "null"
-p, --port <port> runs Cypress on a specific port. overrides any value in cypress.json.
-e, --env <env> sets environment variables. separate multiple values with a comma. overrides any value in cypress.json or cypress.env.json
-c, --config <config> sets configuration values. separate multiple values with a comma. overrides any value in cypress.json.
-b, --browser <browser-name> runs Cypress in the browser with the given name. note: using an external browser will not record a video.
-P, --project <project-path> path to the project
--group flag to group individual runs by using common --group-id
--group-id <group-id> optional common id to group runs by, extracted from CI environment variables by default
--dev runs cypress in development and bypasses binary check
-h, --help output usage information
-------
stderr:
-------
-------
`
12 changes: 12 additions & 0 deletions cli/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const debug = require('debug')('cypress:cli')
const util = require('./util')
const logger = require('./logger')

// patch "commander" method called when a user passed an unknown option
// we want to print help for the current command and exit with an error
commander.Command.prototype.unknownOption = function (flag) {
if (this._allowUnknownOption) return
logger.error()
logger.error(' error: unknown option:', flag)
logger.error()
this.outputHelp()
logger.error()
process.exit(1)
}

const coerceFalse = (arg) => {
return arg !== 'false'
}
Expand Down
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"nock": "^9.0.9",
"shelljs": "0.7.8",
"sinon": "3.2.1",
"snap-shot-it": "4.0.1",
"snap-shot-it": "4.1.4",
"strip-ansi": "4.0.0"
},
"files": [
Expand Down
15 changes: 15 additions & 0 deletions cli/test/lib/cli_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ describe('cli', function () {
this.exec = (args) => cli.init(`node test ${args}`.split(' '))
})

context('unknown option', () => {
// note it shows help for that specific command
it('shows help', () =>
execa('bin/cypress', ['open', '--foo']).then((result) => {
snapshot('shows help for open --foo', result)
})
)

it('shows help for run command', () =>
execa('bin/cypress', ['run', '--foo']).then((result) => {
snapshot('shows help for run --foo', result)
})
)
})

context('help command', () => {
it('shows help', () =>
execa('bin/cypress', ['help']).then(snapshot)
Expand Down

0 comments on commit 1f1935b

Please sign in to comment.