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'],
28 $res->command->options['scope']
33 $cmd = new Command_Server($this->cfg);
35 $res->command->args['server'],
36 $res->command->options['verbose']
41 $class = 'shpub\\Command_' . ucfirst($res->command_name);
42 $this->requireValidHost();
43 $cmd = new $class($this->cfg);
44 $cmd->run($res->command);
47 } catch (\Exception $e) {
48 Log::err('Error: ' . $e->getMessage());
54 * Let the CLI option parser parse the options.
56 * @param object $optParser Option parser
58 * @return object Parsed command line parameters
60 protected function parseParameters(\Console_CommandLine $optParser)
63 $res = $optParser->parse();
64 $opts = $res->options;
66 $this->cfg->host = new Config_Host();
67 if ($opts['server'] !== null) {
68 $key = $this->cfg->getHostByName($opts['server']);
70 $this->cfg->host->server = $opts['server'];
72 $this->cfg->host = $this->cfg->hosts[$key];
75 $key = $this->cfg->getDefaultHost();
77 $this->cfg->host = $this->cfg->hosts[$key];
80 $this->cfg->setDebug($opts['debug']);
83 } catch (\Exception $exc) {
84 $optParser->displayError($exc->getMessage());
89 * Load parameters for the CLI option parser.
91 * @return \Console_CommandLine CLI option parser
93 protected function loadOptParser()
95 $optParser = new \Console_CommandLine();
96 $optParser->name = 'shpub';
97 $optParser->description = 'Command line micropub client';
98 $optParser->version = '0.3.0';
99 $optParser->subcommand_required = true;
101 $optParser->addOption(
104 'short_name' => '-s',
105 'long_name' => '--server',
106 'description' => 'Server URL',
107 'help_name' => 'URL',
108 'action' => 'StoreString',
112 $optParser->addOption(
115 'short_name' => '-d',
116 'long_name' => '--debug',
117 'description' => 'Verbose output',
118 'action' => 'StoreTrue',
123 Command_Connect::opts($optParser);
124 Command_Server::opts($optParser);
125 Command_Targets::opts($optParser);
127 Command_Article::opts($optParser);
128 Command_Note::opts($optParser);
129 Command_Reply::opts($optParser);
130 Command_Like::opts($optParser);
131 Command_Repost::opts($optParser);
132 Command_Rsvp::opts($optParser);
133 Command_Bookmark::opts($optParser);
135 Command_Delete::opts($optParser);
136 Command_Undelete::opts($optParser);
137 Command_Update::opts($optParser);
139 Command_Upload::opts($optParser);
144 protected function requireValidHost()
146 if ($this->cfg->host->server === null
147 || $this->cfg->host->user === null
148 || $this->cfg->host->token === null
150 throw new \Exception(
151 'Server data incomplete. "shpub connect" first.'
155 $this->cfg->host->loadEndpoints();