getopt1 (a1,a2) (x:xs) opts =
   case x of
     "--"       -> (reverse a1,(reverse a2)++xs)
     ('-':c:cs) ->
       if isFlg c opts
       then if null cs
            then getopt1 ((OptFlg c):a1,a2) xs opts
            else getopt1 ((OptFlg c):a1,a2) (('-':cs):xs) opts
       else if hasArg c opts
            then if null cs
                 then if null xs
                      then ([OptErr e1],[])
                      else let (y:ys) = xs in
                           case y of
                             ('-':_) ->
                               ([OptErr e1],[])
                             _       ->
                               getopt1 ((OptArg c y):a1,a2) ys opts
                 else getopt1 ((OptArg c cs):a1,a2) xs opts
            else ([OptErr e2],[])
       where
       e1 = "argument is required for this option -- " ++ [c]
       e2 = "unknown option -- " ++ [c]
     _          -> 
       getopt1 (a1,x:a2) xs opts
getopt1 (a1,a2) (x:xs) opts =
   case x of
     "--"       -> (reverse a1,(reverse a2)++xs)
     ('-':c:cs) ->
       if isFlg c opts
       then if null cs
            then getopt1 ((OptFlg c):a1,a2) xs opts
            else getopt1 ((OptFlg c):a1,a2) (('-':cs):xs) opts
       else if hasArg c opts
            then if null cs
                 then if null xs
                      then ([OptErr e1],[])
                      else let (y:ys) = xs in
                           case y of
                             ('-':_) ->
                               ([OptErr e1],[])
                             _       ->
                               getopt1 ((OptArg c y):a1,a2) ys opts
                 else getopt1 ((OptArg c cs):a1,a2) xs opts
            else ([OptErr e2],[])
       where
       e1 = "argument is required for this option -- " ++ [c]
       e2 = "unknown option -- " ++ [c]
     _          -> 
       getopt1 (a1,x:a2) xs opts
getopt1 (a1,a2) (x:xs) opts =
   case x of
     "--"       -> (reverse a1,(reverse a2)++xs)
     ('-':c:cs) ->
       if isFlg c opts
       then if null cs
            then getopt1 ((OptFlg c):a1,a2) xs opts
            else getopt1 ((OptFlg c):a1,a2) (('-':cs):xs) opts
       else if hasArg c opts
            then if null cs
                 then if null xs
                      then ([OptErr e1],[])
                      else let (y:ys) = xs in
                           case y of
                             ('-':_) ->
                               ([OptErr e1],[])
                             _       ->
                               getopt1 ((OptArg c y):a1,a2) ys opts
                 else getopt1 ((OptArg c cs):a1,a2) xs opts
            else ([OptErr e2],[])
       where
       e1 = "argument is required for this option -- " ++ [c]
       e2 = "unknown option -- " ++ [c]
     _          -> 
       getopt1 (a1,x:a2) xs opts