How to use the InvalidArgumentError function from commander
Find comprehensive JavaScript commander.InvalidArgumentError code examples handpicked from public code repositorys.
commander.InvalidArgumentError is an error type in the Commander library for Node.js that is thrown when a command-line argument is invalid or missing.
146 147 148 149 150 151 152 153 154 155
.requiredOption( '-u, --user <email>', 'the e-mail address you used to register with Managed Runtime', (val) => { if (!validator.isEmail(val)) { throw new program.InvalidArgumentError(`"${val}" is not a valid email`) } else { return val } }
+ 7 other calls in file
65 66 67 68 69 70 71 72 73 74
return { match, [handler]: mapping } } catch (e) { throw new InvalidArgumentError('Invalid mapping') } } function getCommand (cwd) {
+ 9 other calls in file
How does commander.InvalidArgumentError work?
The commander.InvalidArgumentError error type in the Commander library for Node.js is used to indicate that a command-line argument is invalid or missing. When a command-line argument is found to be invalid or missing during argument parsing, Commander throws an instance of commander.InvalidArgumentError. This error type extends the built-in JavaScript Error object, and includes additional properties such as message and code that provide more information about the error. The commander.InvalidArgumentError error can be caught and handled using standard JavaScript error handling mechanisms, such as try/catch blocks or process.on('uncaughtException', ...). To provide a custom error message for an InvalidArgumentError, you can use the Command#exitOverride() method to define a custom error handler function that will be called when the error occurs. The error handler function should return a non-zero exit code to indicate failure, and can provide a custom error message or other error handling logic as needed. In summary, commander.InvalidArgumentError is an error type in the Commander library for Node.js that is thrown when a command-line argument is invalid or missing, and can be caught and handled using standard JavaScript error handling mechanisms.
13 14 15 16 17 18 19 20 21 22
exports.Argument = commander.Argument; exports.Command = commander.Command; exports.CommanderError = commander.CommanderError; exports.Help = commander.Help; exports.InvalidArgumentError = commander.InvalidArgumentError; exports.InvalidOptionArgumentError = commander.InvalidArgumentError; // Deprecated exports.Option = commander.Option; // In Commander, the create routines end up being aliases for the matching
+ 3 other calls in file
15 16 17 18 19 20 21 22 23
export const parseIntArg = (value: string) => { // parseInt takes a string and a radix const parsedValue = parseInt(value, 10); if (isNaN(parsedValue)) { throw new commander.InvalidArgumentError('Not a number.'); } return parsedValue; };
+ 7 other calls in file
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
const { Command } = require("commander"); // Define a command with required argument const program = new Command(); program.arguments(" ").action(function (filename) { if (!filename.endsWith(".txt")) { throw new commander.InvalidArgumentError( "Invalid filename: must end with .txt" ); } console.log(`Reading file: ${filename}`); }); // Parse command-line arguments and execute command try { program.parse(process.argv); } catch (err) { if (err instanceof commander.InvalidArgumentError) { console.error(`Error: ${err.message}`); process.exitCode = 1; } else { throw err; } }
In this example, we define a command using the Commander library that takes a single required argument representing a filename. If the filename does not end with the string ".txt", we throw an instance of commander.InvalidArgumentError with a custom error message. We then parse the command-line arguments using program.parse() and wrap the call in a try/catch block. If an InvalidArgumentError is thrown, we catch the error, print a custom error message to the console, and set the process exit code to 1 to indicate failure. If any other type of error is thrown, we re-throw the error so that it can be caught by other error handlers in the program.
127 128 129 130 131 132 133 134 135 136
} const parseBool = (value) => { if (value === 'true') return true; if (value === 'false') return false; throw new commander.InvalidArgumentError('Not a boolean.'); } const log = (message) => { console.log(`${chalk.cyan.bold("ℹ Info")} ${chalk.cyan(message ?? "")}`);
+ 11 other calls in file
45 46 47 48 49 50 51 52 53 54 55
return value; } function parseProjectName(value) { if (!isProjectName(value)) { throw new InvalidArgumentError('Please inform a valid project name, like <project-name> or <project_name>.'); } return value; }
+ 5 other calls in file
GitHub: whitlockjc/m8-js
65 66 67 68 69 70 71 72 73 74
const m8File = loadM8File(bytesFromDisk) const m8FileType = M8File.typeToStr(m8File.m8FileType) if (typeof type === 'string') { if (m8FileType !== type) { throw new commander.InvalidArgumentError(`m8-file must be a ${type} file`) } } else { if (typeof type !== 'undefined' && type.indexOf(m8FileType) === -1) { throw new commander.InvalidArgumentError(`m8-file must be a ${type.join(' or ')} file`)
+ 31 other calls in file
GitHub: timrogers/litra
48 49 50 51 52 53 54 55 56 57
* @returns {number} The option value parsed as an integer */ const parseIntOption = (value) => { const parsedValue = parseInt(value); if (isNaN(parsedValue)) { throw new commander.InvalidArgumentError('The value must be an integer.'); } return parsedValue; }; exports.parseIntOption = parseIntOption;
128 129 130 131 132 133 134 135 136 137
program .command('start') .argument('<port>', 'local server port number', (value) => { const port = parseInt(value, 10); if (isNaN(port)) { throw new InvalidArgumentError('Not a number.'); } return port; }) .option('-p, --profile <string>', 'setting profile name', 'default')
27 28 29 30 31 32 33 34 35 36
(slug) => { const sanitizedSlug = kebabCase(slug) // Per WordPress' requirements, the slug cannot exceed 32 characters. if (sanitizedSlug.length > 32) { throw new InvalidArgumentError("'slug' must not exceed 32 characters") } return sanitizedSlug }
24 25 26 27 28 29 30 31 32 33
(slug) => { const sanitizedSlug = kebabCase(slug) // Per WordPress' requirements, the slug cannot exceed 20 characters. if (sanitizedSlug.length > 20) { throw new InvalidArgumentError('SLUG must not exceed 20 characters') } return sanitizedSlug }
GitHub: JamesTeague/ghost-writer
40 41 42 43 44 45 46 47 48 49
const logger = (0, stoolie_1.default)(stoolie_1.LogLevel.INFO); const watcher = (0, services_1.createWatcher)(client, publisher, logger); const parseIntFromArgument = (value) => { const parsedValue = parseInt(value, 10); if (isNaN(parsedValue)) { throw new commander_1.InvalidArgumentError('Not a number.'); } return parsedValue; }; deleteCommand
+ 2 other calls in file
GitHub: Taliss/Chatter
19 20 21 22 23 24 25 26 27 28 29
}; const parseToInt = (value) => { const parsedValue = parseInt(value, 10); if (isNaN(parsedValue)) { throw new commander.InvalidArgumentError('Not a number'); } return parsedValue; };
commander.Option is the most popular function in commander (1786 examples)