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 $class = 'shpub\\Command_' . ucfirst($res->command_name);
38 $this->requireValidHost();
39 $cmd = new $class($this->cfg);
40 $cmd->run($res->command);
43 } catch (\Exception $e) {
44 Log::err('Error: ' . $e->getMessage());
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 $key = $this->cfg->getDefaultHost();
73 $this->cfg->host = $this->cfg->hosts[$key];
76 $this->cfg->setDebug($opts['debug']);
79 } catch (\Exception $exc) {
80 $optParser->displayError($exc->getMessage());
85 * Load parameters for the CLI option parser.
87 * @return \Console_CommandLine CLI option parser
89 protected function loadOptParser()
91 $optParser = new \Console_CommandLine();
92 $optParser->description = 'shpub';
93 $optParser->version = '0.0.5';
94 $optParser->subcommand_required = true;
96 $optParser->addOption(
100 'long_name' => '--server',
101 'description' => 'Server URL',
102 'help_name' => 'URL',
103 'action' => 'StoreString',
107 $optParser->addOption(
110 'short_name' => '-d',
111 'long_name' => '--debug',
112 'description' => 'Verbose output',
113 'action' => 'StoreTrue',
118 $cmd = $optParser->addCommand('connect');
122 'short_name' => '-f',
123 'long_name' => '--force-update',
124 'description' => 'Force token update if token already available',
125 'action' => 'StoreTrue',
133 'description' => 'Server URL',
140 'description' => 'User URL',
147 'description' => 'Short name (key)',
151 $cmd = $optParser->addCommand('server');
155 'short_name' => '-v',
156 'long_name' => '--verbose',
157 'description' => 'Show more server infos',
158 'action' => 'StoreTrue',
163 Command_Article::opts($optParser);
164 Command_Note::opts($optParser);
165 Command_Reply::opts($optParser);
166 Command_Like::opts($optParser);
167 Command_Repost::opts($optParser);
169 Command_Delete::opts($optParser);
170 Command_Undelete::opts($optParser);
175 protected function requireValidHost()
177 if ($this->cfg->host->server === null
178 || $this->cfg->host->user === null
179 || $this->cfg->host->token === null
181 throw new \Exception(
182 'Server data incomplete. "shpub connect" first.'
186 $this->cfg->host->loadEndpoints();