#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var parser = require('./lib/parser');
var readToEnd = require('./lib/read').readToEnd;


if(process.argv[2] === "--help" || process.argv[2] === "-h"){
    console.log("Fast XML Parser");
    console.log("----------------");
    console.log("xml2js [-ns|-a] <filename> [-o outputfile.json]");
}else if(process.argv[2] === "--version"){
    console.log(require(path.join(__dirname + "/package.json")).version);
}else{
    var options = {
        ignoreNameSpace : true,
        ignoreNonTextNodeAttr : false,
        ignoreTextNodeAttr : false
    };
    var fileName = "";
    var outputFileName;
	for(var i=2; i<process.argv.length;i++){
		if(process.argv[i] === "-ns"){
            options.ignoreNameSpace = false;
        }else if(process.argv[i] === "-a"){
            options.ignoreNonTextNodeAttr = true;
            options.ignoreTextNodeAttr = true;
        }else if(process.argv[i] === "-o"){
            outputFileName = process.argv[++i];
            if (outputFileName === '-') {
                outputFileName = void 0;
            }
        }else{//filename
            fileName = process.argv[i];
        }
    }
    var callback = function (xmlData) {
        output = JSON.stringify(parser.parse(xmlData,options),null,4);
        if (outputFileName) {
            writeToFile(outputFileName, output);
        } else {
            console.log(output);
        }
    };

    try{
        if (!fileName) {
            readToEnd(process.stdin, function (err, data) {
                if (err) {
                    throw err;
                }
                callback(data);
            });
        }else {
            fs.readFile(fileName, function (err, data) {
                if (err) {
                    throw err;
                }
                callback(xmlData);
            });
        }
    }catch(e){
        console.log("Seems an invalid file." + e);
    }
}

function writeToFile(fileName, data){
	fs.writeFile(fileName, data, function (err) {
		if (err) throw err;
	 	console.log('JSON output has been written to ' + fileName);
	});
}

@@ -249,17 +249,76 @@
 L Parser
-%22
+ %22 + require(path.join(__dirname + %22/package.json%22)).version
 );%0A    c
@@ -417,16 +417,91 @@
 son%5D%22);%0A
+    console.log(%22cat xmlfile.xml %7C xml2js %5B-ns%7C-a%5D %5B-o outputfile.json%5D%22);%0A
 %7Delse if
@@ -1153,105 +1153,8 @@
 i%5D;%0A
-            if (outputFileName === '-') %7B%0A                outputFileName = void 0;%0A            %7D%0A
     
@@ -1514,32 +1514,88 @@
 f (!fileName) %7B%0A
+            console.log(%22when file name is not passed%22)%0A
             read
@@ -1936,20 +1936,17 @@
 allback(
-xmlD
+d
 ata);%0A  
@@ -1984,16 +1984,16 @@
 tch(e)%7B%0A
-
         
@@ -2026,16 +2026,26 @@
 lid file
+ or stream
 .%22 + e);

Fix variable name when filename is given on CLI Update CLI help text