Felhasználói eszközök

Eszközök a webhelyen


oktatas:web:nodejs:yargs

< NodeJS

Parancssori argumentumok

Bevezetés

Többféle csomag érhető el a célhoz:

  • yargs
  • commands

Telepítés

npm i yargs

Argumentumok

valami.js
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const cli = yargs(hideBin(process.argv)).argv
 
console.log(cli)

Lehetséges futtatás, eredménnyel:

node valami.js egy kettő
{ _: [ 'egy', 'kettő' ], '$0': 'valami.js' }

Kapunk egy tömböt „_” néven. Ebben találjuk a paramétereket. A $0 tulajdonságban a script nevét.

console.log(cli._)
console.log(cli.$0)

Külön fájlba

cli.js
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const cli = yargs(hideBin(process.argv));
 
module.exports = cli;

Használata:

valami.js
const cli = require('./cli');
 
function init() {
    console.log('init megy ...');
}
 
cli.command('init', 'Initilize the project', {}, init);

Futtatás:

node valami.js init
oktatas/web/nodejs/yargs.txt · Utolsó módosítás: 2024/04/11 20:49 szerkesztette: admin