/** * Check wrapping */ public function test_wrap() { $text = "this is a long string something\n" . "123456789012345678901234567890"; $expt = "this is a long\n" . "string\n" . "something\n" . "123456789012345\n" . "678901234567890"; $tf = new TableFormatter(); $this->assertEquals($expt, $tf->wordwrap($text, 15, "\n", true)); }
/** * Builds a help screen from the available options. You may want to call it from -h or on error * * @return string */ public function help() { $tf = new TableFormatter($this->colors); $text = ''; $hascommands = count($this->setup) > 1; foreach ($this->setup as $command => $config) { $hasopts = (bool) $this->setup[$command]['opts']; $hasargs = (bool) $this->setup[$command]['args']; // usage or command syntax line if (!$command) { $text .= $this->colors->wrap('USAGE:', Colors::C_BROWN); $text .= "\n"; $text .= ' ' . $this->bin; $mv = 2; } else { $text .= "\n"; $text .= $this->colors->wrap(' ' . $command, Colors::C_PURPLE); $mv = 4; } if ($hasopts) { $text .= ' ' . $this->colors->wrap('<OPTIONS>', Colors::C_GREEN); } if (!$command && $hascommands) { $text .= ' ' . $this->colors->wrap('<COMMAND> ...', Colors::C_PURPLE); } foreach ($this->setup[$command]['args'] as $arg) { $out = $this->colors->wrap('<' . $arg['name'] . '>', Colors::C_CYAN); if (!$arg['required']) { $out = '[' . $out . ']'; } $text .= ' ' . $out; } $text .= "\n"; // usage or command intro if ($this->setup[$command]['help']) { $text .= "\n"; $text .= $tf->format(array($mv, '*'), array('', $this->setup[$command]['help'] . "\n")); } // option description if ($hasopts) { if (!$command) { $text .= "\n"; $text .= $this->colors->wrap('OPTIONS:', Colors::C_BROWN); } $text .= "\n"; foreach ($this->setup[$command]['opts'] as $long => $opt) { $name = ''; if ($opt['short']) { $name .= '-' . $opt['short']; if ($opt['needsarg']) { $name .= ' <' . $opt['needsarg'] . '>'; } $name .= ', '; } $name .= "--{$long}"; if ($opt['needsarg']) { $name .= ' <' . $opt['needsarg'] . '>'; } $text .= $tf->format(array($mv, '30%', '*'), array('', $name, $opt['help']), array('', 'green', '')); $text .= "\n"; } } // argument description if ($hasargs) { if (!$command) { $text .= "\n"; $text .= $this->colors->wrap('ARGUMENTS:', Colors::C_BROWN); } $text .= "\n"; foreach ($this->setup[$command]['args'] as $arg) { $name = '<' . $arg['name'] . '>'; $text .= $tf->format(array($mv, '30%', '*'), array('', $name, $arg['help']), array('', 'cyan', '')); } } // head line and intro for following command documentation if (!$command && $hascommands) { $text .= "\n"; $text .= $this->colors->wrap('COMMANDS:', Colors::C_BROWN); $text .= "\n"; $text .= $tf->format(array($mv, '*'), array('', 'This tool accepts a command as first parameter as outlined below:')); $text .= "\n"; } } return $text; }