Truffle CLI exec with arguments

Jul 14, 2021 · Dung Huynh

Context

truffle exec doesn't support CLI arguments natively. Use yargs-parser to parse process.argv in your script.

Usage

const argv = require("yargs-parser")(process.argv.slice(2));

module.exports = async (callback) => {
  const name = argv?.n || "World";
  callback(`Hello ${name}`);
};
truffle exec hello.js -n=Dung --network development
  • -n=Dung: Custom argument
  • --network: Standard Truffle option (still works)