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);
39 $cmd->run($res->command->args['url']);
43 $this->requireValidHost();
44 $cmd = new Command_Reply($this->cfg);
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'];
86 $this->cfg->setDebug($opts['debug']);
89 } catch (\Exception $exc) {
90 $optParser->displayError($exc->getMessage());
95 * Load parameters for the CLI option parser.
97 * @return \Console_CommandLine CLI option parser
99 protected function loadOptParser()
101 $optParser = new \Console_CommandLine();
102 $optParser->description = 'shpub';
103 $optParser->version = '0.0.0';
104 $optParser->subcommand_required = true;
106 $optParser->addOption(
109 'short_name' => '-s',
110 'long_name' => '--server',
111 'description' => 'Server URL',
112 'help_name' => 'URL',
113 'action' => 'StoreString',
117 $optParser->addOption(
120 'short_name' => '-u',
121 'long_name' => '--user',
122 'description' => 'User URL',
123 'help_name' => 'URL',
124 'action' => 'StoreString',
128 $optParser->addOption(
131 'short_name' => '-d',
132 'long_name' => '--debug',
133 'description' => 'Verbose output',
134 'action' => 'StoreTrue',
139 $cmd = $optParser->addCommand('connect');
143 'short_name' => '-f',
144 'long_name' => '--force-update',
145 'description' => 'Force token update if token already available',
146 'action' => 'StoreTrue',
154 'description' => 'Server URL',
161 'description' => 'User URL',
168 'description' => 'Short name (key)',
172 $cmd = $optParser->addCommand('server');
176 'short_name' => '-v',
177 'long_name' => '--verbose',
178 'description' => 'Show more server infos',
179 'action' => 'StoreTrue',
184 //$cmd = $optParser->addCommand('post');
185 $cmd = $optParser->addCommand('reply');
190 'description' => 'URL that is replied to',
198 'description' => 'Reply text',
202 $cmd = $optParser->addCommand('like');
207 'description' => 'URL that is liked',
214 protected function requireValidHost()
216 if ($this->cfg->host->server === null
217 || $this->cfg->host->user === null
218 || $this->cfg->host->token === null
220 throw new \Exception(
221 'Server data incomplete. "shpub connect" first.'
225 $this->cfg->host->loadEndpoints();