Skip to content

Commit

Permalink
Standard completion of current option
Browse files Browse the repository at this point in the history
Make options complete in a manner more consistent with other shell programs.

Closes #5845.
  • Loading branch information
Roy-Orbison committed Nov 20, 2023
1 parent a92e34a commit 3e3d850
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 8 additions & 2 deletions features/cli-bash-completion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ Feature: `wp cli completions` tasks
"""

When I run `wp cli completions --line='wp config create --dbname=' --point=100`
Then STDOUT should be empty
Then STDOUT should contain:
"""
--dbname=
"""

When I run `wp cli completions --line='wp config create --dbname=foo ' --point=100`
Then STDOUT should not contain:
Expand Down Expand Up @@ -321,7 +324,10 @@ Feature: `wp cli completions` tasks
"""

When I run `wp cli completions --line="wp core download --no-color" --point=100`
Then STDOUT should not contain:
Then STDOUT should contain:
"""
--no-color
"""

When I run `wp cli completions --line="wp core download --no-color --no-color" --point=100`
Then STDOUT should be empty
8 changes: 7 additions & 1 deletion php/WP_CLI/Completions.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,14 @@ private function get_command( $words ) {
$positional_args = [];
$assoc_args = [];

foreach ( $words as $arg ) {
# Avoid having to polyfill array_key_last().
end( $words );
$last_arg_i = key( $words );
foreach ( $words as $i => $arg ) {
if ( preg_match( '|^--([^=]+)=?|', $arg, $matches ) ) {
if ( $i === $last_arg_i ) {
continue;
}
$assoc_args[ $matches[1] ] = true;
} else {
$positional_args[] = $arg;
Expand Down

0 comments on commit 3e3d850

Please sign in to comment.