@@ -22,39 +22,64 @@ impl ShellCommand for SetCommand {
2222}
2323
2424fn execute_set ( context : & mut ShellCommandContext ) -> ExecuteResult {
25- let mut changes = Vec :: new ( ) ;
2625 let args: Vec < String > = context
2726 . args
2827 . iter ( )
2928 . filter_map ( |a| a. to_str ( ) . map ( |s| s. to_string ( ) ) )
3029 . collect ( ) ;
3130
31+ // no arguments - in bash this would list all shell variables
32+ // for now, just return success
33+ if args. is_empty ( ) {
34+ return ExecuteResult :: from_exit_code ( 0 ) ;
35+ }
36+
37+ // set -o (list options in human-readable format)
38+ if args. len ( ) == 1 && args[ 0 ] == "-o" {
39+ let opts = context. state . shell_options ( ) ;
40+ let _ = context. stdout . write_line ( & format ! (
41+ "pipefail\t {}" ,
42+ if opts. contains( ShellOptions :: PIPEFAIL ) {
43+ "on"
44+ } else {
45+ "off"
46+ }
47+ ) ) ;
48+ return ExecuteResult :: from_exit_code ( 0 ) ;
49+ }
50+
51+ // set +o (output commands to recreate current settings)
52+ if args. len ( ) == 1 && args[ 0 ] == "+o" {
53+ let opts = context. state . shell_options ( ) ;
54+ let _ = context. stdout . write_line ( & format ! (
55+ "set {} pipefail" ,
56+ if opts. contains( ShellOptions :: PIPEFAIL ) {
57+ "-o"
58+ } else {
59+ "+o"
60+ }
61+ ) ) ;
62+ return ExecuteResult :: from_exit_code ( 0 ) ;
63+ }
64+
65+ // parse option changes: set -o opt1 -o opt2 +o opt3 ...
66+ let mut changes = Vec :: new ( ) ;
3267 let mut i = 0 ;
3368 while i < args. len ( ) {
3469 let arg = & args[ i] ;
35- if arg == "-o" || arg == "+o" {
70+ if ( arg == "-o" || arg == "+o" ) && i + 1 < args . len ( ) {
3671 let enable = arg == "-o" ;
37- if i + 1 < args. len ( ) {
38- let option_name = & args[ i + 1 ] ;
39- match option_name. as_str ( ) {
40- "pipefail" => {
41- changes. push ( EnvChange :: SetOption ( ShellOptions :: PIPEFAIL , enable) ) ;
42- }
43- _ => {
44- let _ = context
45- . stderr
46- . write_line ( & format ! ( "set: unknown option: {}" , option_name) ) ;
47- return ExecuteResult :: from_exit_code ( 1 ) ;
48- }
49- }
72+ let option_name = & args[ i + 1 ] ;
73+ if let Some ( option) = parse_option_name ( option_name) {
74+ changes. push ( EnvChange :: SetOption ( option, enable) ) ;
5075 i += 2 ;
5176 } else {
52- // No option name provided - in bash this would list options
53- // For now, just return success
54- i += 1 ;
77+ let _ = context
78+ . stderr
79+ . write_line ( & format ! ( "set: unknown option: {}" , option_name) ) ;
80+ return ExecuteResult :: from_exit_code ( 1 ) ;
5581 }
5682 } else {
57- // unknown argument
5883 let _ = context
5984 . stderr
6085 . write_line ( & format ! ( "set: invalid option: {}" , arg) ) ;
@@ -64,3 +89,10 @@ fn execute_set(context: &mut ShellCommandContext) -> ExecuteResult {
6489
6590 ExecuteResult :: Continue ( 0 , changes, Vec :: new ( ) )
6691}
92+
93+ fn parse_option_name ( name : & str ) -> Option < ShellOptions > {
94+ match name {
95+ "pipefail" => Some ( ShellOptions :: PIPEFAIL ) ,
96+ _ => None ,
97+ }
98+ }
0 commit comments