From 1505e0d97eaa969e79748b441fff45efffcb8dda Mon Sep 17 00:00:00 2001 From: scribu Date: Tue, 22 Jan 2013 22:47:19 +0200 Subject: [PATCH] move special args handling to separate class --- php/WP_CLI/InternalAssoc.php | 73 ++++++++++++++++++++++++++++++++++++++++++++ php/class-wp-cli.php | 72 ++++++------------------------------------- 2 files changed, 82 insertions(+), 63 deletions(-) create mode 100644 php/WP_CLI/InternalAssoc.php diff --git a/php/WP_CLI/InternalAssoc.php b/php/WP_CLI/InternalAssoc.php new file mode 100644 index 00000000..2652ed77 --- /dev/null +++ b/php/WP_CLI/InternalAssoc.php @@ -0,0 +1,73 @@ + $command->get_name(), + 'description' => $command->get_shortdesc(), + ); + + if ( $command instanceof Dispatcher\AtomicCommand ) { + $dump['synopsis'] = (string) $command->get_synopsis(); + } else { + foreach ( Dispatcher\get_subcommands( $command ) as $subcommand ) { + $dump['subcommands'][] = self::command_to_array( $subcommand ); + } + } + + return $dump; + } + + static function completions() { + foreach ( WP_CLI::$root->get_subcommands() as $name => $command ) { + $subcommands = Dispatcher\get_subcommands( $command ); + + WP_CLI::line( $name . ' ' . implode( ' ', array_keys( $subcommands ) ) ); + } + } + + static function man( $args ) { + $arg_copy = $args; + + $command = WP_CLI::$root; + + while ( !empty( $args ) && $command && $command instanceof Dispatcher\CommandContainer ) { + $command = $command->find_subcommand( $args ); + } + + if ( !$command ) + WP_CLI::error( sprintf( "'%s' command not found.", + implode( ' ', $arg_copy ) ) ); + + foreach ( WP_CLI::get_man_dirs() as $dest_dir => $src_dir ) { + WP_CLI\Man\generate( $src_dir, $dest_dir, $command ); + } + } +} + diff --git a/php/class-wp-cli.php b/php/class-wp-cli.php index 4b0f68a9..6167cd1d 100644 --- a/php/class-wp-cli.php +++ b/php/class-wp-cli.php @@ -254,6 +254,10 @@ class WP_CLI { } } + static function get_config_path() { + return self::$config_path; + } + static function get_config( $key = null ) { if ( null === $key ) return self::$config; @@ -289,19 +293,19 @@ class WP_CLI { // Handle --version parameter if ( isset( self::$assoc_args['version'] ) && empty( self::$arguments ) ) { - self::line( 'wp-cli ' . WP_CLI_VERSION ); + \WP_CLI\InternalAssoc::version(); exit; } // Handle --info parameter if ( isset( self::$assoc_args['info'] ) && empty( self::$arguments ) ) { - self::show_info(); + \WP_CLI\InternalAssoc::info(); exit; } // Handle --cmd-dump parameter if ( isset( self::$assoc_args['cmd-dump'] ) ) { - self::cmd_dump(); + \WP_CLI\InternalAssoc::cmd_dump(); exit; } @@ -380,39 +384,18 @@ class WP_CLI { require self::$config['require']; if ( isset( self::$assoc_args['man'] ) ) { - self::generate_man( self::$arguments ); + \WP_CLI\InternalAssoc::man( self::$arguments ); exit; } if ( isset( self::$assoc_args['completions'] ) ) { - self::render_automcomplete(); + \WP_CLI\InternalAssoc::completions(); exit; } self::_run_command(); } - private static function cmd_dump() { - echo json_encode( self::command_to_array( self::$root ) ); - } - - private static function command_to_array( $command ) { - $dump = array( - 'name' => $command->get_name(), - 'description' => $command->get_shortdesc(), - ); - - if ( $command instanceof Dispatcher\AtomicCommand ) { - $dump['synopsis'] = (string) $command->get_synopsis(); - } else { - foreach ( Dispatcher\get_subcommands( $command ) as $subcommand ) { - $dump['subcommands'][] = self::command_to_array( $subcommand ); - } - } - - return $dump; - } - private static function _run_command() { self::run_command( self::$arguments, self::$assoc_args ); } @@ -441,43 +424,6 @@ class WP_CLI { } } - private static function show_info() { - $php_bin = defined( 'PHP_BINARY' ) ? PHP_BINARY : getenv( 'WP_CLI_PHP_USED' ); - - WP_CLI::line( "PHP binary:\t" . $php_bin ); - WP_CLI::line( "PHP version:\t" . PHP_VERSION ); - WP_CLI::line( "php.ini used:\t" . get_cfg_var( 'cfg_file_path' ) ); - WP_CLI::line( "wp-cli root:\t" . WP_CLI_ROOT ); - WP_CLI::line( "wp-cli config:\t" . self::$config_path ); - WP_CLI::line( "wp-cli version:\t" . WP_CLI_VERSION ); - } - - private static function generate_man( $args ) { - $arg_copy = $args; - - $command = \WP_CLI::$root; - - while ( !empty( $args ) && $command && $command instanceof Dispatcher\CommandContainer ) { - $command = $command->find_subcommand( $args ); - } - - if ( !$command ) - WP_CLI::error( sprintf( "'%s' command not found.", - implode( ' ', $arg_copy ) ) ); - - foreach ( self::$man_dirs as $dest_dir => $src_dir ) { - \WP_CLI\Man\generate( $src_dir, $dest_dir, $command ); - } - } - - private static function render_automcomplete() { - foreach ( self::$root->get_subcommands() as $name => $command ) { - $subcommands = Dispatcher\get_subcommands( $command ); - - self::line( $name . ' ' . implode( ' ', array_keys( $subcommands ) ) ); - } - } - // back-compat static function addCommand( $name, $class ) { self::add_command( $name, $class ); -- 2.11.0