getopt should consume options terminator "--".

As stated by the docs. (Cf. issue 9583)
Add unittest.
Improve code based on feedback.