#!/usr/bin/env node
'use strict';
var path = require('path');
var program = require('commander');
var subfil = require('./');
var pkg = require('./package');
var videoExtensions = require('video-extensions');

program
	.version(pkg.version)
	.usage('subfil <path>')
	.option('-r, --recursive', 'download subtitles for files recursively')
	.option('-l, --language <language>', 'download subtitles in given language')
	.option('-d, --destination <path>', 'download destination for subtitles')
	.parse(process.argv);

if (program.args.length === 0) {
	console.error('You need to specify at least one path');
	process.exit(1);
}

var i, files = [];

if (program.recursive) {
	var extensionFilter = '{' + videoExtensions.join(',') + '}';
} else {
	files = program.args;
}

var language = program.language;
var destination = program.destination;

var file = files[0];

console.log('Downloading subtitles for ' + file);
subfil.download(file, language, destination, function (err, dest) {
	if (err) {
		throw err;
	} else {
		console.log('Subtitles successfully downloaded in ' + dest);
	}
});

@@ -201,16 +201,750 @@
 sions');
+%0Avar allLanguages = require('./languages')%0A%0Avar NOOP = function () %7B%7D;%0A%0Avar handleFile = function (file, callback) %7B%0A%09callback = arguments%5Barguments.length - 1%5D;%0A%09if (typeof callback !== 'function') %7B%0A%09%09callback = NOOP;%0A%09%7D%0A%0A%09console.log('Downloading subtitles for ' + path.basename(file));%0A%09subfil.download(file, language, destination, function (err, dest) %7B%0A%09%09if (err) %7B%0A%09%09%09switch (err.message) %7B%0A%09%09%09%09case 'Invalid video file':%0A%09%09%09%09%09console.log('Please specify a valid video file');%0A%09%09%09%09%09break;%0A%0A%09%09%09%09case 'No subtitles for hash in specified language':%0A%09%09%09%09%09console.log('No subtitles available')%0A%09%09%09%09%09break;%0A%0A%09%09%09%09default:%0A%09%09%09%09%09callback(err);%0A%09%09%09%09%09return;%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09console.log('Subtitles downloaded successfully');%0A%09%09%7D%0A%09%7D);%0A%7D
 %0A%0Aprogra
@@ -1349,24 +1349,319 @@
 exit(1);%0A%7D%0A%0A
+if (typeof program.language === 'string') %7B%0A%09if (allLanguages.indexOf(program.language.toLowerCase()) === -1) %7B%0A%09%09console.log('Invalid language provided');%0A%09%09console.log('Please choose a language from the following:');%0A%09%09console.log(allLanguages.join(', ').join('%5Cn'));%0A%09%09process.exit(1);%0A%09%7D%0A%7D%0A%0A
 var i, files
@@ -1889,73 +1889,18 @@
 %5D;%0A%0A
-console.log('Downloading subtitles for ' + file);%0Asubfil.download
+handleFile
 (fil
@@ -1934,30 +1934,24 @@
 unction (err
-, dest
 ) %7B%0A%09if (err
@@ -1971,81 +1971,8 @@
 rr;%0A
-%09%7D else %7B%0A%09%09console.log('Subtitles successfully downloaded in ' + dest);%0A
 %09%7D%0A%7D

Add error handling in CLI. Print error message to STDOUT