@@ -23,17 +23,32 @@ mod options {
2323 pub const DISABLE_BACKSLASH_ESCAPE : & str = "disable_backslash_escape" ;
2424}
2525
26+ fn is_echo_flag ( arg : & OsString ) -> bool {
27+ matches ! ( arg. to_str( ) , Some ( "-e" | "-E" | "-n" ) )
28+ }
29+
2630// A workaround because clap interprets the first '--' as a marker that a value
2731// follows. In order to use '--' as a value, we have to inject an additional '--'
2832fn handle_double_hyphens ( args : impl uucore:: Args ) -> impl uucore:: Args {
2933 let mut result = Vec :: new ( ) ;
30-
31- for ( i, arg) in args. enumerate ( ) {
32- // check for argument at index 1 which is the first argument to echo (0 being "echo")
33- if i == 1 && arg == "--" {
34- result. push ( OsString :: from ( "--" ) ) ;
34+ let mut is_first_argument = true ;
35+ let mut args_iter = args. into_iter ( ) ;
36+
37+ if let Some ( first_val) = args_iter. next ( ) {
38+ // the first argument ('echo') gets pushed before we start with the checks for flags/'--'
39+ result. push ( first_val) ;
40+ // We need to skip any possible Flag arguments until we find the first argument to echo that
41+ // is not a flag. If the first argument is double hyphen we inject an additional '--'
42+ // otherwise we switch is_first_argument boolean to skip the checks for any further arguments
43+ for arg in args_iter {
44+ if is_first_argument && !is_echo_flag ( & arg) {
45+ is_first_argument = false ;
46+ if arg == "--" {
47+ result. push ( OsString :: from ( "--" ) ) ;
48+ }
49+ }
50+ result. push ( arg) ;
3551 }
36- result. push ( arg) ;
3752 }
3853
3954 result. into_iter ( )
0 commit comments