PHP code example of wp-php-toolkit / cli
1. Go to this page and download the library: Download wp-php-toolkit/cli library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wp-php-toolkit / cli example snippets
WordPress\CLI\CLI;
$option_defs = array(
'verbose' => array( 'v', false, false, 'Enable verbose output' ),
);
list( $positionals, $options ) = CLI::parse_command_args_and_options(
array( '-v', 'input.txt' ),
$option_defs
);
echo "verbose: " . ( $options['verbose'] ? 'yes' : 'no' ) . "\n";
echo "input: " . $positionals[0] . "\n";
WordPress\CLI\CLI;
$option_defs = array(
'all' => array( 'a', false, false, 'Process everything' ),
'force' => array( 'f', false, false, 'Overwrite existing files' ),
'verbose' => array( 'v', false, false, 'Verbose output' ),
'output' => array( 'o', true, null, 'Output path' ),
'port' => array( 'p', true, '3000', 'Server port' ),
);
$argv = array( '-afv', '--port=8080', '-o', '/tmp/result.txt', 'input.json' );
list( $positionals, $options ) = CLI::parse_command_args_and_options( $argv, $option_defs );
echo "input: " . $positionals[0] . "\n";
echo "flags: " . implode( ', ', array_keys( array_filter( array(
'all' => $options['all'],
'force' => $options['force'],
'verbose' => $options['verbose'],
) ) ) ) . "\n";
echo "output: " . $options['output'] . "\n";
echo "port: " . $options['port'] . "\n";
WordPress\CLI\CLI;
$option_defs = array(
'site-url' => array( 'u', true, null, 'Public site URL (, 'https://mysite.test' );
try {
list( , $options ) = CLI::parse_command_args_and_options( $argv, $option_defs );
foreach ( array( 'site-url', 'site-path' ) as $name ) {
if ( null === $options[ $name ] ) {
throw new RuntimeException( "Missing
WordPress\CLI\CLI;
$option_defs = array(
'output' => array( 'o', true, null, 'Write result to FILE' ),
'force' => array( 'f', false, false, 'Overwrite existing files' ),
'verbose' => array( 'v', false, false, 'Verbose output' ),
'help' => array( 'h', false, false, 'Show this help and exit' ),
);
function render_help( array $defs ) {
echo "Usage: mytool [options] <input>\n\nOptions:\n";
foreach ( $defs as $long => $def ) {
list( $short, $has_value, $default, $desc ) = $def;
$flag = ( $short ? "-{$short}, " : ' ' ) . "--{$long}";
if ( $has_value ) $flag .= '=VALUE';
echo sprintf( " %-28s %s\n", $flag, $desc );
}
}
list( , $options ) = CLI::parse_command_args_and_options( array( '-h' ), $option_defs );
if ( $options['help'] ) render_help( $option_defs );
WordPress\CLI\CLI;
$commands = array(
'deploy' => array(
'env' => array( 'e', true, 'staging', 'Target environment' ),
'dry-run' => array( 'n', false, false, 'Preview without applying' ),
),
'rollback' => array(
'to' => array( 't', true, null, 'Revision to roll back to' ),
),
);
function run( array $argv, array $commands ) {
if ( empty( $argv ) ) {
echo "Usage: mytool <command> [options]\nCommands: " . implode( ', ', array_keys( $commands ) ) . "\n";
return;
}
$command = array_shift( $argv );
if ( ! isset( $commands[ $command ] ) ) {
echo "Unknown command: {$command}\n";
return;
}
list( $positionals, $options ) = CLI::parse_command_args_and_options( $argv, $commands[ $command ] );
echo "command={$command}\n";
echo "options: " . json_encode( $options ) . "\n";
echo "positionals: " . json_encode( $positionals ) . "\n";
}
run( array( 'deploy', '--env=production', '-n', 'web-01', 'web-02' ), $commands );
echo "---\n";
run( array( 'rollback', '-t', 'abc123' ), $commands );