    {-# LANGUAGE RecordWildCards #-}

    module System.Console.CmdArgs.Implicit.Reform(reform) where

    import System.Console.CmdArgs.Implicit.Local
    import System.Console.CmdArgs.Implicit.Type
    import System.Console.CmdArgs.Verbosity

    import Data.Generics.Any
    import Data.List
    import Data.Maybe

    reform :: Prog_ -> CmdArgs Any -> Maybe [String]
    reform :: Prog_ -> CmdArgs Any -> Maybe [String]
    reform Prog_{Bool
    String
    [Mode_]
    Maybe [String]
    Maybe Builtin_
    (Maybe Builtin_, Maybe Builtin_)
    progModes :: [Mode_]
    progSummary :: Maybe [String]
    progProgram :: String
    progHelp :: String
    progVerbosityArgs :: (Maybe Builtin_, Maybe Builtin_)
    progHelpArg :: Maybe Builtin_
    progVersionArg :: Maybe Builtin_
    progNoAtExpand :: Bool
    progNoAtExpand :: Prog_ -> Bool
    progVersionArg :: Prog_ -> Maybe Builtin_
    progHelpArg :: Prog_ -> Maybe Builtin_
    progVerbosityArgs :: Prog_ -> (Maybe Builtin_, Maybe Builtin_)
    progHelp :: Prog_ -> String
    progProgram :: Prog_ -> String
    progSummary :: Prog_ -> Maybe [String]
    progModes :: Prog_ -> [Mode_]
    ..} CmdArgs{Maybe String
    Maybe Verbosity
    Any
    CmdArgsPrivate
    cmdArgsValue :: Any
    cmdArgsHelp :: Maybe String
    cmdArgsVersion :: Maybe String
    cmdArgsVerbosity :: Maybe Verbosity
    cmdArgsPrivate :: CmdArgsPrivate
    cmdArgsPrivate :: forall a. CmdArgs a -> CmdArgsPrivate
    cmdArgsVerbosity :: forall a. CmdArgs a -> Maybe Verbosity
    cmdArgsVersion :: forall a. CmdArgs a -> Maybe String
    cmdArgsHelp :: forall a. CmdArgs a -> Maybe String
    cmdArgsValue :: forall a. CmdArgs a -> a
    ..} = [String] -> Maybe [String]
    forall a. a -> Maybe a
    Just ([String] -> Maybe [String]) -> [String] -> Maybe [String]
    forall a b. (a -> b) -> a -> b
    $
        String -> Maybe Builtin_ -> Bool -> [String]
    f String
    "help" Maybe Builtin_
    progHelpArg (Maybe String -> Bool
    forall a. Maybe a -> Bool
    isJust Maybe String
    cmdArgsHelp) [String] -> [String] -> [String]
    forall a. [a] -> [a] -> [a]
    ++
        String -> Maybe Builtin_ -> Bool -> [String]
    f String
    "version" Maybe Builtin_
    progVersionArg (Maybe String -> Bool
    forall a. Maybe a -> Bool
    isJust Maybe String
    cmdArgsVersion) [String] -> [String] -> [String]
    forall a. [a] -> [a] -> [a]
    ++
        String -> Maybe Builtin_ -> Bool -> [String]
    f String
    "verbose" ((Maybe Builtin_, Maybe Builtin_) -> Maybe Builtin_
    forall a b. (a, b) -> a
    fst (Maybe Builtin_, Maybe Builtin_)
    progVerbosityArgs) (Maybe Verbosity
    cmdArgsVerbosity Maybe Verbosity -> Maybe Verbosity -> Bool
    forall a. Eq a => a -> a -> Bool
    == Verbosity -> Maybe Verbosity
    forall a. a -> Maybe a
    Just Verbosity
    Loud) [String] -> [String] -> [String]
    forall a. [a] -> [a] -> [a]
    ++
        String -> Maybe Builtin_ -> Bool -> [String]
    f String
    "quiet" ((Maybe Builtin_, Maybe Builtin_) -> Maybe Builtin_
    forall a b. (a, b) -> b
    snd (Maybe Builtin_, Maybe Builtin_)
    progVerbosityArgs) (Maybe Verbosity
    cmdArgsVerbosity Maybe Verbosity -> Maybe Verbosity -> Bool
    forall a. Eq a => a -> a -> Bool
    == Verbosity -> Maybe Verbosity
    forall a. a -> Maybe a
    Just Verbosity
    Quiet)
        where
            f :: String -> Maybe Builtin_ -> Bool -> [String]
    f String
    ex (Just Builtin_
    x) Bool
    True = [String] -> [String]
    pickArg ([String] -> [String]) -> [String] -> [String]
    forall a b. (a -> b) -> a -> b
    $ Builtin_ -> [String]
    builtinNames Builtin_
    x [String] -> [String] -> [String]
    forall a. [a] -> [a] -> [a]
    ++ [String
    ex]
            f String
    _ Maybe Builtin_
    _ Bool
    _ = []

    pickArg :: [String] -> [String]
    pickArg :: [String] -> [String]
    pickArg [String]
    xs = case (String -> Bool) -> [String] -> ([String], [String])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    partition (Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    (==) Int
    1 (Int -> Bool) -> (String -> Int) -> String -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . String -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length) [String]
    xs of
        ([String]
    _, String
    x:[String]
    _) -> [String
    "--" String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    x]
        (String
    x:[String]
    _, [String]
    _) -> [String
    "-" String -> String -> String
    forall a. [a] -> [a] -> [a]
    ++ String
    x]
        ([String], [String])
    _ -> []

    {-

    data Prog_ = Prog_
        {progModes :: [Mode_]
        ,progSummary :: Maybe [String]
        ,progProgram :: String
        ,progHelp :: String -- only for multiple mode programs
        ,progVerbosityArgs :: (Maybe Builtin_, Maybe Builtin_)
        ,progHelpArg :: Maybe Builtin_
        ,progVersionArg :: Maybe Builtin_
        } deriving Show
    -}