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']
32 $cmd = new Command_Server($this->cfg);
33 $cmd->run($res->command->options['verbose']);
37 $this->requireValidHost();
38 $cmd = new Command_Like($this->cfg->host);
39 $cmd->run($res->command->args['url']);
43 $this->requireValidHost();
44 $cmd = new Command_Reply($this->cfg->host);
46 $res->command->args['url'],
47 implode(' ', $res->command->args['text'])
52 var_dump($this->cfg->host, $res);
55 } catch (\Exception $e) {
56 echo 'Error: ' . $e->getMessage() . "\n";
62 * Let the CLI option parser parse the options.
64 * @param object $parser Option parser
66 * @return object Parsed command line parameters
68 protected function parseParameters(\Console_CommandLine $optParser)
71 $res = $optParser->parse();
72 $opts = $res->options;
74 $this->cfg->host = new Config_Host();
75 if ($opts['server'] !== null) {
76 $key = $this->cfg->getHostByName($opts['server']);
78 $this->cfg->host->server = $opts['server'];
80 $this->cfg->host = $this->cfg->hosts[$key];
83 if ($opts['user'] !== null) {
84 $this->cfg->host->user = $opts['user'];
88 } catch (\Exception $exc) {
89 $optParser->displayError($exc->getMessage());
94 * Load parameters for the CLI option parser.
96 * @return \Console_CommandLine CLI option parser
98 protected function loadOptParser()
100 $optParser = new \Console_CommandLine();
101 $optParser->description = 'shpub';
102 $optParser->version = '0.0.0';
103 $optParser->subcommand_required = true;
105 $optParser->addOption(
108 'short_name' => '-s',
109 'long_name' => '--server',
110 'description' => 'Server URL',
111 'help_name' => 'URL',
112 'action' => 'StoreString',
116 $optParser->addOption(
119 'short_name' => '-u',
120 'long_name' => '--user',
121 'description' => 'User URL',
122 'help_name' => 'URL',
123 'action' => 'StoreString',
128 $cmd = $optParser->addCommand('connect');
132 'short_name' => '-f',
133 'long_name' => '--force-update',
134 'description' => 'Force token update if token already available',
135 'action' => 'StoreTrue',
143 'description' => 'Server URL',
150 'description' => 'User URL',
157 'description' => 'Short name (key)',
161 $cmd = $optParser->addCommand('server');
165 'short_name' => '-v',
166 'long_name' => '--verbose',
167 'description' => 'Show more server infos',
168 'action' => 'StoreTrue',
173 //$cmd = $optParser->addCommand('post');
174 $cmd = $optParser->addCommand('reply');
179 'description' => 'URL that is replied to',
187 'description' => 'Reply text',
191 $cmd = $optParser->addCommand('like');
196 'description' => 'URL that is liked',
203 protected function requireValidHost()
205 if ($this->cfg->host->server === null
206 || $this->cfg->host->user === null
207 || $this->cfg->host->token === null
209 throw new \Exception(
210 'Server data incomplete. "shpub connect" first.'
214 $this->cfg->host->loadEndpoints();