javascript - How do I pass arguments to a self-executing module.exported anonymous node function? -
i'm trying call self-executing anonymous function exported module.exports parameters object nodejs file requiring file anonymous function , passing parameters object this:
filethatcallstheanonymousselfexecutingfunction: `
var parameters = { filename: "usernamesandqueues", headers: ["username", "password", "queue"], columns: ["ascii", "ascii", "ascii"], wordlength: 25, count: 100 }; var generatedata = require("../scripts/generatedata.js")(parameters);
generatedata.js:
// if function not call in file, can pass // arguments normally, , prints out expected options. module.exports = (function (options) { console.log("hello %s", json.stringify(options)); options = options || { "headers": ["username", "password", "queuename"], "columns": ["ascii", "ascii", "ascii"], "wordlength": 26, "count": 101 }; console.log("current options are: %s", json.stringify(options)); // want know arguments now, interrupt process.exit(); // want moment print out options object // later on, use options parse arguments ??? options[0], options[1], options[2] etc. // rest of file needs function arguments console.log("something"); var fs = require('fs'), generate = require('csv-generate'), filename = "usernamesandqueues", csvfilestream = fs.createwritestream("../data/" + "generated" + filename + ".csv", { flags: 'w' }), error = fs.createwritestream(__dirname + '/node.error.log', {flags: 'w'}), // csv generator (read command line arguments, create defaults, making filename required) generator = generate({ header: options.headers, columns: options.columns, max_word_length: options.wordlength, length: options.count }); // redirect stdout / stderr file process.stdout.write = csvfilestream.write.bind(csvfilestream); process.stderr.write = error.write.bind(error); // handle uncaught exceptions / errors process.on('uncaughtexception', function(err){ console.error((err && err.stack) ? err.stack : err); }); // output csv stdout stream -> file generator.pipe(process.stdout); })(arguments[0]);
i'm asking correct way of handling arguments passed function ? want way in order call cmd line, well, (optional) command line arguments. after figure out how way, can use argument parser, further.
if have other suggestions of implementing node script (function) can run command line script (node generatedata.js --headers ["param1", "param2", "param3"] --columns ["ascii", "ascii", "ascii"] --wordlength 25 --count 100
, required module exported function file 1 , passing parameter object above (var generatedata = require("../scripts/generatedata.js")(parameters);
), please write witty response!
other questions: doing right, doing wrong, , can read better grasp these concepts? i've looked on mozilla dev , other sites. i've found things regarding self-executing anonymous functions parameters, none called external file. maybe i'm not searching right thing...
later edit: have tried exporting function , calling file. easy way.
scriptthatrequirestheexportedmoduleandcallsitwithparameters(arguments).js
var parameters = { headers: ["username", "password", "queue"], columns: ["ascii", "ascii", "ascii"], word_length: 25, count: 100 }; // works var generateusernames = require('../scripts/generatedata.js')(parameters);
generatedata.js
// works, exporting function, without self-executing (calling) // call function (providing arguments) other file requiring module.exports = function (options) { console.log("hello %s", json.stringify(options)); options = options || { "headers": ["username", "password", "queuename"], "columns": ["ascii", "ascii", "ascii"], "wordlength": 26, "count": 101 }; console.log("current options are: %s", json.stringify(options)); // exiting, because, now, want know arguments (options) process.exit(); console.log("something"); var fs = require('fs'), generate = require('csv-generate'), filename = "usernamesandqueues", csvfilestream = fs.createwritestream("../data/" + "generated" + filename + ".csv", { flags: 'w' }), error = fs.createwritestream(__dirname + '/node.error.log', {flags: 'w'}), // csv generator (read command line arguments, create defaults, making filename required) generator = generate({ header: options.headers, columns: options.columns, max_word_length: options.wordlength, length: options.count }); // redirect stdout / stderr file process.stdout.write = csvfilestream.write.bind(csvfilestream); process.stderr.write = error.write.bind(error); // handle uncaught exceptions / errors process.on('uncaughtexception', function(err){ console.error((err && err.stack) ? err.stack : err); }); // output csv stdout stream -> file generator.pipe(process.stdout); };
as jfriend00 pointed out, "... [your code] executes immediately, not later when uses module decides call it." need assign function module.exports
require
use in expected way.
to "command line node generatedata.js [list of arguments]" behavior you're looking for, can add conditional checks if context cli, , invoke function, or export if not:
var fancyfn = function(options) { ... }; if (require.main === module) { fancyfn(arguments[0]); } else { module.exports = fancyfn; }
Comments
Post a Comment