This is not a Python problem; it is your shell that parses the backslashes in the command line before sending it to Python. optparse
is not to blame.
Escape them by doubling the slashes:
./script.py --dir c:\\a\\b
or put quotes around the argument:
./script.py --dir "c:\a\b"