1: <?php
2: 3: 4: 5: 6:
7:
8: namespace Peg\Custom\Command\Action;
9:
10: use Peg\Custom\Application;
11:
12: 13: 14:
15: class Help extends \Peg\Custom\CommandLine\Action
16: {
17:
18: public function OnCall(\Peg\Custom\CommandLine\Command $command)
19: {
20: if(strlen($command->value) > 0)
21: {
22: if($help_command = Application::GetCLIParser()->GetCommand($command->value))
23: {
24: $this->PrintHelp($help_command);
25: }
26: else
27: {
28: \Peg\Custom\CommandLine\Error::Show(t("Invalid command supplied."));
29: }
30: }
31: else
32: {
33: Application::GetCLIParser()->PrintHelp();
34: }
35: }
36:
37: 38: 39: 40:
41: public function PrintHelp(\Peg\Custom\CommandLine\Command $command)
42: {
43:
44: $max_command_len = 0;
45:
46:
47: $max_option_len = 0;
48:
49: $parser = Application::GetCLIParser();
50:
51: print $parser->application_name . " v" . $parser->application_version . "\n";
52: print t($parser->application_description) . "\n\n";
53:
54: print t("Usage:") . "\n";
55:
56: if(strlen($command->name) > $max_command_len)
57: $max_command_len = strlen($command->name);
58:
59: if(count($command->options) > 0)
60: {
61: foreach($command->options as $option)
62: {
63: if(strlen($option->long_name) > $max_option_len)
64: $max_option_len = strlen($option->long_name);
65: }
66:
67: print " peg {$command->name} " . t("[options]") . "\n\n";
68: }
69: else
70: {
71: print " peg {$command->name}\n\n";
72: }
73:
74: print t("Description:") . "\n";
75:
76: $line = " " . str_pad($command->name, $max_command_len + 2) . t($command->description);
77: $line = wordwrap($line, 80);
78: $line_array = explode("\n", $line);
79:
80: print $line_array[0] . "\n";
81: unset($line_array[0]);
82:
83: if(count($line_array) > 0)
84: {
85: foreach($line_array as $line)
86: {
87: print str_pad($line, strlen($line) + ($max_command_len + 4), " ", STR_PAD_LEFT) . "\n";
88: }
89: }
90:
91: if(count($command->options) > 0)
92: {
93: print "\n";
94: print t("Options:") . "\n";
95: foreach($command->options as $option)
96: {
97: $line = " " .
98: str_pad(
99: "-" . $option->short_name . " --" . $option->long_name, $max_option_len + 8
100: ) .
101: t($option->description)
102: ;
103:
104: $line = wordwrap($line, 80);
105: $line_array = explode("\n", $line);
106:
107: print $line_array[0] . "\n";
108: unset($line_array[0]);
109:
110: if(count($line_array) > 0)
111: {
112: foreach($line_array as $line)
113: {
114: print str_pad($line, strlen($line) + ($max_option_len + 10), " ", STR_PAD_LEFT) . "\n";
115: }
116: }
117: }
118: }
119:
120: print "\n";
121: }
122:
123: }