console-program 0.1.0.1 → 0.1.0.2
raw patch · 4 files changed
+39/−14 lines, 4 files
Files
- console-program.cabal +4/−4
- src/System/Console/Action.hs +5/−0
- src/System/Console/Argument.hs +25/−6
- src/System/Console/Options.hs +5/−4
console-program.cabal view
@@ -1,5 +1,5 @@ Name: console-program-Version: 0.1.0.1+Version: 0.1.0.2 Cabal-Version: >= 1.6 License: BSD3 Author: Arie Peterson@@ -9,18 +9,18 @@ Description: This library provides an infrastructure to build command line programs. It provides the following features: .- - declare any number of "actions" (commands, or modes of operation, of the program);+ - declare any number of \"actions\" (commands, or modes of operation, of the program); . - declare options of the program; . - collect options and actions from a configuration file and the command line, and execute the proper action. . .- It provides functionality similar to the "cmdargs" package. Main differences:+ It provides functionality similar to the \"cmdargs\" package. Main differences: . - console-program does not use unsafePerformIO, and tries to give a more haskellish, referentially transparent interface; .- - it allows a full tree of "modes", instead of a list, so a command can have subcommands;+ - it allows a full tree of \"modes\", instead of a list, so a command can have subcommands; . - it parses a configuration file, in addition to the command line arguments. Category: System
src/System/Console/Action.hs view
@@ -21,9 +21,14 @@ readerT :: Reader.ReaderT o IO () -> Action o readerT f = Action (const $ Reader.runReaderT f) +-- | A simple action, taking no argument. simple :: IO () -> Action o simple = Action . const . const +-- | Create an action that takes an argument (non-option).+-- +-- The type of argument is specified by the first parameter; these values can+-- be obtained from the module "System.Console.Argument". withArgument :: Argument.Type x -> (x -> Action o) -> Action o withArgument at f = Action g where g (x : xs) = either
src/System/Console/Argument.hs view
@@ -2,8 +2,7 @@ ( Type (Type,parser,name,defaultValue) - , option- + -- * Argument types , optional , string , boolean@@ -12,6 +11,9 @@ , device , natural , integer+ + -- * Option descriptions+ , option ) where @@ -33,19 +35,31 @@ instance Functor Type where fmap f t = t { parser = ((.) . (.)) (fmap f) (parser t), defaultValue = fmap f (defaultValue t) } -option :: (a -> s) -> [Char] -> [String] -> Type a -> String -> GetOpt.OptDescr (Either String s)+-- | Create an option description.+option+ :: (a -> s) -- ^ Function that creates a setting (of type @s@) from the option argument.+ -> [Char] -- ^ List of short option characters.+ -> [String] -- ^ List of long option strings.+ -> Type a -- ^ Type of option argument.+ -> String -- ^ Description.+ -> GetOpt.OptDescr (Either String s) -- ^ The resulting option description. option inj short long t description = case defaultValue t of Nothing -> GetOpt.Option short long (GetOpt.ReqArg (fmap inj . parser t (name t)) (name t)) description Just a -> GetOpt.Option short long (GetOpt.OptArg (maybe (Right $ inj a) $ fmap inj . parser t (name t)) (name t)) description --- Common argument types+-- Argument types. -optional :: a -> Type a -> Type a+optional+ :: a -- ^ Default value.+ -> Type a+ -> Type a optional x t = t { defaultValue = Just x } +-- | A plain string. string :: Type String string = Type (const Right) "STRING" Nothing +-- | A boolean. Argument can be \"1\",\"0\",\"true\",\"false\",\"on\",\"off\". boolean :: Type Bool boolean = Type {@@ -54,15 +68,18 @@ , defaultValue = Just True } where- m = Map.fromList [("1",True),("0",False),("true",True),("false",False)]+ m = Map.fromList [("1",True),("0",False),("true",True),("false",False),("on",True),("off",False)] e y = Left $ "Argument " ++ show y ++ " could not be recognised as a boolean." +-- | A natural number (in decimal). natural :: Type Integer natural = Type { name = "INT (natural)", parser = const (P.parseM P.natural ""), defaultValue = Nothing } +-- | An integer number (in decimal). integer :: Type Integer integer = Type { name = "INT", parser = const (P.parseM P.integer ""), defaultValue = Nothing } +-- | A directory path. A trailing slash is stripped, if present. directory :: Type FilePath directory = Type { name = "DIR", parser = const (Right . stripTrailingSlash), defaultValue = Nothing } where@@ -72,8 +89,10 @@ | l == '/' -> i | otherwise -> x +-- | A file path. file :: Type FilePath file = string { name = "FILE" } +-- | A device path. device :: Type FilePath device = string { name = "DEVICE" }
src/System/Console/Options.hs view
@@ -16,14 +16,15 @@ import Language.Haskell.TH - -- | An instance @s@ of 'Setting' has as values /partial/ sets of option assignments,- -- as given by the user in a configuration file or command line options.- -- @'Options' s@ is the associated type of complete configurations, as the- -- program needs.+-- | An instance @s@ of 'Setting' has as values /partial/ sets of option assignments,+-- as given by the user in a configuration file or command line options.+-- @'Options' s@ is the associated type of complete configurations, as the+-- program peruses. class Setting s where type Options s set :: s -> Options s -> Options s +-- Necessary to avoid TH staging error. setName = 'set apply :: forall s. (Setting s) => [s] -> Options s -> Options s