phobos icon indicating copy to clipboard operation
phobos copied to clipboard

std.getopt does not support order-sensitive options

Open CyberShadow opened this issue 1 month ago • 0 comments

Consider the following hypothetical program invocations:

$ program --include=dir1 --exclude=*.avi
$ program file1 -C dir2 file2 -C dir3 file3

Both examples are not currently supported by std.getopt:

  • In the first example, we lose the information of whether --include=dir1 was specified before --exclude=*.avi. As such, we would not be able to know if the user wants the program to act on .avi files in dir1.
  • In the second example, we lose the relative ordering of positional arguments and options, so we don't know which directory each file should be searched for.

The above examples are based on the CLI of real ubiquitous tools - see find(1), rsync(1), tar(1).

The first example is not supported because std.getopt acts on options in the order they are declared in the program, not the order they apprear on the command line. Thus, getopt(args, "include", ..., "exclude", ...) will process all --include arguments before any --exclude arguments. The second example is not supported because std.getopt does not allow capturing the relative order of positional arguments with respect to options; the two are separated, losing ordering, with one being returned in the mutated args and the other in the passed ref-variable or predicate.

We need:

  1. an option to switch the processing order of arguments to be in the order they were specified on the command line
  2. a method of receiving positional arguments in-band with options (e.g. via a predicate).

CyberShadow avatar Nov 19 '25 16:11 CyberShadow