AAI (empty) → 0.1.0.0
raw patch · 6 files changed
+130/−0 lines, 6 filesdep +basesetup-changed
Dependencies added: base
Files
- AAI.cabal +33/−0
- LICENSE +20/−0
- README.md +4/−0
- Setup.hs +2/−0
- src/Control/Command.hs +25/−0
- src/Control/Router.hs +46/−0
+ AAI.cabal view
@@ -0,0 +1,33 @@+name: AAI+version: 0.1.0.0+synopsis: Abstract Application Interface.+description: The Abstract Application Interface is used to define a+ generic interface for handling command line parameter+ to command mapping. It may route a command by giving a+ simple parameter list and a list of supported commands.+license: MIT+license-file: LICENSE+author: Nils 'bash0r' Jonsson+maintainer: aka.bash0r@gmail.com+copyright: (c) 2015 Nils 'bash0r' Jonsson+category: Control+build-type: Simple+extra-source-files: README.md+ , LICENSE+cabal-version: >=1.10++source-repository head+ type: git+ location: https://github.com/aka-bash0r/AAI++source-repository this+ type: git+ location: https://github.com/aka-bash0r/AAI+ tag: v0.1.0.0++library+ exposed-modules: Control.Command+ , Control.Router+ build-depends: base >=4.8 && <4.9+ hs-source-dirs: src+ default-language: Haskell2010
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Nils 'bash0r' Jonsson++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ README.md view
@@ -0,0 +1,4 @@+# AAI+The Abstract Application Interface may be used to route several commands via a+command line argument list.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Command.hs view
@@ -0,0 +1,25 @@+{- |+Module : $Header$+Description : A command interface.+Author : Nils 'bash0r' Jonsson+Copyright : (c) 2015 Nils 'bash0r' Jonsson+License : MIT++Maintainer : aka.bash0r@gmail.com+Stability : unstable+Portability : non-portable (Portability is untested.)++A command interface.+-}+module Control.Command+( Command (..)+) where+++-- | A type class describing a basic command.+class Command a where+ -- | Execute a command.+ execute :: a -> [String] -> IO ()+ -- | Execute the help of a command.+ help :: a -> [String] -> IO ()+
+ src/Control/Router.hs view
@@ -0,0 +1,46 @@+{- |+Module : $Header$+Description : A simple type class for routing arguments to commands.+Author : Nils 'bash0r' Jonsson+Copyright : (c) 2015 Nils 'bash0r' Jonsson+License : MIT++Maintainer : aka.bash0r@gmail.com+Stability : unstable+Portability : non-portable (Portability is untested.)++A simple type class implementing a simple routing table for commands.+-}+module Control.Router+( Router (..)+, ErrorHandler+, route+) where++import Control.Applicative+import Control.Command+import Control.Monad++-- | A type class describing basing routing table.+class (Command a) => Router a where+ -- | Checks if a command is routable to a specific command name.+ routes :: a -> String -> Bool++-- | A simple error handling function.+type ErrorHandler = String -> IO ()++-- | Route a specific instruction to a command.+route :: (Command a, Router a) => ErrorHandler -> [a] -> [String] -> IO ()+route eh cmds [] = eh "Empty argument list. Cannot route command."+route eh cmds (cmd:args) =+ case findCommand cmd cmds of+ Just a -> execute a args+ Nothing -> eh ("Command " ++ cmd ++ " is unknown.")+ where+ findCommand _ [] =+ empty+ findCommand cmd (x:xs) =+ if routes x cmd+ then return x+ else findCommand cmd xs+