Console_Commandlineが便利だった件

Console_CommandLineが思いの外利用しやすかったので、TypeTalkにメッセージをPushするスクリプトでお試ししてみた。

## composerで設定
```json:composer.json
{
	"require": {
		 "guzzle/guzzle": "*"
		,"codeception/codeception": "*"
		,"pear/pear_exception": "1.0.*@dev"
		,"pear/console_commandline": "dev-trunk"
	}
}
```

## 設定値
```php:config.php
//TypeTalkのID等
```

## TypeTalk
```php:typetalk.php
require_once './vendor/autoload.php';

use Guzzle\Http\Client;
use Guzzle\Http\Message\Request;
use Guzzle\Http\Exception\RequestException;

//アクセスTokenを取得する
//まとめのTalkID一覧を取得する
//メッセージをPushする
//...
```

## pushする
addOptionで必須項目の設定について、分からなかった。

```php:push.php
require_once './vendor/autoload.php';
require_once './config.php';
require_once './typetalk.php';

use Console\CommandLine;

$cmdline = new Console_CommandLine();
$cmdline->description = Configuration::CLI_DESCRIPTION;
$cmdline->version = Configuration::VERSION;

$cmdline->addOption('topic', array(
	 'short_name' => '-t'
	,'long_name' => '--topic'
	,'description' => 'TypeTalk TopicID' . PHP_EOL . 'Default: ' . Configuration::DEFAULT_TOPIC_ID
	,'help_name' => '{TopicID}'
	,'action' => 'StoreInt'
	,'default' => Configuration::DEFAULT_TOPIC_ID
));

$cmdline->addOption('talk', array(
	 'short_name' => '-g'
	,'long_name' => '--group'
	,'description' => 'TypeTalk TalkID' . PHP_EOL . 'Default: ' . Configuration::DEFAULT_TALK_ID
	,'help_name' => '{TalkID}'
	,'action' => 'StoreInt'
	,'default' => Configuration::DEFAULT_TALK_ID
));

$cmdline->addOption('message', array(
	 'short_name' => '-m'
	,'long_name' => '--message'
	,'description' => 'TypeTalk Push Message' . PHP_EOL . 'Default: ' . Configuration::DEFAULT_MESSAGE
	,'help_name' => '{Message}'
	,'action' => 'StoreString'
	,'default' => Configuration::DEFAULT_MESSAGE
));

try {
	$cmdlineResult = $cmdline->parse();
	
	$errors = array();
	
	$typeTalkTopicId = isset($cmdlineResult->options['topic']) ? trim($cmdlineResult->options['topic']) : '';
	$typeTalkTalkId = isset($cmdlineResult->options['talk']) ? trim($cmdlineResult->options['talk']) : '';
	$message = isset($cmdlineResult->options['message']) ? trim($cmdlineResult->options['message']) : '';
	
	if (!strlen($typeTalkTopicId)) $errors[] = 'TopicID is not null.'; //TopicIDは必須
	if (!strlen($message)) $errors[] = 'Message is not null.'; //メッセージは必須
	if (count($errors) > 0) throw new Exception(implode(PHP_EOL, $errors));
	
	$typeTalk = new TypeTalk(Configuration::TYPETALK_CLIENT_ID, Configuration::TYPETALK_CLIENT_SECRET);
	$talkIdList = $typeTalk->getTalkIdListByTopicId($typeTalkTopicId);
	
	if (!in_array($typeTalkTalkId, $talkIdList)) {
		//トピックに存在しないまとめIDが指定された場合はまとめナシとする
		//throw new Exception('Missing TalkId[' . $typeTalkTalkId . '] in Topic[' . $typeTalkTopicId . '].');
		$typeTalkTalkId = null;
	}
	
	$typeTalk->sendMessage($typeTalkTopicId, $typeTalkTalkId, $message, null);
	
} catch (Exception $e) {
	var_dump($e->getMessage());
}
```

## 実際に動かしてみる

```
php ./push.php --help
```

```
BearTalk CommandLine

Usage:
  push.php [options]

Options:
  -t {TopicID}, --topic={TopicID}    TypeTalk TopicID
                                     Default: {TopicID}
  -g {TalkID}, --group={TalkID}      TypeTalk TalkID
                                     Default: {TalkID}
  -m {Message}, --message={Message}  TypeTalk Push Message
                                     Default: デュフフフフ
  -h, --help                         show this help message and exit
  -v, --version                      show the program version and exit
```