1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

node.js からのコマンドライン引数を綺麗に受け取る

Posted at
runner.cjs
const parseArgs = () => {
  // 一旦ポジショナルだけパースする
  const { positionals } = util.parseArgs({
    allowPositionals: true
  });

  // パースした値をさらにオプションをつけてパースする
  return util.parseArgs({
    args: positionals,
    allowPositionals: true,
    options: {
      dir: {
        type: "string",
        multiple: true
      },
      mode: {
        type: "string"
      },
      help: {
        type: "boolean"
      }
    }
  });
};

const { values, positionals } = parseArgs();
console.log(values, positionals); // { dir: "./hoge", mode: "dev", help: true } [ "./fuga.json" ] 
bash
node runner.js ./fuga.json --dir ./hoge --mode dev --help
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?