Skip to content

Commit a48b2ec

Browse files
authored
Merge pull request #5924 from tfirdaus/fix/alias-prefix
2 parents 9aec20f + 09f9227 commit a48b2ec

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

features/aliases.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,30 @@ Feature: Create shortcuts to specific WordPress installs
637637

638638
When I try `wp cli alias is-group @foo`
639639
Then the return code should be 1
640+
641+
Scenario: Automatically add "@" prefix to an alias
642+
Given a WP install
643+
And a wp-cli.yml file:
644+
"""
645+
@foo:
646+
path: foo
647+
"""
648+
649+
When I run `wp cli alias add hello --set-path=/path/to/wordpress`
650+
Then STDOUT should be:
651+
"""
652+
Success: Added '@hello' alias.
653+
"""
654+
655+
When I run `wp eval --skip-wordpress 'echo realpath( getenv( "RUN_DIR" ) );'`
656+
Then save STDOUT as {TEST_DIR}
657+
658+
When I run `wp cli alias list`
659+
Then STDOUT should be YAML containing:
660+
"""
661+
@all: Run command against every registered alias.
662+
@hello:
663+
path: /path/to/wordpress
664+
@foo:
665+
path: {TEST_DIR}/foo
666+
"""

php/class-wp-cli.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ public static function error_to_string( $errors ) {
10901090

10911091
// PHP 7+: internal and user exceptions must implement Throwable interface.
10921092
// PHP 5: internal and user exceptions must extend Exception class.
1093-
if ( interface_exists( 'Throwable' ) && ( $errors instanceof Throwable ) || ( $errors instanceof Exception ) ) {
1093+
if ( ( interface_exists( 'Throwable' ) && ( $errors instanceof Throwable ) ) || ( $errors instanceof Exception ) ) {
10941094
return get_class( $errors ) . ': ' . $errors->getMessage();
10951095
}
10961096

php/commands/src/CLI_Alias_Command.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ private function validate_config_file( $config_path ) {
408408
* @return mixed
409409
*/
410410
private function build_aliases( $aliases, $alias, $assoc_args, $is_grouping, $grouping = '', $is_update = false ) {
411+
$alias = $this->normalize_alias( $alias );
411412

412413
if ( $is_grouping ) {
413414
$valid_assoc_args = [ 'config', 'grouping' ];
@@ -507,6 +508,8 @@ private function validate_alias_type( $aliases, $alias, $assoc_args, $grouping )
507508
* @param string $operation Current operation string fro message.
508509
*/
509510
private function process_aliases( $aliases, $alias, $config_path, $operation = '' ) {
511+
$alias = $this->normalize_alias( $alias );
512+
510513
// Convert data to YAML string.
511514
$yaml_data = Spyc::YAMLDump( $aliases );
512515

@@ -515,4 +518,21 @@ private function process_aliases( $aliases, $alias, $config_path, $operation = '
515518
WP_CLI::success( "$operation '{$alias}' alias." );
516519
}
517520
}
521+
522+
/**
523+
* Normalize the alias to an expected format.
524+
*
525+
* - Add @ if not present.
526+
*
527+
* @param string $alias Name of alias.
528+
*/
529+
private function normalize_alias( $alias ) {
530+
// Check if the alias starts with the @.
531+
// See: https://github.com/wp-cli/wp-cli/issues/5391
532+
if ( strpos( $alias, '@' ) !== 0 ) {
533+
$alias = '@' . ltrim( $alias, '@' );
534+
}
535+
536+
return $alias;
537+
}
518538
}

0 commit comments

Comments
 (0)