// Author: Theodore Zacharia
// V0.1: 03/08/2021 - Initial Release
//
// This script provides a template to start all other scripts with
// supports arguments being passed
// Usage: node script_template.js -t --msg=hello

var trace = 0;

function getArgs () {
    const args = {};
    process.argv
        .slice(2, process.argv.length)
        .forEach( arg => {
        // long arg
        if (arg.slice(0,2) === '--') {
            const longArg = arg.split('=');
            const longArgFlag = longArg[0].slice(2,longArg[0].length);
            const longArgValue = longArg.length > 1 ? longArg[1] : true;
            args[longArgFlag] = longArgValue;
        }
        // flags
        else if (arg[0] === '-') {
            const flags = arg.slice(1,arg.length).split('');
            flags.forEach(flag => {
            args[flag] = true;
            });
        }
    });

    // check specific args being set
    process.argv.indexOf('-t') > -1 ? trace = 1 : trace = 0 ; 
    return args;
}

const args = getArgs();
console.log(args);
// the following is useful when there is an object that you want to format as proper json with quotes
// console.log(JSON.stringify(args)); 

if ( trace > 0 ) {

	process.argv.forEach(function (val, index, array) {
	  console.log(index + ': ' + val);
	});
}