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 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 $key = $this->cfg->getDefaultHost();
73 $this->cfg->host = $this->cfg->hosts[$key];
76 if ($opts['user'] !== null) {
77 $this->cfg->host->user = $opts['user'];
79 $this->cfg->setDebug($opts['debug']);
82 } catch (\Exception $exc) {
83 $optParser->displayError($exc->getMessage());
88 * Load parameters for the CLI option parser.
90 * @return \Console_CommandLine CLI option parser
92 protected function loadOptParser()
94 $optParser = new \Console_CommandLine();
95 $optParser->description = 'shpub';
96 $optParser->version = '0.0.2';
97 $optParser->subcommand_required = true;
99 $optParser->addOption(
102 'short_name' => '-s',
103 'long_name' => '--server',
104 'description' => 'Server URL',
105 'help_name' => 'URL',
106 'action' => 'StoreString',
110 $optParser->addOption(
113 'short_name' => '-u',
114 'long_name' => '--user',
115 'description' => 'User URL',
116 'help_name' => 'URL',
117 'action' => 'StoreString',
121 $optParser->addOption(
124 'short_name' => '-d',
125 'long_name' => '--debug',
126 'description' => 'Verbose output',
127 'action' => 'StoreTrue',
132 $cmd = $optParser->addCommand('connect');
136 'short_name' => '-f',
137 'long_name' => '--force-update',
138 'description' => 'Force token update if token already available',
139 'action' => 'StoreTrue',
147 'description' => 'Server URL',
154 'description' => 'User URL',
161 'description' => 'Short name (key)',
165 $cmd = $optParser->addCommand('server');
169 'short_name' => '-v',
170 'long_name' => '--verbose',
171 'description' => 'Show more server infos',
172 'action' => 'StoreTrue',
177 Command_Note::opts($optParser);
178 Command_Reply::opts($optParser);
179 Command_Like::opts($optParser);
184 protected function requireValidHost()
186 if ($this->cfg->host->server === null
187 || $this->cfg->host->user === null
188 || $this->cfg->host->token === null
190 throw new \Exception(
191 'Server data incomplete. "shpub connect" first.'
195 $this->cfg->host->loadEndpoints();