@@ -36,43 +36,60 @@ use crate::cli::{
3636
3737/// Normalize CLI arguments:
3838/// - `vp list ...` / `vp ls ...` → `vp pm list ...`
39+ /// - `vp rebuild ...` → `vp pm rebuild ...`
3940/// - `vp help [command]` → `vp [command] --help`
4041/// - `vp node [args...]` → `vp env exec node [args...]`
4142fn normalize_args ( args : Vec < String > ) -> Vec < String > {
42- match args. get ( 1 ) . map ( String :: as_str) {
43- // `vp list ...` → `vp pm list ...`
44- // `vp ls ...` → `vp pm list ...`
45- Some ( "list" | "ls" ) => {
46- let mut normalized = Vec :: with_capacity ( args. len ( ) + 1 ) ;
47- normalized. push ( args[ 0 ] . clone ( ) ) ;
48- normalized. push ( "pm" . to_string ( ) ) ;
49- normalized. push ( "list" . to_string ( ) ) ;
50- normalized. extend ( args[ 2 ..] . iter ( ) . cloned ( ) ) ;
51- normalized
52- }
53- // `vp help` alone -> show main help
54- Some ( "help" ) if args. len ( ) == 2 => vec ! [ args[ 0 ] . clone( ) , "--help" . to_string( ) ] ,
55- // `vp help [command] [args...]` -> `vp [command] --help [args...]`
56- Some ( "help" ) if args. len ( ) > 2 => {
57- let mut normalized = Vec :: with_capacity ( args. len ( ) ) ;
58- normalized. push ( args[ 0 ] . clone ( ) ) ;
59- normalized. push ( args[ 2 ] . clone ( ) ) ;
60- normalized. push ( "--help" . to_string ( ) ) ;
61- normalized. extend ( args[ 3 ..] . iter ( ) . cloned ( ) ) ;
62- normalized
63- }
64- // `vp node [args...]` → `vp env exec node [args...]`
65- Some ( "node" ) => {
66- let mut normalized = Vec :: with_capacity ( args. len ( ) + 2 ) ;
67- normalized. push ( args[ 0 ] . clone ( ) ) ;
68- normalized. push ( "env" . to_string ( ) ) ;
69- normalized. push ( "exec" . to_string ( ) ) ;
70- normalized. push ( "node" . to_string ( ) ) ;
71- normalized. extend ( args[ 2 ..] . iter ( ) . cloned ( ) ) ;
72- normalized
73- }
74- // No transformation needed
75- _ => args,
43+ let mut normalized = args;
44+ loop {
45+ let next = match normalized. get ( 1 ) . map ( String :: as_str) {
46+ // `vp list ...` → `vp pm list ...`
47+ // `vp ls ...` → `vp pm list ...`
48+ Some ( "list" | "ls" ) => {
49+ let mut next = Vec :: with_capacity ( normalized. len ( ) + 1 ) ;
50+ next. push ( normalized[ 0 ] . clone ( ) ) ;
51+ next. push ( "pm" . to_string ( ) ) ;
52+ next. push ( "list" . to_string ( ) ) ;
53+ next. extend ( normalized[ 2 ..] . iter ( ) . cloned ( ) ) ;
54+ next
55+ }
56+ // `vp rebuild ...` → `vp pm rebuild ...`
57+ Some ( "rebuild" ) => {
58+ let mut next = Vec :: with_capacity ( normalized. len ( ) + 1 ) ;
59+ next. push ( normalized[ 0 ] . clone ( ) ) ;
60+ next. push ( "pm" . to_string ( ) ) ;
61+ next. push ( "rebuild" . to_string ( ) ) ;
62+ next. extend ( normalized[ 2 ..] . iter ( ) . cloned ( ) ) ;
63+ next
64+ }
65+ // `vp help` alone -> show main help
66+ Some ( "help" ) if normalized. len ( ) == 2 => {
67+ vec ! [ normalized[ 0 ] . clone( ) , "--help" . to_string( ) ]
68+ }
69+ // `vp help [command] [args...]` -> `vp [command] --help [args...]`
70+ Some ( "help" ) if normalized. len ( ) > 2 => {
71+ let mut next = Vec :: with_capacity ( normalized. len ( ) ) ;
72+ next. push ( normalized[ 0 ] . clone ( ) ) ;
73+ next. push ( normalized[ 2 ] . clone ( ) ) ;
74+ next. push ( "--help" . to_string ( ) ) ;
75+ next. extend ( normalized[ 3 ..] . iter ( ) . cloned ( ) ) ;
76+ next
77+ }
78+ // `vp node [args...]` → `vp env exec node [args...]`
79+ Some ( "node" ) => {
80+ let mut next = Vec :: with_capacity ( normalized. len ( ) + 2 ) ;
81+ next. push ( normalized[ 0 ] . clone ( ) ) ;
82+ next. push ( "env" . to_string ( ) ) ;
83+ next. push ( "exec" . to_string ( ) ) ;
84+ next. push ( "node" . to_string ( ) ) ;
85+ next. extend ( normalized[ 2 ..] . iter ( ) . cloned ( ) ) ;
86+ next
87+ }
88+ // No transformation needed
89+ _ => return normalized,
90+ } ;
91+
92+ normalized = next;
7693 }
7794}
7895
@@ -434,6 +451,34 @@ mod tests {
434451 assert_eq ! ( normalized, s( & [ "vp" , "env" , "exec" , "node" ] ) ) ;
435452 }
436453
454+ #[ test]
455+ fn normalize_args_rewrites_bare_vp_rebuild ( ) {
456+ let input = s ( & [ "vp" , "rebuild" ] ) ;
457+ let normalized = normalize_args ( input) ;
458+ assert_eq ! ( normalized, s( & [ "vp" , "pm" , "rebuild" ] ) ) ;
459+ }
460+
461+ #[ test]
462+ fn normalize_args_rewrites_vp_rebuild_with_args ( ) {
463+ let input = s ( & [ "vp" , "rebuild" , "--" , "--update-binary" ] ) ;
464+ let normalized = normalize_args ( input) ;
465+ assert_eq ! ( normalized, s( & [ "vp" , "pm" , "rebuild" , "--" , "--update-binary" ] ) ) ;
466+ }
467+
468+ #[ test]
469+ fn normalize_args_rewrites_vp_help_rebuild ( ) {
470+ let input = s ( & [ "vp" , "help" , "rebuild" ] ) ;
471+ let normalized = normalize_args ( input) ;
472+ assert_eq ! ( normalized, s( & [ "vp" , "pm" , "rebuild" , "--help" ] ) ) ;
473+ }
474+
475+ #[ test]
476+ fn normalize_args_rewrites_vp_help_node ( ) {
477+ let input = s ( & [ "vp" , "help" , "node" ] ) ;
478+ let normalized = normalize_args ( input) ;
479+ assert_eq ! ( normalized, s( & [ "vp" , "env" , "exec" , "node" , "--help" ] ) ) ;
480+ }
481+
437482 #[ test]
438483 fn unknown_argument_detected_without_pass_as_value_hint ( ) {
439484 let error = try_parse_args_from ( [ "vp" . to_string ( ) , "--cache" . to_string ( ) ] )
0 commit comments