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 $key = $this->cfg->getDefaultHost();
85 $this->cfg->host = $this->cfg->hosts[$key];
88 if ($opts['user'] !== null) {
89 $this->cfg->host->user = $opts['user'];
91 $this->cfg->setDebug($opts['debug']);
94 } catch (\Exception $exc) {
95 $optParser->displayError($exc->getMessage());
100 * Load parameters for the CLI option parser.
102 * @return \Console_CommandLine CLI option parser
104 protected function loadOptParser()
106 $optParser = new \Console_CommandLine();
107 $optParser->description = 'shpub';
108 $optParser->version = '0.0.0';
109 $optParser->subcommand_required = true;
111 $optParser->addOption(
114 'short_name' => '-s',
115 'long_name' => '--server',
116 'description' => 'Server URL',
117 'help_name' => 'URL',
118 'action' => 'StoreString',
122 $optParser->addOption(
125 'short_name' => '-u',
126 'long_name' => '--user',
127 'description' => 'User URL',
128 'help_name' => 'URL',
129 'action' => 'StoreString',
133 $optParser->addOption(
136 'short_name' => '-d',
137 'long_name' => '--debug',
138 'description' => 'Verbose output',
139 'action' => 'StoreTrue',
144 $cmd = $optParser->addCommand('connect');
148 'short_name' => '-f',
149 'long_name' => '--force-update',
150 'description' => 'Force token update if token already available',
151 'action' => 'StoreTrue',
159 'description' => 'Server URL',
166 'description' => 'User URL',
173 'description' => 'Short name (key)',
177 $cmd = $optParser->addCommand('server');
181 'short_name' => '-v',
182 'long_name' => '--verbose',
183 'description' => 'Show more server infos',
184 'action' => 'StoreTrue',
189 //$cmd = $optParser->addCommand('post');
190 $cmd = $optParser->addCommand('reply');
195 'description' => 'URL that is replied to',
203 'description' => 'Reply text',
207 $cmd = $optParser->addCommand('like');
212 'description' => 'URL that is liked',
219 protected function requireValidHost()
221 if ($this->cfg->host->server === null
222 || $this->cfg->host->user === null
223 || $this->cfg->host->token === null
225 throw new \Exception(
226 'Server data incomplete. "shpub connect" first.'
230 $this->cfg->host->loadEndpoints();