Given an empty directory
When I run `wp help`
- Then STDOUT should contain:
- """
- Available commands:
- """
+ Then STDOUT should not be empty
When I run `wp help core`
- Then STDOUT should contain:
- """
- usage: wp core
- """
+ Then STDOUT should not be empty
When I run `wp help core download`
- Then STDOUT should contain:
- """
- WP-CORE-DOWNLOAD(1)
- """
+ Then STDOUT should not be empty
- When I run `wp help --help`
- Then STDOUT should contain:
- """
- WP-HELP(1)
- """
+ When I run `wp help help`
+ Then STDOUT should not be empty
When I try `wp help non-existent-command`
Then the return code should be 1
And STDERR should not be empty
- @ronn
- Scenario: Generating help for subcommands
- Given an empty directory
- When I run `wp help --gen option`
- Then STDOUT should be:
- """
- generated option.1
- """
-
- @ronn
- Scenario: Generating help for multisite-only subcommands
- Given an empty directory
- When I run `wp help --gen site create`
- Then STDOUT should be:
- """
- generated site-create.1
- """
-
- @ronn
Scenario: Help for third-party commands
Given a WP install
And a wp-content/plugins/test-cli/test-help.txt file:
When I run `wp help test-help`
Then STDOUT should contain:
"""
- usage: wp test-help
- """
-
- When I run `wp help --gen test-help`
- Then STDOUT should contain:
- """
- generated test-help.1
- """
-
- When I run `wp help test-help`
- Then STDOUT should contain:
- """
- WP-TEST-HELP(1)
+ wp test-help
"""
/**
* Get help on a certain topic.
*
- * @synopsis [<command>] [--gen]
+ * @synopsis [<command>]
*/
function __invoke( $args, $assoc_args ) {
- if ( isset( $assoc_args['gen'] ) )
- $this->generate( $args );
- else
- $this->show( $args );
+ $command = self::find_subcommand( $args );
+
+ if ( $command ) {
+ self::add_initial_markdown( $command );
+
+ $extra_markdown_path = self::find_extra_markdown( $command );
+ if ( $extra_markdown_path ) {
+ echo file_get_contents( $extra_markdown_path );
+ }
+
+ exit;
+ }
// WordPress is already loaded, so there's no chance we'll find the command
if ( function_exists( 'add_filter' ) ) {
return $command;
}
- private function show( $args ) {
- if ( self::maybe_show_manpage( $args ) ) {
- exit;
- }
-
- $command = self::find_subcommand( $args );
-
- if ( $command ) {
- $command->show_usage();
- exit;
- }
- }
-
- private function generate( $args ) {
- if ( '' === exec( 'which ronn' ) ) {
- WP_CLI::error( '`ronn` executable not found.' );
- }
-
- $command = self::find_subcommand( $args );
-
- if ( $command ) {
- foreach ( WP_CLI::get_man_dirs() as $dest_dir => $src_dir ) {
- self::_generate( $src_dir, $dest_dir, $command );
- }
- exit;
- }
- }
-
- private static function maybe_show_manpage( $args ) {
- $man_file = self::get_file_name( $args );
-
- foreach ( \WP_CLI::get_man_dirs() as $dest_dir => $_ ) {
- $man_path = "$dest_dir/" . $man_file;
-
- if ( is_readable( $man_path ) ) {
- \WP_CLI::launch( "man $man_path" );
- return true;
- }
- }
-
- return false;
- }
-
- private static function _generate( $src_dir, $dest_dir, $command ) {
+ private static function find_extra_markdown( $command ) {
$cmd_path = Dispatcher\get_path( $command );
array_shift( $cmd_path ); // discard 'wp'
+ $cmd_path = implode( '-', $cmd_path );
- $src_path = "$src_dir/" . self::get_src_file_name( $cmd_path );
- $dest_path = "$dest_dir/" . self::get_file_name( $cmd_path );
-
- self::call_ronn( self::get_markdown( $src_path, $command ), $dest_path );
-
- if ( $command->has_subcommands() ) {
- foreach ( $command->get_subcommands() as $subcommand ) {
- self::_generate( $src_dir, $dest_dir, $subcommand );
- }
+ foreach ( WP_CLI::get_man_dirs() as $src_dir ) {
+ $src_path = "$src_dir/$cmd_path.txt";
+ if ( is_readable( $src_path ) )
+ return $src_path;
}
- }
-
- // returns a file descriptor or false
- private static function get_markdown( $doc_path, $command ) {
- if ( !file_exists( $doc_path ) )
- return false;
-
- $fd = fopen( "php://temp", "rw" );
-
- self::add_initial_markdown( $fd, $command );
- fwrite( $fd, file_get_contents( $doc_path ) );
-
- if ( 0 === ftell( $fd ) )
- return false;
-
- fseek( $fd, 0 );
-
- return $fd;
+ return false;
}
- private static function add_initial_markdown( $fd, $command ) {
- $path = Dispatcher\get_path( $command );
+ private static function add_initial_markdown( $command ) {
+ $name = implode( ' ', Dispatcher\get_path( $command ) );
$binding = array(
- 'name_m' => implode( '-', $path ),
+ 'name' => $name,
'shortdesc' => $command->get_shortdesc(),
);
- $synopsis = Dispatcher\get_full_synopsis( $command, true );
-
- $synopsis = str_replace( '_', '\_', $synopsis );
- $synopsis = str_replace( array( '<', '>' ), '_', $synopsis );
-
- $binding['synopsis'] = $synopsis;
-
- if ( !$binding['shortdesc'] ) {
- $name_s = implode( ' ', $path );
- \WP_CLI::warning( "No shortdesc for $name_s" );
- }
+ $binding['synopsis'] = "$name " . $command->get_synopsis();
if ( $command->has_subcommands() ) {
foreach ( $command->get_subcommands() as $subcommand ) {
}
}
- fwrite( $fd, Utils\mustache_render( 'man.mustache', $binding ) );
- }
-
- private static function call_ronn( $markdown, $dest ) {
- if ( !$markdown )
- return;
-
- $descriptorspec = array(
- 0 => $markdown,
- 1 => array( 'file', $dest, 'w' ),
- 2 => STDERR
- );
-
- $cmd = "ronn --date=2012-01-01 --roff --manual='WP-CLI'";
-
- $r = proc_close( proc_open( $cmd, $descriptorspec, $pipes ) );
-
- $roff = file_get_contents( $dest );
- $roff = str_replace( ' "January 2012"', '', $roff );
- file_put_contents( $dest, $roff );
-
- \WP_CLI::log( "generated " . basename( $dest ) );
- }
-
- private static function get_file_name( $args ) {
- return implode( '-', $args ) . '.1';
- }
-
- private static function get_src_file_name( $args ) {
- return implode( '-', $args ) . '.txt';
+ echo Utils\mustache_render( 'man.mustache', $binding );
}
}