Shells in OCaml
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Position arguments

Expansions for position arguments like $0 and $1.

+25 -3
+7 -3
src/bin/main.ml
··· 2 2 module C = Merry.Eval.Make (Merry_posix.State) (Merry_posix.Exec) 3 3 module I = Merry.Interactive.Make (Merry_posix.State) (Merry_posix.Exec) 4 4 5 - let sh ~command ~dump ~file env = 5 + let sh ~command ~dump ~file ~rest env = 6 6 let executor = Merry_posix.Exec.{ mgr = env#process_mgr } in 7 7 let interactive = Option.is_none file && Option.is_none command in 8 + let pos_zero = match file with Some f -> f | None -> "msh" in 8 9 Eio.Switch.run @@ fun async_switch -> 9 10 let ctx = 10 11 C. ··· 22 23 async_switch; 23 24 background_jobs = []; 24 25 last_background_process = ""; 26 + argv = Array.of_list (pos_zero :: rest); 25 27 } 26 28 in 27 29 match (file, command) with ··· 56 58 in 57 59 Arg.(value & flag & info [ "d"; "D"; "dump" ] ~doc) 58 60 61 + let rest = Arg.(value & pos_right 0 string [] & info []) 62 + 59 63 let cmd env = 60 64 let doc = "A shell." in 61 65 let man = ··· 76 80 in 77 81 Cmd.make (Cmd.info "msh" ~version:"v0.0.1" ~doc ~man) 78 82 @@ 79 - let+ command = command and+ dump = dump and+ file = file in 80 - sh ~command ~dump ~file env 83 + let+ command = command and+ dump = dump and+ file = file and+ rest = rest in 84 + sh ~command ~dump ~file ~rest env 81 85 82 86 let main () = Eio_posix.run @@ fun env -> Cmd.eval (cmd env) 83 87
+7
src/lib/eval.ml
··· 39 39 background_jobs : J.t list; 40 40 last_background_process : string; 41 41 async_switch : Eio.Switch.t; 42 + argv : string array; 42 43 } 43 44 44 45 let clear_local_state ctx = { ctx with local_state = [] } ··· 92 93 match v with 93 94 | Ast.VariableAtom ("!", NoAttribute) -> 94 95 Ast.WordName ctx.last_background_process :: expand rest 96 + | Ast.VariableAtom (n, NoAttribute) 97 + when Option.is_some (int_of_string_opt n) -> ( 98 + let n = int_of_string n in 99 + match Array.get ctx.argv n with 100 + | v -> Ast.WordName v :: expand rest 101 + | exception Invalid_argument _ -> Ast.WordName "" :: expand rest) 95 102 | Ast.VariableAtom (s, NoAttribute) -> ( 96 103 match S.lookup ctx.state ~param:s with 97 104 | None -> Ast.WordName "" :: expand rest
+11
test/functions.t
··· 1 + Testing functions and position arguments. 2 + 3 + $ cat > test.sh << EOF 4 + > echo "\$1 from \$0" 5 + > EOF 6 + 7 + $ sh test.sh hello 8 + hello from test.sh 9 + $ msh test.sh hello 10 + hello from test.sh 11 +