License | BSD-Style |
---|---|
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Copyright : Copyright © 2014 Nicolas DI PRIMA
Maintainer : Nicolas DI PRIMA nicolas@di-prima.fr Stability : experimental Portability : unknown
This module aimes to provide an easy and low-dependencies Command Line configuration options.
You can create a new command for your program by creating an object And creating an instance of CLI of this object
data MyEchoCommand = MyEchoCommand instance CLI MyCommand where name _ = "cmd" desc _ = "just echo the given arguments" options _ = [ OptHelp [] (Just "string") "the message to print" ] action _ _ = withStr "string" $ str -> exectute $ putStrLn str
And to create you application, you need 2 functions (initialize, with and defaultMain).
* initialize
: only create the default CLIContext with a default Help command
and a default Usage function
* with
: insert the Command into the CLIContext
* defaultMain
: will trigger the right command
main :: IO () main = defaultMain $ with MyEchoCommand $ initialize "a message header in case the help command is triggered"
- defaultMain :: CLIContext -> IO ()
- class CLI cli where
- data OptHelp = OptHelp {
- optSymbols :: [String]
- optArgument :: Maybe String
- optDescription :: String
- data Help = Help
- printHelp :: String -> String -> CLIContext -> String
- data CLIContext
- getHeader :: CLIContext -> String
- initialize :: String -> CLIContext
- initializeWithDefault :: CLI cliDefault => cliDefault -> String -> CLIContext
- with :: CLI cli => cli -> CLIContext -> CLIContext
- type Options = [String]
- withStr :: String -> (String -> Options -> IO a) -> Options -> IO a
- withOptionalStr :: (Maybe String -> Options -> IO a) -> Options -> IO a
- withParameterStr :: [String] -> (String -> Options -> IO a) -> Options -> IO a
- withOptionalParameterStr :: [String] -> (Maybe String -> Options -> IO a) -> Options -> IO a
- withFlag :: [String] -> (Bool -> Options -> IO a) -> Options -> IO a
- execute :: IO a -> Options -> IO a
Default Main
:: CLIContext | A collection of commands |
-> IO () |
The default Main It will analyze the program's parameters and will trigger the right command.
CLI
This is the CLI Class All command you want to add into the CLIContext must be an instance of this class.
This is the name of the command. It is also the command line option the action will be triggered every time this string is found to be the first argument of the program
A description of this command Will be use in the default Usage and the Help function
options :: cli -> [OptHelp] Source
The list of Options It is only for printing, the parser won't use this to parse the command parameters.
:: cli | |
-> CLIContext | This could be use in order to know all the different available commands etc... |
-> Options | The Command Parameters |
-> IO () |
The action to perfom every time the name
is found to be the first
string of the program parameters
Option help
It describes the Command parameters
OptHelp | |
|
Default CLIs
This is the default help command
It is triggered by the command "help" and also provides an option to print a specific command options
:: String | indentation |
-> String | program name |
-> CLIContext | command list |
-> String |
Print all the Command's Option help
Commands
data CLIContext Source
This is the Command Line Context This object embeds: * the commands to trigger * a default command in case no command is given * a program description * a Helper Command: the command to trigger in case of a wrong command (example: Usage)
getHeader :: CLIContext -> String Source
Program's description
:: CLI cliDefault | |
=> cliDefault | The command to execute if no option is given |
-> String | CLI Description |
-> CLIContext |
Initialize a collection of commands
:: CLI cli | |
=> cli | A new Command Interface |
-> CLIContext | The original Collection of commands |
-> CLIContext |
Add a new Command into a collection of commands
Options
This function is to expect an argument at the Head of the Options
if the command is not found at the head, the program fails and the description
message is printed
withOptionalStr :: (Maybe String -> Options -> IO a) -> Options -> IO a Source
This function is to expect an optional argument
of the command is not found at the Head of the Options
the function will be executed with Nothing, otherwise it will be executed
with (Just String
)
Look for a parameterized option The given list of flags are the potential list of flags that can be use to find the position of the options
This parameter can be found anywhere in the Options list and the argument of the function will be the string following one of this Flags The parameter and its value are removed from the options before being use into the function.
withOptionalParameterStr Source
idem as withParameterStr
but it is an optional parameter