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 $cmd = new Command_Server($this->cfg);
37 $cmd->run($res->command->options['verbose']);
40 var_dump($this->cfg->host, $res);
43 } catch (\Exception $e) {
44 echo 'Error: ' . $e->getMessage() . "\n";
50 * Let the CLI option parser parse the options.
52 * @param object $parser Option parser
54 * @return object Parsed command line parameters
56 protected function parseParameters(\Console_CommandLine $optParser)
59 $res = $optParser->parse();
60 $opts = $res->options;
62 $this->cfg->host = new Config_Host();
63 if ($opts['server'] !== null) {
64 $key = $this->cfg->getHostByName($opts['server']);
66 $this->cfg->host->server = $opts['server'];
68 $this->cfg->host = $this->cfg->hosts[$key];
71 if ($opts['user'] !== null) {
72 $this->cfg->host->user = $opts['user'];
76 } catch (\Exception $exc) {
77 $optParser->displayError($exc->getMessage());
82 * Load parameters for the CLI option parser.
84 * @return \Console_CommandLine CLI option parser
86 protected function loadOptParser()
88 $optParser = new \Console_CommandLine();
89 $optParser->description = 'shpub';
90 $optParser->version = '0.0.0';
91 $optParser->subcommand_required = true;
93 $optParser->addOption(
97 'long_name' => '--server',
98 'description' => 'Server URL',
100 'action' => 'StoreString',
104 $optParser->addOption(
107 'short_name' => '-u',
108 'long_name' => '--user',
109 'description' => 'User URL',
110 'help_name' => 'URL',
111 'action' => 'StoreString',
116 $cmd = $optParser->addCommand('connect');
120 'short_name' => '-f',
121 'long_name' => '--force-update',
122 'description' => 'Force token update if token already available',
123 'action' => 'StoreTrue',
131 'description' => 'Server URL',
138 'description' => 'User URL',
145 'description' => 'Short name (key)',
149 $cmd = $optParser->addCommand('server');
153 'short_name' => '-v',
154 'long_name' => '--verbose',
155 'description' => 'Show more server infos',
156 'action' => 'StoreTrue',
161 //$cmd = $optParser->addCommand('post');
162 $cmd = $optParser->addCommand('reply');
167 'description' => 'URL that is replied to',
174 'description' => 'Reply text',
178 $cmd = $optParser->addCommand('like');
183 'description' => 'URL that is liked',
190 protected function requireValidHost()
192 if ($this->cfg->host->server === null
193 || $this->cfg->host->user === null
194 || $this->cfg->host->token === null
196 throw new \Exception(
197 'Server data incomplete. "shpub connect" first.'
201 $this->cfg->host->loadEndpoints();