Commit 8b945169 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Telematica ass2: Added '--help' option.

parent 77cb903f
......@@ -4,11 +4,14 @@
// Call Google Translate API
function translate($text, $from, $to) {
$key = "AIzaSyD5sFp2R2IMtDVWdu-4X-TptJCSod0chVc";
$url = sprintf("https://www.googleapis.com/language/translate/v2?key=%s&source=%s&target=%s&q=%s",
$key, $from, $to, urlencode($text));
$url = sprintf("https://www.googleapis.com/language/translate/v2?"
."key=%s&source=%s&target=%s&q=%s", $key, $from, $to, urlencode($text));
if( ($response = file_get_contents($url)) === false )
die("Cannot connect to Google Translate server.");
if( ($response = @file_get_contents($url)) === false ) {
die("Got negative response from Google Translate server.\n"
."Check your internet connection and make sure that you "
."entered valid language codes.\n");
}
$response = json_decode($response, true);
......@@ -16,7 +19,17 @@ function translate($text, $from, $to) {
}
// Parse input
$argc != 3 && die(sprintf("Usage: %s FROM TO\n", $argv[0]));
if( $argc != 3 ) {
echo sprintf("Usage: %s FROM TO\n", $argv[0]);
if( $argv[1] == "--help" ) {
echo "FROM is the input language, TO is the language to translate to.\n"
."Both languages should be entered as a 2-character long code.\n";
}
exit;
}
list($from, $to) = array_slice($argv, 1);
$lang_format = "/^[a-z]{2}$/i";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment