getopt should consume options terminator "--".

As stated by the docs. (Cf. issue 9583)
Add unittest.
Improve code based on feedback.
Merge pull request #1159 from quickfur/getopt_optend

Fix issue 9583