<commit_before><?php
if (!defined('STDIN')) {
    echo "This script is supposed to be run from command line. Exiting.\n";
    exit;
}

require_once dirname(__FILE__) . "/../src/CloudStackClient.php";

try {
    throw new Exception("haha", 2);
} catch (Exception $e) {
    if ($e->getCode() == 0) {
        printf("Fatal Error: %s\n", $e->getMessage());
    } else {
        printf("Fatal Error (code %s): %s\n", $e->getCode(), $e->getMessage());
    }
    
}<commit_msg>added initial checks and error handling in command line tool<commit_after><?php

/**************************
  File paths
 **************************/
define("CLOUDSTACKCLIENT_FILE", dirname(__FILE__) . "/../src/CloudStackClient.php");
define("CONFIG_FILE", dirname(__FILE__) . "/../config.php");

/**************************
  Initial checks
 **************************/
check(defined('STDIN'), "This script is supposed to be run from command line. Exiting.");
check(file_exists(CLOUDSTACKCLIENT_FILE), "Unable to find CloudStackClient file, looked for " . CLOUDSTACKCLIENT_FILE);
check(file_exists(CONFIG_FILE), "Unable to find config file, looked for " . CONFIG_FILE);
check($argc > 1, "Usage: php cloudstack.php command args...");

require CLOUDSTACKCLIENT_FILE;
$config = require CONFIG_FILE;
$command = $argv[1];
$args = array_slice($argv, 1);

/**************************
  Request
 **************************/
$cloudstack = new BaseCloudStackClient($config['endpoint'], $config['api_key'], $config['secret_key']);
try {
    $result = $cloudstack->request($command, $args);
} catch (Exception $e) {
    err($e->getMessage());
}

//check(property_exists($result, strtolower($command)), sprintf("Incorrect command. Received %s", var_dump_str($result)));

/**************************
  Helpers
 **************************/
function check($cond, $errorMessage) {
    if (!$cond) {
        err($errorMessage);
    }
}
function err($errorMessage) {
    die("Fatal Error: $errorMessage\n");
}

function var_dump_str($obj) {
    ob_start();
    var_dump($obj);
    return ob_get_clean();
}
{'fraction_non_alphanumeric': 0.2565597667638484, 'fraction_numerical': 0.0024295432458697765, 'mean_word_length': 4.916666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7118760', 'n_tokens_mistral': 632, 'n_tokens_neox': 630, 'n_words': 148}