-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_archiver.php
68 lines (57 loc) · 1.47 KB
/
audio_archiver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
require_once 'main.php';
try {
// Get arguments
$args = parseArgs($argv);
// Initialize global objects
$options = [
'backup' => $args['backup'],
'convert' => $args['convert'],
'debug' => $args['debug'],
'load_custom' => $args['load_custom'],
];
// Start program
main($args['dir'], $options);
} catch (Exception $e) {
handleException($e);
}
function parseArgs($argv)
{
$args = ['dir' => ''];
$availOptions = [
'b' => 'backup',
'c' => 'convert',
'd' => 'debug',
's' => 'load_custom',
];
// Read options
$inputOptions = getopt(implode('', array_keys($availOptions)));
foreach ($availOptions as $letter => $name) {
$args[$name] = array_key_exists($letter, $inputOptions);
}
$off = count($inputOptions);
// Read target directory
if (empty($argv[$off + 1])) {
throw new Exception('Usage: php [-b] [-c] [-s] audio_archiver.php target', E_ERROR);
}
$dir = $argv[$off + 1];
$args['dir'] = realpath($dir);
if (!is_dir($args['dir'])) {
throw new Exception("'$dir' is not a valid directory", E_ERROR);
}
return $args;
}
function handleException($e)
{
switch ($e->getCode()) {
case E_WARNING:
$type = '[Warning]';
case E_ERROR:
default:
$type = '[Error]';
}
echo($type . ' ' . $e->getMessage() . "\n");
if ($e->getCode() == E_ERROR) {
die;
}
}