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.[0] = '-' ->
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
Note that this is the standard command-line behaviour in the Unix world (try it with your favourite C compiler).
Only extensible sum types can be, well, extended. Ordinary sum types cannot be extended. Note that it wouldn’t make sense to extend Arg.spec, as the module Arg would not know what to do with the new constructor.
Note that this is the standard command-line behaviour in the Unix world (try it with your favourite C compiler).
You’re right.
I thought filenames with leading dashes must be “escaped” with -- in
the Unix world, e.g. gcc -o -- -v file.c. Turns out this is not the
case and thus shouldn’t be “fixed” in the Stdlib.
Only extensible sum types can be, well, extended.
Ordinary sum types cannot be extended.
I skimed over https://sites.google.com/site/ocamlopen/ and
misinterpreted the “…” as arbitrary constructors.
Thus I concluded all types are extensible since 4.02.
I was wrong again.
Note that it wouldn’t make sense to extend Arg.spec, as the module Arg would not know what to do with the new constructor.
This isn’t really a solution, but … have you thought about learning to use the excellent, excellent (did I mention it’s excellent) cmdliner package? It was sort of meant for this sort of thing – adding behaviour to the processing of args in a modular way, I mean.