getopt should consume options terminator "--".

As stated by the docs. (Cf. issue 9583)
Add unittest.
Improve code based on feedback.
Revert "Merge pull request #1170 from jmdavis/perf"

This reverts commit 684c43dfc52105b232641965702a10295d0ba7cb, reversing
changes made to 707a60101b41ac54af2f757beec89875fe51ac37.
Merge pull request #1159 from quickfur/getopt_optend

Fix issue 9583