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);
34 $cmd->run($res->command->options['verbose']);
38 $class = 'shpub\\Command_' . ucfirst($res->command_name);
39 $this->requireValidHost();
40 $cmd = new $class($this->cfg);
41 $cmd->run($res->command);
44 } catch (\Exception $e) {
45 Log::err('Error: ' . $e->getMessage());
51 * Let the CLI option parser parse the options.
53 * @param object $optParser Option parser
55 * @return object Parsed command line parameters
57 protected function parseParameters(\Console_CommandLine $optParser)
60 $res = $optParser->parse();
61 $opts = $res->options;
63 $this->cfg->host = new Config_Host();
64 if ($opts['server'] !== null) {
65 $key = $this->cfg->getHostByName($opts['server']);
67 $this->cfg->host->server = $opts['server'];
69 $this->cfg->host = $this->cfg->hosts[$key];
72 $key = $this->cfg->getDefaultHost();
74 $this->cfg->host = $this->cfg->hosts[$key];
77 $this->cfg->setDebug($opts['debug']);
80 } catch (\Exception $exc) {
81 $optParser->displayError($exc->getMessage());
86 * Load parameters for the CLI option parser.
88 * @return \Console_CommandLine CLI option parser
90 protected function loadOptParser()
92 $optParser = new \Console_CommandLine();
93 $optParser->name = 'shpub';
94 $optParser->description = 'Command line micropub client';
95 $optParser->version = '0.3.0';
96 $optParser->subcommand_required = true;
98 $optParser->addOption(
101 'short_name' => '-s',
102 'long_name' => '--server',
103 'description' => 'Server URL',
104 'help_name' => 'URL',
105 'action' => 'StoreString',
109 $optParser->addOption(
112 'short_name' => '-d',
113 'long_name' => '--debug',
114 'description' => 'Verbose output',
115 'action' => 'StoreTrue',
120 Command_Connect::opts($optParser);
121 Command_Server::opts($optParser);
122 Command_Targets::opts($optParser);
124 Command_Article::opts($optParser);
125 Command_Note::opts($optParser);
126 Command_Reply::opts($optParser);
127 Command_Like::opts($optParser);
128 Command_Repost::opts($optParser);
129 Command_Rsvp::opts($optParser);
130 Command_Bookmark::opts($optParser);
132 Command_Delete::opts($optParser);
133 Command_Undelete::opts($optParser);
134 Command_Update::opts($optParser);
136 Command_Upload::opts($optParser);
141 protected function requireValidHost()
143 if ($this->cfg->host->server === null
144 || $this->cfg->host->user === null
145 || $this->cfg->host->token === null
147 throw new \Exception(
148 'Server data incomplete. "shpub connect" first.'
152 $this->cfg->host->loadEndpoints();