cli-0.0.4: Simple Command Line Interface Library

LicenseBSD-Style
Safe HaskellSafe-Inferred
LanguageHaskell2010

Application.CLI

Contents

Description

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"

Synopsis

Default Main

defaultMain Source

Arguments

:: CLIContext

A collection of commands

-> IO () 

The default Main It will analyze the program's parameters and will trigger the right command.

CLI

class CLI cli where Source

This is the CLI Class All command you want to add into the CLIContext must be an instance of this class.

Methods

name :: cli -> String Source

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

desc :: cli -> String Source

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.

action Source

Arguments

:: 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

Instances

data OptHelp Source

Option help

It describes the Command parameters

Constructors

OptHelp 

Fields

optSymbols :: [String]

an option can have multiple symbols, mainly a short option and a long option for example [ "-h", "--help" ]

optArgument :: Maybe String

an option can have an argument, or not

optDescription :: String

option description

Instances

Default CLIs

data Help Source

This is the default help command

It is triggered by the command "help" and also provides an option to print a specific command options

Constructors

Help 

Instances

printHelp Source

Arguments

:: 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

initialize Source

Arguments

:: String

CLI Description

-> CLIContext 

Initialize a collection of command

initializeWithDefault Source

Arguments

:: CLI cliDefault 
=> cliDefault

The command to execute if no option is given

-> String

CLI Description

-> CLIContext 

Initialize a collection of commands

with Source

Arguments

:: CLI cli 
=> cli

A new Command Interface

-> CLIContext

The original Collection of commands

-> CLIContext 

Add a new Command into a collection of commands

Options

type Options = [String] Source

The Command Options parameter

withStr Source

Arguments

:: String

a description of what is expected

-> (String -> Options -> IO a) 
-> Options 
-> IO a 

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)

withParameterStr Source

Arguments

:: [String]

the reference flag

-> (String -> Options -> IO a) 
-> Options 
-> IO a 

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

Arguments

:: [String]

the reference flag

-> (Maybe String -> Options -> IO a) 
-> Options 
-> IO a 

idem as withParameterStr but it is an optional parameter

withFlag Source

Arguments

:: [String]

the flag to lookup

-> (Bool -> Options -> IO a)

the function to execute

-> Options

the options

-> IO a 

This function is to expect Flag in the Options it will look into all the options and will remove the string from the Options

execute :: IO a -> Options -> IO a Source

This function makes the use of the other function easier and also provide a verification that there is no "unused function". If there is unexpected options remaining, the program will stop and an error message will be printed