Can I pass argument to js script executed using "truffle exec"?
Example:
script.js
module.exports = (callback, arg) => {
console.log(arg);
callback();
}
exeucution
truffle exec "./script.js" "argument"
expected output
argument
actual output
undefined
You can use process.argv for this, but note the offset - I believe this will be interpreted as node /path/to/truffle/cli.js exec <args...>, so args start at index 4.
Closing this. Feel free to open a new issue for feature requests to expand on this, if it doesn't meet your needs. Thanks!
EDIT: I got it working just running truffle exec outside of the truffle console.
Hi @gnidan . I ran into the same problem today, where I want to extract the one command line argument inputted via the truffle console:
truffle console
exec ./script.js hash
I want to extract the hash
But I get the following argvs:
process.argv[0]: /usr/local/Cellar/node@10/10.16.0/bin/node
process.argv[1]: /usr/local/bin/truffle
process.argv[2]: console
process.argv[3]: undefined
So somehow the command line argument input after exec ./script.js does not make it into process.argv.
Any chance you can help me extracting command line arguments from truffle console exec ?
@bytezantium can you try exec script.js hash while in the console?
bump to reopen. It's quite annoying to deal with potential variations in executing like:
truffle exec script.js arg1
truffle exec script.js --network live arg1
truffle exec script.js --network live --compile arg1
truffle exec script.js arg1 --network live --compile
These should really all do the same thing as far as the script is concerned. It'd be nice to make it easier on the developer and provide an array of arguments. I'd support a function signature that looked like this...
module.exports = function(callback, CLIArgs) {}
@bytezantium can you try
exec script.js hashwhile in the console?
For me that does not work
In truffle console i see no parameters from process.argv
truffle(development)> exec moveTime.js 10000 1000
Using network 'development'.
0: C:\......................\node.exe
1: C:\Users\.................\npm\node_modules\truffle\build\cli.bundled.js
2: console
Most helpful comment
bump to reopen. It's quite annoying to deal with potential variations in executing like:
These should really all do the same thing as far as the script is concerned. It'd be nice to make it easier on the developer and provide an array of arguments. I'd support a function signature that looked like this...