-//gopt{|Maybe|} (Positionals [ReqArg f d]) = Positionals
-// [OptArg (\ms _ ->case ms of
-// Nothing = Ok Nothing
-// // Is this necessary
-// Just s = case f s undef of
-// Ok a = Ok (Just a)
-// Error e = Error e
-// ) d]
-//gopt{|PAIR|} fx fg = case (fx, fg) of
-// (Options as, Options bs) = Options $
-// [Option s r (topair (\(PAIR l r)->l) (\(PAIR _ r) l->PAIR l r) f) d\\(Option s r f d)<-as]
-// ++ [Option s r (topair (\(PAIR l r)->r) (\(PAIR l _) r->PAIR l r) f) d\\(Option s r f d)<-bs]
-//topair fr to (NoArg fa) = NoArg \a->case fa (fr a) of
-// Ok na = Ok (to a na)
-// Error e = Error e
-//topair fr to (ReqArg fa d) = ReqArg (\s a->case fa s (fr a) of
-// Ok na = Ok (to a na)
-// Error e = Error e) d
-//topair fr to (OptArg fa d) = OptArg (\s a->case fa s (fr a) of
-// Ok na = Ok (to a na)
-// Error e = Error e) d
-//
-parseOpts :: (Opt a) [String] a -> MaybeError [String] a
+
+ ptoarg [p] [] i = Error ["Not enough arguments for " +++ gfd_name]
+ ptoarg [p:ps] [a:as] i = p a i >>= ptoarg ps as
+ ptoarg [] as i = Ok (i, as)
+gopt{|PAIR|} l r = case (l, r) of
+ (Positionals pl, Positionals pr)
+ = Positionals
+ $ map (combine PFst appPFst) pl
+ ++ map (combine PSnd appPSnd) pr
+ (Flags fl, Flags fr)
+ = Flags
+ $ map (appSnd $ combine` PFst appPFst) fl
+ ++ map (appSnd $ combine` PSnd appPSnd) fr
+where
+ appPFst f (PAIR x y) = PAIR (f x) y
+ appPSnd f (PAIR x y) = PAIR x (f y)
+ PFst (PAIR x y) = x
+ PSnd (PAIR x y) = y
+gopt{|UNIT|} = Positionals []
+gopt{|CONS of {gcd_name}|} c = bifmap (\(CONS a)->a) CONS $ SubParsers [(gcd_name, c)]
+gopt{|EITHER|} l r = case (l, r) of
+ (SubParsers sl, SubParsers sr)
+ = SubParsers
+ $ map (appSnd $ bifmap (\(LEFT a)->a) LEFT) sl
+ ++ map (appSnd $ bifmap (\(RIGHT a)->a) RIGHT) sr
+gopt{|(,)|} l r = case (l, r) of
+ (Positionals pl, Positionals pr)
+ = Positionals
+ $ map (combine fst appFst) pl
+ ++ map (combine snd appSnd) pr
+gopt{|(,,)|} f s t = case (f, s, t) of
+ (Positionals pf, Positionals ps, Positionals pt)
+ = Positionals
+ $ map (combine fst3 appFst3) pf
+ ++ map (combine snd3 appSnd3) ps
+ ++ map (combine thd3 appThd3) pt
+
+consPrint (Positionals x) = "Positionals"
+consPrint (BinaryFlag x _) = "BinaryFlag"
+consPrint (Flags x) = "Flags"
+consPrint (SubParsers x) = "SubParsers"
+
+parseOpts :: (Opt a) [String] a -> MaybeError [String] (a, [String])