andrey-zherikov / argparse

Parser for command-line arguments
https://andrey-zherikov.github.io/argparse/
Boost Software License 1.0
30 stars 6 forks source link

`--` is handled unconventionally #117

Closed SirNickolas closed 7 months ago

SirNickolas commented 9 months ago

All ---aware programs I’ve encountered understand it the same way: it marks the end of named parameters; all arguments following it are bound to positional ones. To illustrate, these three invocations are equivalent:

cp -f -- a b
cp -f a -- b
cp -f a b

This is necessary in order to support binding an argument that starts with a dash to a positional one:

cp -- a -R # Copy the file named `a` to a file named `-R`.
cp a -- -R # Ditto.

This is important to a program user if the names aren’t known in advance:

cp -- "$from" "$to" # Will work predictably no matter what these variables contain.

If we were implementing cp in D, we’d like to declare it as follows:

struct Args {
    @PositionalArgument(0) string from;
    @PositionalArgument(1) string to;
}

…and expect everything to work out of the box.

A program should never need to know if it was invoked with -- or without. If it needs to accept a variable number of arguments, it just asks for @(PositionalArgument(0).Optional) string[ ] rest.