Skip to content

Fix handling of positional arguments.

Kevin Cox requested to merge kevincox/blackbox:pos-args into main

Previously blackbox attempted to require that arguments are behind a --. While this is likely a good idea because commands frequently have flags and not using -- is a footgun, this doesn't work in practice because GLib strips -- if it is "unnecessary" before we see the arguments. This means that for commands like blackbox -- vi -e everything will work properly. But for backbox -- vi GLib will strip the -- and the previous parser would ignore the arguments.

This change leaves the documentation suggesting that -- is required as it is a good idea but properly handles the case where GLib removes the "unnecessary" --. Additionally it raises an error if both -c and positional arguments are used as that is ambiguous. Before the positional arguments were just ignored. This catches dumb cases like blackbox -c top -- vi but also somewhat reasonable looking cases such as blackbox -c vi myfile.txt.

Additionally positional arguments are properly quoted. Previously they were just smushed into one string then parsed by the shell. This means that things like blackbox -- vi "my file.txt" wouldn't work as it would be executed as vi "my" "file.txt". Ideally we would probably just keep the args as an array and skip the shell entirely but at least this is correct, if not efficient and elegant. (Assuming that GLib.Shell.quote is correct for your shell which isn't guaranteed. But that is best left as a future improvement.)

Fixes #276

Edited by Kevin Cox

Merge request reports