<commit_before>var os = require("os");
var commander = require("commander");
var NumberConverter = require("..").NumberConverter;

var appendNewLine;

function run(args, inStream, outStream, options) {
	parseOptions(options);

	var parsedArgs = parseArguments(args);
	var inputNumbers = determineInputNumbers(args);

	var outputString = determineOutputString(parsedArgs, inputNumbers);
	writeResults(outStream, outputString);
}

function parseOptions(options) {
	options = options || {};

	appendNewLine = options.appendNewLine || false;
}

function parseArguments(args) {
	var packageObj = require("../../package.json");
	if(packageObj) {
		commander.version(packageObj.version);
	}

	// Fake adding first two arguments normally found in process.argv
	// Commander appears to expect it
	args = args.slice(0, args.length); // Clone args first
	args.unshift(null, null);

	commander
		.option("-t, --to <toFormat>", "Convert to this number format (binary, octal, decimal, hexadecimal, roman, scientific)")
		.parse(args);

	var argumentsObj = {
		toFormat: commander.to
	};

	return argumentsObj;
}

function determineInputNumbers(args) {
	return [args[args.length - 1]];
}

function determineOutputString(args, inputNumbers) {
	var nc = new NumberConverter(args.toFormat);

	var resultNumbers = [];
	for(var i = 0; i < inputNumbers.length; i++) {
		resultNumbers.push(nc.convert(inputNumbers[i]));
	}

	return resultNumbers.join(os.EOL);
}

function writeResults(outStream, outputString) {
	if(outStream) {
		outStream.write(outputString);

		if(appendNewLine) {
			outStream.write(os.EOL);
		}

		if(outStream !== process.stdout) {
			outStream.end();
		}
	}
}

exports.run = run;<commit_msg>Formalize CLI app options and argument structures in types; some function refactoring<commit_after>var os = require("os");
var commander = require("commander");
var NumberConverter = require("..").NumberConverter;

function run(rawArgs, inStream, outStream, options) {
	var cliAppOptions = new CliAppOptions(options);

	var cliAppArguments = parseArguments(rawArgs);
	var inputNumbers = determineInputNumbers(rawArgs);

	var outputString = determineOutputString(cliAppArguments, inputNumbers);
	writeResults(outStream, outputString, cliAppOptions);
}

function parseArguments(rawArgs) {
	setVersionInCommander(commander);
	parseAppArgumentsUsingCommander(commander, rawArgs);
	return getCliAppArgumentsFromParsedCommander(commander);
}

function setVersionInCommander(cmdr) {
	var packageObj = require("../../package.json");
	if(packageObj) {
		cmdr.version(packageObj.version);
	}
}

function parseAppArgumentsUsingCommander(cmdr, rawArgs) {
	// Fake adding first two arguments normally found in process.argv
	// Commander appears to expect it
	rawArgs = rawArgs.slice(0, rawArgs.length); // Clone first
	rawArgs.unshift(null, null);

	cmdr
		.option("-t, --to <toFormat>", "Convert to this number format (binary, octal, decimal, hexadecimal, roman, scientific)")
		.parse(rawArgs);
}

function getCliAppArgumentsFromParsedCommander(cmdr) {
	var fromFormat = null;
	var toFormat = cmdr.to;

	return new CliAppArguments(fromFormat, toFormat);
}

function determineInputNumbers(args) {
	return [args[args.length - 1]];
}

function determineOutputString(cliAppArguments, inputNumbers) {
	var nc = new NumberConverter(cliAppArguments.fromFormat, cliAppArguments.toFormat);

	var resultNumbers = [];
	for(var i = 0; i < inputNumbers.length; i++) {
		resultNumbers.push(nc.convert(inputNumbers[i]));
	}

	return resultNumbers.join(os.EOL);
}

function writeResults(outStream, outputString, cliAppOptions) {
	if(outStream) {
		outStream.write(outputString);

		if(cliAppOptions.appendNewLine) {
			outStream.write(os.EOL);
		}

		if(outStream !== process.stdout) {
			outStream.end();
		}
	}
}

var CliAppOptions = function(options) {
	options = options || {};

	this.appendNewLine = options.appendNewLine || false;
};

var CliAppArguments = function(fromFormat, toFormat) {
	this.fromFormat = fromFormat;
	this.toFormat = toFormat;
};

exports.run = run;
{'fraction_non_alphanumeric': 0.12592775853537852, 'fraction_numerical': 0.0014844136566056407, 'mean_word_length': 5.4276629570747215, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2679359', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1251, 'n_words': 282}