Closed yawaramin closed 5 years ago
I goofed and it expects at least 2 args, fixing that today.
Here's a sample usage of the OCaml built-in Arg
module:
// Main.re
let prepend(item, listRef) = listRef := [item, ...listRef^];
let addEnvFile(envFiles) = Arg.String(envFile => prepend(envFile, envFiles));
// Need 'rest args' to stop parsing the command line
let addRestArg(restArgs) = Arg.Rest(arg => prepend(arg, restArgs));
let specList(envFiles, restArgs) = [
("-e", addEnvFile(envFiles), "FILE Load environment variables from FILE"),
("--", addRestArg(restArgs), "[COMMAND [ARGS]] Command to run"),
];
let usage = {|NAME
reenv - load environment variables and execute command
SYNOPSIS
reenv [-e FILE] [-help] [--help] [-- COMMAND [ARGS]]
OPTIONS|};
let () = {
let envFiles = ref([]);
let restArgs = ref([]);
Arg.parse(specList(envFiles, restArgs), ignore, usage);
// Load env vars from envFiles ... then run command:
restArgs^ |> List.rev |> String.concat(" ") |> Sys.command |> ignore;
};
Output looks like this:
$ esy x Hello.exe -h
/Users/yawar/src/hello-reason/_esy/default/store/i/hello_reason-f08a77ce/bin/Hello.exe: unknown option '-h'.
NAME
reenv - load environment variables and execute command
SYNOPSIS
reenv [-e FILE] [-help] [--help] [-- COMMAND [ARGS]]
OPTIONS
-e FILE Load environment variables from FILE
-- [COMMAND [ARGS]] Command to run
-help Display this list of options
--help Display this list of options
Thanks for the suggestion on Arg
, I ended up using Cmdliner
anyway as I wanted to learn that API.
The issue should be fixed in 0.2.1 that I just tagged. Closing tomorrow if this works.
Gotcha. Yeah that makes sense. Looking forward to trying it out!
Here is the error:
Version is 0.2.0.
(
dotenv ls
works.reenv ls subdir
also works for a givensubdir
.)