13 $this->cfg = new Config();
17 $optParser = $this->loadOptParser();
18 $res = $this->parseParameters($optParser);
20 switch ($res->command_name) {
22 $cmd = new Command_Connect($this->cfg);
24 $res->command->args['server'],
25 $res->command->args['user'],
26 $res->command->args['key']
30 $this->requireValidHost();
31 $cmd = new Command_Like($this->cfg->host);
32 $cmd->run($res->command->args['url']);
35 var_dump($this->cfg->host, $res);
38 } catch (\Exception $e) {
39 echo 'Error: ' . $e->getMessage() . "\n";
45 * Let the CLI option parser parse the options.
47 * @param object $parser Option parser
49 * @return object Parsed command line parameters
51 protected function parseParameters(\Console_CommandLine $optParser)
54 $res = $optParser->parse();
55 $opts = $res->options;
57 $this->cfg->host = new Config_Host();
58 if ($opts['server'] !== null) {
59 $key = $this->cfg->getHostByName($opts['server']);
61 $this->cfg->host->server = $opts['server'];
63 $this->cfg->host = $this->cfg->hosts[$key];
66 if ($opts['user'] !== null) {
67 $this->cfg->host->user = $opts['user'];
71 } catch (\Exception $exc) {
72 $optParser->displayError($exc->getMessage());
77 * Load parameters for the CLI option parser.
79 * @return \Console_CommandLine CLI option parser
81 protected function loadOptParser()
83 $optParser = new \Console_CommandLine();
84 $optParser->description = 'shpub';
85 $optParser->version = '0.0.0';
86 $optParser->subcommand_required = true;
88 $optParser->addOption(
92 'long_name' => '--server',
93 'description' => 'Server URL',
95 'action' => 'StoreString',
99 $optParser->addOption(
102 'short_name' => '-u',
103 'long_name' => '--user',
104 'description' => 'User URL',
105 'help_name' => 'URL',
106 'action' => 'StoreString',
111 $cmd = $optParser->addCommand('connect');
116 'description' => 'Server URL',
123 'description' => 'User URL',
130 'description' => 'Short name (key)',
134 //$cmd = $optParser->addCommand('post');
135 $cmd = $optParser->addCommand('reply');
140 'description' => 'URL that is replied to',
147 'description' => 'Reply text',
151 $cmd = $optParser->addCommand('like');
156 'description' => 'URL that is liked',
163 protected function requireValidHost()
165 if ($this->cfg->host->server === null
166 || $this->cfg->host->user === null
167 || $this->cfg->host->token === null
169 throw new \Exception(
170 'Server data incomplete. "shpub connect" first.'