Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 58 lines (51 sloc) 1.34 KB
#!/usr/bin/env node
try {
var shorturl = require('shorturl');
} catch(e) {
var shorturl = require('./index');
}
var opts = require('optimist')
.usage("Usage: shorturl [options] <longurl>")
.boolean('debug')
.describe('service', "URL shortening service (such as: bit.ly, goo.gl, is.gd)")
.describe('user', "User name for services which require it")
.describe('key', "API key for services which require it")
.describe('debug', "Display troubleshooting information")
.default('service', 'is.gd')
.default('debug', false)
.alias('d', 'debug')
.check(function(argv) {
return ( argv._.length == 1 && /^https?:/.test(argv._[0]) );
})
.argv,
longurl = opts._[0],
params = {};
// Service-specific parameter building
switch ( opts.service ) {
case 'bit.ly':
params.login = opts.user;
params.apiKey = opts.key;
break;
case 'goo.gl':
params.key = opts.key;
break;
}
// DEBUG
if ( opts.debug ) {
console.log('\nargv[]: ' + require('util').inspect(opts));
console.log('\nparams: '+require('util').inspect(params));
}
shorturl(longurl, opts.service, params, function(result) {
// DEBUG
if ( opts.debug ) {
console.log('\nresult: ' + require('util').inspect(arguments));
return;
}
if ( result instanceof Error ) {
console.log('Error: ' + result.message);
process.exit(1);
} else {
console.log(result);
}
});
/* vim: set ft=javascript: */