diff --git a/otodo-add b/otodo-add new file mode 100755 index 0000000..c788186 --- /dev/null +++ b/otodo-add @@ -0,0 +1,85 @@ +#!/usr/bin/php +. +*/ + +require_once dirname(__FILE__) . '/init.php'; + +$optsSetup = array( + 'c:' => 'config:', + 'h' => 'help', + 't:' => 'text:', + 'd:' => 'due:', +); +$optsReq = array('config', 'text'); +$opts = getopt(implode('', array_keys($optsSetup)), array_values($optsSetup)); + +foreach ($opts as $n => $v) { + foreach ($optsSetup as $short => $long) { + $short = rtrim($short, ':'); + $long = rtrim($long, ':'); + if ($short == $n) { + $opts[$long] = $v; + } + } +} + +foreach ($optsReq as $optReq) { + if (!array_key_exists($optReq, $opts)) { + $opts['help'] = false; + break; + } +} + +if (array_key_exists('help', $opts)) { + echo $argv[0] . ' ' . PHP_EOL; + echo PHP_EOL; + echo 'Options:' . PHP_EOL; + echo ' -h Help' . PHP_EOL; + echo ' -c , --config Config file' . PHP_EOL; + echo ' -t , --text Text of todo to add' . PHP_EOL; + echo ' -d , --due Due date' . PHP_EOL; + exit(-1); +} + +$configFile = $opts['config']; +try { + Config::loadFile($configFile); +} catch (ConfigLoadException $cle) { + echo $cle->getMessage() . PHP_EOL; + exit(-1); +} + +$todos = new TodosEx(); +$todos->loadFromFile(Config::$config['core']['todo_file']); + +$t = new TodoEx($todos); +$t->creationDate = new DateTime('today'); +$t->text = $opts['text']; +if (array_key_exists('due', $opts)) { + $t->due = new DateTime($opts['due']); +} +$todos[] = $t; + +$todos->saveToFile(Config::$config['core']['todo_file']); +$dir = Config::$config['core']['backup_dir']; +if ($dir) { + $source = Config::$config['core']['todo_file']; + Todos::backup($source, $dir); +}