packages feed

htalkat-0.1.1: Opts.hs

-- This file is part of htalkat
-- Copyright (C) 2021 Martin Bays <mbays@sdf.org>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of version 3 of the GNU General Public License as
-- published by the Free Software Foundation, or any later version.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see http://www.gnu.org/licenses/.

{-# LANGUAGE Safe #-}

module Opts (help, parseArgs, Opt(..)) where

import           System.Console.GetOpt

data Opt
    = DataDir FilePath
    | SocksHost String
    | SocksPort String
    | Help
    | Version
    deriving (Eq, Ord, Show)

options :: [OptDescr Opt]
options =
    [ Option ['d'] ["datadir"] (ReqArg DataDir "PATH") "default: ~/.htalkat, or $HTALKAT_DIR if set"
    , Option ['S'] ["socks-host"] (ReqArg SocksHost "HOST") "use SOCKS5 proxy"
    , Option ['P'] ["socks-port"] (ReqArg SocksPort "PORT") "port for SOCKS5 proxy (default: 1080)"
    , Option ['v'] ["version"] (NoArg Version) "show version information"
    , Option ['h'] ["help"] (NoArg Help) "show usage information"
    ]

help :: String -> String
help = (`usageInfo` options)

parseArgs :: [String] -> IO ([Opt],[String])
parseArgs argv =
    case getOpt RequireOrder options argv of
        (o,n,[])   -> return (o,n)
        (_,_,errs) -> ioError . userError $ concat errs