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->name = 'shpub';
93 $optParser->description = 'Command line micropub client';
94 $optParser->version = '0.2.1';
95 $optParser->subcommand_required = true;
97 $optParser->addOption(
100 'short_name' => '-s',
101 'long_name' => '--server',
102 'description' => 'Server URL',
103 'help_name' => 'URL',
104 'action' => 'StoreString',
108 $optParser->addOption(
111 'short_name' => '-d',
112 'long_name' => '--debug',
113 'description' => 'Verbose output',
114 'action' => 'StoreTrue',
119 Command_Connect::opts($optParser);
120 Command_Server::opts($optParser);
122 Command_Article::opts($optParser);
123 Command_Note::opts($optParser);
124 Command_Reply::opts($optParser);
125 Command_Like::opts($optParser);
126 Command_Repost::opts($optParser);
127 Command_Rsvp::opts($optParser);
128 Command_Bookmark::opts($optParser);
130 Command_Delete::opts($optParser);
131 Command_Undelete::opts($optParser);
132 Command_Update::opts($optParser);
134 Command_Upload::opts($optParser);
139 protected function requireValidHost()
141 if ($this->cfg->host->server === null
142 || $this->cfg->host->user === null
143 || $this->cfg->host->token === null
145 throw new \Exception(
146 'Server data incomplete. "shpub connect" first.'
150 $this->cfg->host->loadEndpoints();