File tree Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Expand file tree Collapse file tree 3 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -417,8 +417,22 @@ impl Lexer {
417417 if g. delimiter ( ) == Delimiter :: Brace {
418418 self . extend_last_arg ( quote ! ( #var. as_os_str( ) ) ) ;
419419 } else {
420+ // For vector variables $[var], we need to check if there's content
421+ // immediately before (without space), which would be invalid like: xxx$[var]
422+ // But if last_arg_str is just the command name, it should be flushed first
420423 if !self . last_arg_str . is_empty ( ) {
421- abort ! ( span, "vector variable can only be used alone" ) ;
424+ // Check if this is the first argument (command name)
425+ if self . args . is_empty ( ) {
426+ // Flush the command name as the first argument
427+ self . args . push ( ParseArg :: ArgStr ( self . last_arg_str . clone ( ) ) ) ;
428+ self . last_arg_str = TokenStream :: new ( ) ;
429+ } else {
430+ // This is an error: something like xxx$[var] without space
431+ abort ! (
432+ span,
433+ "vector variable cannot have content immediately before it"
434+ ) ;
435+ }
422436 }
423437 self . args . push ( ParseArg :: ArgVec ( quote ! ( #var) ) ) ;
424438 }
Original file line number Diff line number Diff line change 5656//! run_fun!(echo "${msg 0}");
5757//! ```
5858//!
59+ //! ### Invalid vector variable syntax
60+ //!
61+ //! Vector variables cannot have text immediately before them:
62+ //! ```compile_fail
63+ //! # use cmd_lib::*;
64+ //! let opts = ["-a", "-f"];
65+ //! run_cmd!(ls xxx$[opts]);
66+ //! ```
67+ //!
68+ //! ```compile_fail
69+ //! # use cmd_lib::*;
70+ //! let msg = "hello";
71+ //! let opts = ["-a", "-f"];
72+ //! run_cmd!(ls $msg$[opts]);
73+ //! ```
74+ //!
5975//! ### Invalid redirect syntax
6076//!
6177//! Invalid redirect operator spacing:
Original file line number Diff line number Diff line change @@ -372,3 +372,9 @@ fn test_empty_arg() {
372372fn test_env_var_with_equal_sign ( ) {
373373 assert ! ( run_cmd!( A ="-c B=c" echo) . is_ok( ) ) ;
374374}
375+
376+ #[ test]
377+ fn test_vector_variable ( ) {
378+ let opts = [ "-n" , "hello" ] ;
379+ assert ! ( run_cmd!( echo $[ opts] ) . is_ok( ) )
380+ }
You can’t perform that action at this time.
0 commit comments