I want to print a number in caporal js. But the result not i want because return true/false. I want to if not exist passing a parameter number,default value is 32. This is my code
#!/usr/bin/env node
const { program } = require("@caporal/core");
program
.command("random", "Generate random alphanumeric")
.option("--length", "length of number", program.NUMERIC, 32)
.action(({ logger, args, options }) => {
let length = options.length;
let letters = options.letters
logger.info(length);
});
program.run();
if i run on terminal
let say
./app.js random
will be result 32
if i run ./app.js random --length=10
will be result 10
i have tried found the problem, but i cant. How i must repair my code so appropriated with i want?
You can use this code:
#!/usr/bin/env node
const { program } = require("@caporal/core");
program
.command("random", "Generate random alphanumeric")
.option("--length <num>", "length of number", {
validator: program.NUMBER,
default: 32,
})
.action(({ logger, args, options }) => {
let length = options.length;
let letters = options.letters
logger.info(length);
});
program.run();
options in .option there are 3 arguments, including:
You can change --length to --length <num> to add the number value after the --length command. Then, you remove program.NUMERIC, 32 and add this code
{
validator: program.NUMBER,
defaults: 32,
}
to define a validator into the number data type and contains the default number is 32.