#!/usr/bin/env node
'use strict';
var cmd = require('commander')
var fs = require('fs')
var svg2png = require('svg2png')
var gant = require('./src/js/gantt.js')
var yaml = require('./src/js/yaml_parser')

var program = cmd.version('0.1.0')
  .command('yaml2gantt')
  .usage('[options] <file>')
  .option('-c, --config <config>', 'Set config path. default to ./config.yaml')
  .option('-o, --output <output_file>', 'Output to file')
  .option('-f, --format <html|svg|png>', 'Output format. If png is set, output option is required.')
  .option('--compare <compare_file>', 'Set file which you want to compare')
  .arguments('yaml_path')
  .parse(process.argv)

var config = {
  "canvas": {
    "width": 1000,
    "height": 300
  },
  "holidays": [],
  "timeFormatLocale": {
    "dateTime" : "%a %b %e %X %Y",
    "date" : "%Y/%m/%d",
    "time" : "%H:%M:%S",
    "periods" : [ "AM", "PM" ],
    "days" : [
      "日曜日", "月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日"
    ],
    "shortDays" : [ "日", "月", "火", "水", "木", "金", "土" ],
    "months" : [
      "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月",
      "11月", "12月"
    ],
    "shortMonths" : [
      "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月",
      "11月", "12月"
    ]
  }
}

if (program.config) {
  var config_file = program.config
  config = yaml.config(config_file)
} else {
  var config_file = './config.yaml'
  try{
    var stat = fs.statSync(config_file);
    if (stat != undefined && stat.isFile()){
      config = yaml.config(config_file)
    }
  }catch(e){}
}

var filepath = program.args[0]
if (filepath == undefined) {
  program.help();
}

var compare_file = program.compare
if (compare_file == undefined){
  var data = yaml.parse(filepath)
}else {
  var data = yaml.parse(filepath)
  var compare_data = yaml.parse(compare_file, data["range"]["start"], data["range"]["end"])
  data["resources"]["compared_tasks"] = []
  for (let key in data["resources"]["tasks"]) {
    for( let cKey in compare_data["resources"]["tasks"]){
      if (data["resources"]["tasks"][key]["id"] == compare_data["resources"]["tasks"][cKey]["id"]){
        compare_data["resources"]["tasks"][cKey]["y_index"] = data["resources"]["tasks"][key]["y_index"];
        data["resources"]["compared_tasks"].push(compare_data["resources"]["tasks"][cKey]);
      }
    }
  }
}
gant.init(data.range, config);
gant.update(data.resources);

var output_file = program.output;
var format = program.format || 'html';
var out;
if ( format == 'svg' || format == 'png'){
  out = '<?xml version="1.0" encoding="utf-8"?>' + document.body.innerHTML;
} else if (format == 'html' ) {
  out = '<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>'+ document.body.innerHTML + '</body></html>'
} else {
  console.error('Format:' + format + ' is not supported.')
  process.exit(1);
}
if (output_file == undefined ){
  if(format == 'png'){
    console.error('output option is required when format is png.')
    process.exit(1);
  }else {
    console.log(out)
  }
} else {
  if (format == 'png' ){
    svg2png(out)
      .then(buffer => fs.writeFileSync(output_file,buffer))
      .catch(e => console.error(e));
  } else {
    fs.writeFileSync(output_file, out);
  }
}

@@ -77,24 +77,51 @@
 quire('fs')%0A
+var path = require('path')%0A
 var svg2png 
@@ -445,20 +445,86 @@
 tput to 
-file
+a specified file. %5BDefault: input filename + ext in current directory.
 ')%0A  .op
@@ -578,50 +578,8 @@
 mat.
- If png is set, output option is required.
 ')%0A 
@@ -653,16 +653,87 @@
 mpare')%0A
+  .option('--stdout', 'Output to stdout %5BDefault: output to a file.%5D')%0A
   .argum
@@ -2579,16 +2579,46 @@
 'html';%0A
+var stdout = program.stdout;%0A%0A
 var out;
@@ -2982,40 +2982,23 @@
 if (
-output_file == undefined
+ stdout
  )%7B%0A  if
 (for
@@ -2993,17 +2993,19 @@
  )%7B%0A  if
-(
+ ( 
 format =
@@ -3007,24 +3007,25 @@
 mat == 'png'
+ 
 )%7B%0A    conso
@@ -3034,20 +3034,20 @@
 .error('
-outp
+stdo
 ut optio
@@ -3051,22 +3051,22 @@
 tion is 
-requir
+not us
 ed when 
@@ -3081,16 +3081,17 @@
 s png.')
+;
 %0A    pro
@@ -3107,25 +3107,17 @@
 (1);%0A  %7D
-else %7B%0A  
+%0A
   consol
@@ -3130,21 +3130,173 @@
 out)
-%0A  %7D%0A%7D else %7B
+;%0A%7D else %7B%0A  if ( output_file == undefined )%7B%0A    let filename = path.basename(filepath);%0A    output_file = filename.replace(path.extname(filepath), '.'+format);%0A  %7D
 %0A  i

Change default option behavor