structured-cli

Application library for building interactive console CLIs

https://gitlab.com/codemonkeylabs/structured-cli#readme

Version on this page:2.5.2.0
LTS Haskell 21.25:2.7.0.1
Stackage Nightly 2023-06-21:2.7.0.1
Latest on Hackage:2.7.0.1

See all snapshots structured-cli appears in

BSD-3-Clause licensed by Erick Gonzalez
Maintained by [email protected]
This version can be pinned in stack with:structured-cli-2.5.2.0@sha256:3eef76b3a27440ad274ddcdcc0ea4e95311f68226e528564621ee9ce7c0fcaaf,1972

Module documentation for 2.5.2.0

structured-cli

Haskell library for building structured CLI applications

This module provides the tools to build a complete “structured” CLI application, similar to those found in systems like Cisco IOS or console configuration utilities etc. It aims to be easy for implementors to use.

  • How to use this module:

It is often the case that a simple example is the best user guide, at least for the experienced programmer. The following code illustrates a basic but functioning CLI application:

module Main where

import Control.Monad                 (void)
import Control.Monad.IO.Class        (liftIO)
import Data.Default                  (def)
import System.Console.StructuredCLI

root :: Commands ()
root = do
  world >+ do
    hello
    bye
    command "exit" "return to previous level" exit

world :: Commands ()
world = command "world" "enter into the world" $ return NewLevel

hello :: Commands ()
hello = command "hello" "prints a greeting" $ do
          liftIO . putStrLn $ "Hello world!"
          return NoAction

bye :: Commands ()
bye = command "bye" "say goodbye" $ do
        liftIO . putStrLn $ "Sayonara!"
        return NoAction

main :: IO ()
main = void $ runCLI "Hello CLI" def root

resulting example session:

>>> Hello CLI > ?
- world: enter into the world

>>> Hello CLI > world
>>> Hello CLI world > ?
- exit: return to previous level
- bye: say goodbye
- hello: prints a greeting

>>> Hello CLI world > hello
Hello world!
>>> Hello CLI world > bye
Sayonara!
>>> Hello CLI world > exit
>>> Hello CLI >

A good way to get you started is to grab the example code available under example/Main.hs and modify it to suit your needs.