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']);
81 $this->cfg->setDryRun($opts['dryrun']);
84 } catch (\Exception $exc) {
85 $optParser->displayError($exc->getMessage());
90 * Load parameters for the CLI option parser.
92 * @return \Console_CommandLine CLI option parser
94 protected function loadOptParser()
96 $optParser = new \Console_CommandLine();
97 $optParser->name = 'shpub';
98 $optParser->description = 'Command line micropub client';
99 $optParser->version = '0.6.0';
100 $optParser->subcommand_required = true;
102 $optParser->addOption(
105 'short_name' => '-s',
106 'long_name' => '--server',
107 'description' => 'Server URL',
108 'help_name' => 'URL',
109 'action' => 'StoreString',
113 $optParser->addOption(
116 'short_name' => '-d',
117 'long_name' => '--debug',
118 'description' => 'Verbose output',
119 'action' => 'StoreTrue',
123 $optParser->addOption(
126 'short_name' => '-n',
127 'long_name' => '--dry-run',
128 'description' => 'Do not send modifying HTTP request(s) to the server',
129 'action' => 'StoreTrue',
134 Command_Connect::opts($optParser);
135 Command_Server::opts($optParser);
136 Command_Targets::opts($optParser);
138 Command_Article::opts($optParser);
139 Command_Note::opts($optParser);
140 Command_Reply::opts($optParser);
141 Command_Like::opts($optParser);
142 Command_Repost::opts($optParser);
143 Command_Rsvp::opts($optParser);
144 Command_Bookmark::opts($optParser);
145 Command_X::opts($optParser);
147 Command_Delete::opts($optParser);
148 Command_Undelete::opts($optParser);
149 Command_Update::opts($optParser);
151 Command_Upload::opts($optParser);
156 protected function requireValidHost()
158 if ($this->cfg->host->server === null
159 || $this->cfg->host->user === null
160 || $this->cfg->host->token === null
162 throw new \Exception(
163 'Server data incomplete. "shpub connect" first.'
167 $this->cfg->host->loadEndpoints();