Arrays using commander

Hi guys!

I need help in commander node.js library. I need to create this CLI that accepts 3 flags, --input, --output and --pattern, for example:

commander .version('3.0.0') .usage('[options] <file ...>') .option('-i, --input', 'Array of files to be extracted') .option('-o, --output', 'Output file name') .option('-p, --pattern', 'Pattern name to be used in the extraction') .parse(process.argv); 

My problem is with the input flag. I need to send several files, for this I need an array data type.

The problem is that I just can't figure out how to do this:

 node ./bin/extract -i ../files/*.PDF 

Become an array with all my files located in my file directory. I am already trying to run each sample in the documentation, and I have not found a solution for my problem. Also, I searched in the problems and didn’t find one ... that is strange, maybe I'm doing something wrong, and you guys could help?

Thanks!

+5
source share
1 answer

You can use Coercion to achieve this:

 function scanDir(val) { files = fs.readdirSync(val); return files; } program .version('0.0.1') .option('-s, --scan [value]', '', scanDir) .parse(process.argv); console.log(' scan: %j', program.scan); 

And call it that:

 node app.js -s /foo 
+2
source

Source: https://habr.com/ru/post/1242012/


All Articles