is it possible to extend Arg with Arg.File?
My use case
My (imaginary) tool has two options:
-o expects an output filename
-v turns verbose logging on.
So this is the spec:
[ "-o", Arg.String (fun fname -> outfile := fname), "output filename"; "-v", Arg.Set verbose, "verbose mode"; ]
When a user mistakenly omits the filename after
-o and invokes the tool with
mytool -o -v the output is written into a file “-v”.
Most likely, this wasn’t the intended behavior.
My current workarround
Whenever a string argument is supposed to be a filename I make sure it dosn’t start with a dash:
"-o", Arg.String (fun fname -> match fname with | "" -> eprintf "-o: filename cannot be empty string"; exit 1 | "-" -> outfile := fname (* stdin or stdout *) | s when s. = '-' -> eprintf "-o: filename missing before %S\n" fname; exit 1 | _ -> outfile := fname ), "outfile filename";
Problem with my workaround
I need this over and over again and would like to handle it with something like
"-o", Arg.File (fun fname -> outfile := fname), "output filename";
which implements the workaround internally.
My attempted solution
module Arg = struct include Arg type spec += File of (string -> unit) end
Problem with my attempted solution
It doesn’t compile :-/
Error: Type definition spec is not extensible
- Is there a better way to handle the situation than the work around described above?
- Whould it be appropriate to add
Arg.Fileto the Stdlib?