{-# LANGUAGE TemplateHaskell #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}{-# LANGUAGE Rank2Types #-}{-# OPTIONS_HADDOCK show-extensions #-}-- |-- Module : Yi.Option-- License : GPL-2-- Maintainer : yi-devel@googlegroups.com-- Stability : experimental-- Portability : portable---- Command-line optionsmodule Yi.Option ( -- * Types YiOption , YiOptionDescr , YiOptions , OptionError(..) -- * Core , yiCustomOptions , consYiOption , consYiOptions -- * Argument-less options , yiBoolOption , yiFlagOption , yiActionFlagOption -- * Argument-taking options , yiStringOption , yiStringOption' , yiActionOption , yiActionOption' )whereimport Data.Default (Default)import qualified Data.Text as T (Text)import Data.Typeable (Typeable)import Data.String (IsString, fromString)import Lens.Micro.Platform (Lens', makeLenses, over, set)import System.Exit (ExitCode)import System.Console.GetOpt (OptDescr, ArgDescr(..))import Yi.Config.Lens (configVariable, startActionsA)import Yi.Types (Action, Config, YiConfigVariable)data OptionError = OptionError T.Text ExitCode-- | An option is a function that attempts to change the configuration of the-- editor at runtime.type YiOption = Config -> Either OptionError Configtype YiOptionDescr = OptDescr YiOption-- | Custom options that should be accepted. Provided in user configuration.---- The general flow is that the user adds options to his configuration. Options-- are essentially functions describing how to modify the configuration at runtime.-- When an option is called, it gets the current config and may modify it (to encode-- its value)newtype YiOptions = YiOptions { _yiOptions :: [YiOptionDescr] } deriving (Default, Typeable)instance YiConfigVariable YiOptionsmakeLenses ''YiOptions-- | Lens for accessing the list of custom options.---- You can pretty much create whatever types of options you want with this.-- But most cases are taken care of by one of the helper functions in this module.yiCustomOptions :: Lens' Config [YiOptionDescr]yiCustomOptions = configVariable . yiOptions-- | Includes an extra option in the configuration. Small wrapper around 'yiCustomOptions'consYiOption :: YiOptionDescr -> Config -> ConfigconsYiOption opt = over yiCustomOptions (opt:)-- | Like 'consYiOption' but supports multiple options. Convenient for keymaps which might-- want to install lots of options.consYiOptions :: [YiOptionDescr] -> Config -> ConfigconsYiOptions opts = over yiCustomOptions (opts++)-- | An argument which sets some configuration value to 'True'.yiBoolOption :: Lens' Config Bool -> ArgDescr YiOptionyiBoolOption lens = NoArg $ Right . set lens True-- | An argument which applies a function transforming some inner value of-- the configuration.yiFlagOption :: Lens' Config a -> (a -> a) -> ArgDescr YiOptionyiFlagOption lens f = NoArg $ Right . over lens f-- | Flag that appends an action to the startup actions.yiActionFlagOption :: Action -> ArgDescr YiOptionyiActionFlagOption action = NoArg f where f config = Right $ over startActionsA (++[action]) config -- | Sets the value of an option which is any string type (hopefully text...)---- This is not meant to be fully applied. By only passing in the lens you-- will obtain a value suitable for use in OptDescr.yiStringOption :: IsString a => Lens' Config a -> String -> ArgDescr YiOptionyiStringOption lens desc = ReqArg f desc where f string config = Right $ set lens (fromString string) config-- | Just like 'yiStringOption', except it applies a 'Just'. Useful for setting-- string-like values whose default is 'None'.yiStringOption' :: IsString a => Lens' Config (Maybe a) -> String -> ArgDescr YiOptionyiStringOption' lens desc = ReqArg f desc where f string config = Right $ set lens (Just $ fromString string) config-- | Option that appends a parameterized action to the startup actions.yiActionOption :: IsString a => (a -> Action) -> String -> ArgDescr YiOptionyiActionOption action desc = ReqArg f desc where f string config = Right $ over startActionsA (++[action (fromString string)]) configyiActionOption' :: IsString a => (a -> Either OptionError Action) -> String -> ArgDescr YiOptionyiActionOption' action desc = ReqArg f desc where f string config = do action' <- action (fromString string) return $ over startActionsA (++[action']) config