-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: print usage menu for an unknown flag (#130)
When an unknown option was present it would only print a single-line error message stating that the flag was unknwon. Now it also prints the usage menu. This was done by overriding the `commander` code for unknown options handling.
- Loading branch information
Showing
5 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const program = require('commander'); | ||
|
||
// This module is used to modify the commander code. When | ||
// an unsupported option is given, only a single-line error | ||
// is printed. Now, it will also print out the usage menu. | ||
|
||
// deletes the function in order to reimplement it | ||
delete program['unknownOption']; | ||
|
||
// reimplementing the funciton | ||
program.unknownOption = function(flag) { | ||
if (this._allowUnknownOption) return; | ||
console.error(); | ||
console.error(" error: unknown option `%s'", flag); | ||
console.error(); | ||
|
||
// this is the extra code added to the function | ||
this.outputHelp(); | ||
|
||
process.exit(1); | ||
}; | ||
|
||
module.exports = program; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters