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


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