OSDN Git Service

- changed ethna command and add ethna help command.
[ethna/ethna.git] / bin / ethna_handle.php
1 <?php
2 /**
3  *  ethna_handle.php
4  *
5  *  Ethna Handle Gateway
6  *
7  *  @author     Masaki Fujimoto <fujimoto@php.net>
8  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
9  *  @package    Ethna
10  *  @version    $Id$
11  */
12 while (ob_get_level()) {
13     ob_end_clean();
14 }
15
16 $base = dirname(dirname(dirname(__FILE__)));
17 ini_set('include_path', $base.PATH_SEPARATOR.ini_get('include_path'));
18
19 require_once 'Ethna/Ethna.php';
20 require_once ETHNA_BASE . '/class/Ethna_Getopt.php';
21
22 // fetch arguments
23 $opt = new Ethna_Getopt();
24 $arg_list = $opt->readPHPArgv();
25 if (Ethna::isError($arg_list)) {
26     echo $arg_list->getMessage()."\n";
27     exit(2);
28 }
29 array_shift($arg_list);  // remove "ethna_handle.php"
30
31 $eh =& new Ethna_Handle();
32
33 //  はじめの引数に - が含まれていればそれを分離する
34 //  含まれていた場合、それは -v|--version でなければならない
35 list($my_arg_list, $arg_list) = _Ethna_HandleGateway_SeparateArgList($arg_list);
36 $r = $opt->getopt($my_arg_list, "v", array("version"));
37 if (Ethna::isError($r)) {
38     usage($eh);
39     exit(1);
40 }
41
42 // ad-hoc:(
43 foreach ($r[0] as $opt) {
44     if ($opt[0] == "v" || $opt[0] == "--version") {
45         _Ethna_HandleGateway_ShowVersion();
46         exit(2);
47     }
48 }
49
50 if (count($arg_list) == 0) {
51     _Ethna_HandleGateway_ShowUsage();
52     exit(1);
53 }
54
55 $id = array_shift($arg_list);
56
57 $handler =& $eh->getHandler($id);
58 $handler->eh =& $eh;
59 if (Ethna::isError($handler)) {
60     printf("no such command: %s\n\n", $id);
61     _Ethna_HandleGateway_ShowUsage();
62     exit(1);
63 }
64
65 // don't know what will happen:)
66 $handler->setArgList($arg_list);
67 $r = $handler->perform();
68 if (Ethna::isError($r)) {
69     printf("error occured w/ command [%s]\n  -> %s\n\n", $id, $r->getMessage());
70     if ($r->getCode() == 'usage') {
71         $handler->usage();
72     }
73     exit(1);
74 }
75
76 /**
77  *  show usage
78  */
79 function _Ethna_HandleGateway_ShowUsage()
80 {
81     $message = <<<EOD
82 Type 'ethna help' for usage.
83
84 EOD;
85     echo $message;
86 }
87
88 /**
89  *  fetch options for myself
90  */
91 function _Ethna_HandleGateway_SeparateArgList($arg_list)
92 {
93     $my_arg_list = array();
94
95     //  はじめの引数に - が含まれていたら、
96     //  それを $my_arg_list に入れる
97     //  これは --version 判定のため 
98     for ($i = 0; $i < count($arg_list); $i++) {
99         if ($arg_list[$i]{0} == '-') {
100             // assume this should be an option for myself
101             $my_arg_list[] = $arg_list[$i];
102         } else {
103             break;
104         }
105     }
106     $arg_list = array_slice($arg_list, $i);
107
108     return array($my_arg_list, $arg_list);
109 }
110
111 /**
112  *  show version
113  */
114 function _Ethna_HandleGateway_ShowVersion()
115 {
116     $version = <<<EOD
117 Ethna %s (using PHP %s)
118
119 Copyright (c) 2004-%s,
120   Masaki Fujimoto <fujimoto@php.net>
121   halt feits <halt.feits@gmail.com>
122   Takuya Ookubo <sfio@sakura.ai.to>
123   nozzzzz <nozzzzz@gmail.com>
124   cocoitiban <cocoiti@comio.info>
125   Yoshinari Takaoka <takaoka@beatcraft.com>
126
127 http://ethna.jp/
128
129 EOD;
130     printf($version, ETHNA_VERSION, PHP_VERSION, date('Y'));
131 }
132 ?>