easy-cli-framework
Version:
A framework for building CLI applications that are robust and easy to maintain. Supports theming, configuration files, interactive prompts, and more.
58 lines (54 loc) • 1.86 kB
JavaScript
var yargsInteractive = require('yargs-interactive');
// @ts-ignore Untyped Module
/**
* Prompts the user to enter a number.
*
* @param {string} prompt The prompt to display to the user
* @param {PromptNumberOptions} options The options for the prompt
*
* @returns {Promise<number>} The number the user entered
*
* @example
* ```typescript
* // Prompt the user to enter a number
* const number = await promptNumber('Enter a number');
* console.log(number);
*
* // Prompt the user to enter a number between 1 and 10
* const number = await promptNumber('Enter a number between 1 and 10', {
* validator: (input) => input >= 1 && input <= 10,
* validationErrorMessage: 'Number must be between 1 and 10',
* });
* console.log(number);
*
* // Prompt the user to enter a number using a custom theme
* const number = await promptNumber('Enter a number', {
* theme: new EasyCLITheme(),
* promptTheme: 'info',
* });
* console.log(number);
* ```
*
*/
const promptNumber = async (prompt, { value = null, validator = (input) => true, validationErrorMessage = 'Invalid input', theme = null, promptTheme = 'default', } = {}) => {
var _a;
while (true) {
const { choice } = await yargsInteractive()
.usage('$0 <command> [args]')
.interactive({
interactive: { default: true },
choice: {
type: 'number',
describe: (_a = theme === null || theme === undefined ? undefined : theme.formattedString(prompt, promptTheme)) !== null && _a !== undefined ? _a : prompt,
default: value,
prompt: 'always',
},
});
if (!validator || validator(choice)) {
return choice;
}
console.log(validationErrorMessage);
}
};
exports.promptNumber = promptNumber;
;