WP_CLI
Completions::__construct
Instantiate a Completions object.
Method of the class: Completions{}
No Hooks.
Returns
null. Nothing (null).
Usage
$Completions = new Completions(); $Completions->__construct( $line );
- $line(string) (required)
- Line of shell input to compute a completion for.
Completions::__construct() Completions:: construct code WP-CLI 2.13.0-alpha
public function __construct( $line ) {
// TODO: properly parse single and double quotes
$this->words = explode( ' ', $line );
// First word is always `wp`.
array_shift( $this->words );
// Last word is either empty or an incomplete subcommand.
$this->cur_word = end( $this->words );
if ( '' !== $this->cur_word && ! preg_match( '/^\-/', $this->cur_word ) ) {
array_pop( $this->words );
}
$is_alias = false;
$is_help = false;
if ( ! empty( $this->words[0] ) && preg_match( '/^@/', $this->words[0] ) ) {
array_shift( $this->words );
// `wp @al` is false, but `wp @all ` is true.
if ( count( $this->words ) ) {
$is_alias = true;
}
} elseif ( ! empty( $this->words[0] ) && 'help' === $this->words[0] ) {
array_shift( $this->words );
$is_help = true;
}
$r = $this->get_command( $this->words );
if ( ! is_array( $r ) ) {
return;
}
list( $command, $args, $assoc_args ) = $r;
$spec = SynopsisParser::parse( $command->get_synopsis() );
foreach ( $spec as $arg ) {
if ( 'positional' === $arg['type'] && 'file' === $arg['name'] ) {
$this->add( '<file> ' );
return;
}
}
if ( $command->can_have_subcommands() ) {
// Add completion when command is `wp` and alias isn't set.
if ( 'wp' === $command->get_name() && false === $is_alias && false === $is_help ) {
$aliases = WP_CLI::get_configurator()->get_aliases();
foreach ( $aliases as $name => $_ ) {
$this->add( "$name " );
}
}
foreach ( $command->get_subcommands() as $name => $_ ) {
$this->add( "$name " );
}
} else {
foreach ( $spec as $arg ) {
if ( in_array( $arg['type'], [ 'flag', 'assoc' ], true ) ) {
if ( isset( $assoc_args[ $arg['name'] ] ) ) {
continue;
}
$opt = "--{$arg['name']}";
if ( 'flag' === $arg['type'] ) {
$opt .= ' ';
} elseif ( ! $arg['value']['optional'] ) {
$opt .= '=';
}
$this->add( $opt );
}
}
foreach ( $this->get_global_parameters() as $param => $runtime ) {
if ( isset( $assoc_args[ $param ] ) ) {
continue;
}
$opt = "--{$param}";
if ( '' === $runtime || ! is_string( $runtime ) ) {
$opt .= ' ';
} else {
$opt .= '=';
}
$this->add( $opt );
}
}
}