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'],
27 $res->command->options['force']
31 $this->requireValidHost();
32 $cmd = new Command_Like($this->cfg->host);
33 $cmd->run($res->command->args['url']);
36 var_dump($this->cfg->host, $res);
39 } catch (\Exception $e) {
40 echo 'Error: ' . $e->getMessage() . "\n";
46 * Let the CLI option parser parse the options.
48 * @param object $parser Option parser
50 * @return object Parsed command line parameters
52 protected function parseParameters(\Console_CommandLine $optParser)
55 $res = $optParser->parse();
56 $opts = $res->options;
58 $this->cfg->host = new Config_Host();
59 if ($opts['server'] !== null) {
60 $key = $this->cfg->getHostByName($opts['server']);
62 $this->cfg->host->server = $opts['server'];
64 $this->cfg->host = $this->cfg->hosts[$key];
67 if ($opts['user'] !== null) {
68 $this->cfg->host->user = $opts['user'];
72 } catch (\Exception $exc) {
73 $optParser->displayError($exc->getMessage());
78 * Load parameters for the CLI option parser.
80 * @return \Console_CommandLine CLI option parser
82 protected function loadOptParser()
84 $optParser = new \Console_CommandLine();
85 $optParser->description = 'shpub';
86 $optParser->version = '0.0.0';
87 $optParser->subcommand_required = true;
89 $optParser->addOption(
93 'long_name' => '--server',
94 'description' => 'Server URL',
96 'action' => 'StoreString',
100 $optParser->addOption(
103 'short_name' => '-u',
104 'long_name' => '--user',
105 'description' => 'User URL',
106 'help_name' => 'URL',
107 'action' => 'StoreString',
112 $cmd = $optParser->addCommand('connect');
116 'short_name' => '-f',
117 'long_name' => '--force-update',
118 'description' => 'Force token update if token already available',
119 'action' => 'StoreTrue',
127 'description' => 'Server URL',
134 'description' => 'User URL',
141 'description' => 'Short name (key)',
145 //$cmd = $optParser->addCommand('post');
146 $cmd = $optParser->addCommand('reply');
151 'description' => 'URL that is replied to',
158 'description' => 'Reply text',
162 $cmd = $optParser->addCommand('like');
167 'description' => 'URL that is liked',
174 protected function requireValidHost()
176 if ($this->cfg->host->server === null
177 || $this->cfg->host->user === null
178 || $this->cfg->host->token === null
180 throw new \Exception(
181 'Server data incomplete. "shpub connect" first.'
185 $this->cfg->host->loadEndpoints();