Node.js yargsの使い方
yargs
https://www.npmjs.com/package/yargs
yargs-parser
https://www.npmjs.com/package/yargs-parser
yargsとyargs-parserの違い
yargsがyargs-parserを包括している感じでしょうか?実際の挙動を見てみます。
import yargsp from "yargs-parser"; import yargs from "yargs"; import process from "process";
console.log(yargs(process.argv.slice(2))); console.log(yargs(process.argv.slice(2)).argv); console.log(yargsp(process.argv.slice(2)));
$ node app.mjs add --title="Things to buy"
YargsInstance {
customScriptName: false,
parsed: false,
'$0': 'app-chap16.mjs',
argv: [Getter]
}
{ _: [ 'add' ], title: 'Things to buy', '$0': 'app-chap16.mjs' }
{ _: [ 'add' ], title: 'Things to buy' }- yargsは引数を与えることでYargInstanceというオブジェクトになります。
- yargsの引数パース結果はyargs.argvで参照できます
- yargs-parserはyargsのそれとは若干結果が異なります
- yargsはパース結果に実行ファイルそのものを含むのに対してyargs-parserは明示的に与えた引数のみを結果に含みます
実装例1. yargsを使った基本的なパース
コマンドラインからコマンドをパースさせます。
yargs(process.argv.slice(2))
.usage('Usage: $0 <command> [options]')
.command('count', 'Count the files in a directory', function (yargs) {
console.log('count items!')
})
.command('list', 'list files of a directory:', function (yargs) {
console.log('list items!')
})
.example('$0 count', 'count the lines in the given file')
.help('h')
.alias('h', 'help')
.argv;以下のコマンドを実行します。想定通りの結果になっているでしょうか?
$ node app.mjs count count items!$ node app.mjs list list items!
$ node app.mjs —help Usage: app.mjs <command> [options]
Commands: app.mjs count Count the files in a directory app.mjs list list files of a directory:
Options: —version Show version number [boolean] -h, —help Show help [boolean]
Examples: app.mjs count count the lines in the given file
実装例2. コマンドオプションの設定
countやlistといったコマンドにオプションを追加してやります。以前はbuilderとしてオプションを与えていたみたいですが・・・。
yargs(process.argv.slice(2))
.usage('Usage: $0 <command> [options]')
.command('count', 'Count the files in a directory', function (yargs) {
return yargs.option('directory', {
alias: 'd',
default: './'
})
console.log('count items!')
})
.command('list', 'list files of a directory:', function (yargs) {
return yargs.option('directory', {
alias: 'd',
default: './'
})
console.log('list items!')
})
.example('$0 count', 'count the lines in the given file')
.help('h')
.alias('h', 'help')
.argv;
以下コマンドを実行してみます。countのみ確認してますが無事オプションは追加されたようです。
$ node app.mjs count --help app.mjs countCount the files in a directory
Options: —version Show version number [boolean] -h, —help Show help [boolean] -d, —directory [default: ”./”]
しかしながらコマンド実行後の挙動が何もないのは寂しいのでコードを変更して実際の挙動を確認していきます。addコマンドのtitleオプションを指定してコンソール上にタイトルを表示させます。
yargs(process.argv.slice(2))
.usage('Usage: $0 <command> [options]')
.command('add', 'add note',
function (yargs) {
return yargs.option('title', {
alias: 't',
default: '',
demandOption: true})
},
function(argv){
console.log(`Title: ${argv.title}`)
}
)
.example('$0 add --title hoge', 'add title "hoge"')
.help('h')
.alias('h', 'help')
.argv;以下コマンドを実行していきます。
$ node app.mjs add --help app.mjs addadd note
Options: —version Show version number [boolean] -h, —help Show help [boolean] -t, —title [required] [default: ""]
$ node app.mjs add —title hoge Title: hoge
まとめ・所感
- yargsは多機能パーサー、yargs-parserは単機能パーサーといった理解に落ち着くとします