module Action.CmdLine(
CmdLine(..), Language(..), getCmdLine,
defaultGenerate,
whenLoud, whenNormal
) where
import System.Console.CmdArgs
import System.Directory
import System.Environment
import System.FilePath
import Data.List.Extra
import Data.Version
import Paths_hoogle(version)
data Language = Haskell | Frege deriving (Data,Typeable,Show,Eq,Enum,Bounded)
data CmdLine
= Search
{color :: Maybe Bool
,link :: Bool
,numbers :: Bool
,info :: Bool
,database :: FilePath
,count :: Int
,query :: [String]
,repeat_ :: Int
,language :: Language
,compare_ :: [String]
}
| Generate
{download :: Maybe Bool
,database :: FilePath
,insecure :: Bool
,include :: [String]
,local_ :: [Maybe FilePath]
,debug :: Bool
,language :: Language
}
| Server
{port :: Int
,database :: FilePath
,cdn :: String
,logs :: FilePath
,local :: Bool
,language :: Language
,scope :: String
,host :: String
,https :: Bool
,cert :: FilePath
,key :: FilePath
}
| Replay
{logs :: FilePath
,database :: FilePath
,repeat_ :: Int
,language :: Language
,scope :: String
}
| Test
{deep :: Bool
,database :: FilePath
,language :: Language
}
deriving (Data,Typeable,Show)
getCmdLine :: [String] -> IO CmdLine
getCmdLine args = do
args <- withArgs args $ cmdArgsRun cmdLineMode
args <- if database args /= "" then return args else do
dir <- getAppUserDataDirectory "hoogle"
let db = dir </> "default-" ++ lower (show $ language args) ++ "- " ++ showVersion version ++ ".hoo"
debdb = "/var/lib/hoogle/databases/default.hoo"
db_exists <- doesFileExist db
case args of
Generate{..} -> return $ args{database=db}
_ | db_exists -> return $ args{database=db}
_ -> return $ args{database=debdb}
args <- case args of
Generate{..} | "all" `elem` include -> do
putStrLn "Warning: 'all' argument is no longer required, and has been ignored."
return $ args{include = delete "all" include}
_ -> return args
return args
defaultGenerate :: CmdLine
defaultGenerate = generate{language=Haskell}
cmdLineMode = cmdArgsMode $ modes [search_ &= auto,generate,server,replay,test]
&= verbosity &= program "hoogle"
&= summary ("Hoogle " ++ showVersion version ++ ", http://hoogle.haskell.org/")
search_ = Search
{color = def &= name "colour" &= help "Use colored output (requires ANSI terminal)"
,link = def &= help "Give URL's for each result"
,numbers = def &= help "Give counter for each result"
,info = def &= help "Give extended information about the first result"
,database = def &= typFile &= help "Name of database to use (use .hoo extension)"
,count = 10 &= name "n" &= help "Maximum number of results to return"
,query = def &= args &= typ "QUERY"
,repeat_ = 1 &= help "Number of times to repeat (for benchmarking)"
,language = enum [x &= explicit &= name (lower $ show x) &= help ("Work with " ++ show x) | x <- [minBound..maxBound]] &= groupname "Language"
,compare_ = def &= help "Type signatures to compare against"
} &= help "Perform a search"
generate = Generate
{download = def &= help "Download all files from the web"
,insecure = def &= help "Allow insecure HTTPS connections"
,include = def &= args &= typ "PACKAGE"
,local_ = def &= opt "" &= help "Index local packages"
,debug = def &= help "Generate debug information"
} &= help "Generate Hoogle databases"
server = Server
{port = 80 &= typ "INT" &= help "Port number"
,cdn = "" &= typ "URL" &= help "URL prefix to use"
,logs = "" &= opt "log.txt" &= typFile &= help "File to log requests to (defaults to stdout)"
,local = False &= help "Allow following file:// links, restricts to 127.0.0.1 Set --host explicitely (including to '*' for any host) to override the localhost-only behaviour"
,scope = def &= help "Default scope to start with"
,host = "" &= help "Set the host to bind on (e.g., an ip address; '!4' for ipv4-only; '!6' for ipv6-only; default: '*' for any host)."
,https = def &= help "Start an https server (use --cert and --key to specify paths to the .pem files)"
,cert = "cert.pem" &= typFile &= help "Path to the certificate pem file (when running an https server)"
,key = "key.pem" &= typFile &= help "Path to the key pem file (when running an https server)"
} &= help "Start a Hoogle server"
replay = Replay
{logs = "log.txt" &= args &= typ "FILE"
} &= help "Replay a log file"
test = Test
{deep = False &= help "Run extra long tests"
} &= help "Run the test suite"