beef331 / website

Code for the official Nim programming language website
https://nim-lang.org
18 stars 1 forks source link

Loki #18

Closed beshrkayali closed 3 years ago

beshrkayali commented 3 years ago

Name: Loki

Author: Beshr Kayali

Posting: Loki is a small library for writing line-oriented command interpreters (or cli programs) in Nim that is inspired by Python's cmd lib.

Version 0.3 is released this month, and it adds the functionality of automatically generating a TOC and "help" command.

Quick example:

import loki, strutils, options
from sequtils import zip

loki(myHandler, input):
  do_greet name:
   ## Get a nice greeting!
   if isSome(name):
    echo("Hello ", name.get, "!")
   else:
    echo("Hello there!")
  do_add num1, num2:
    if isSome(num1) and isSome(num2):
      echo("Result is ", parseInt(num1.get) + parseInt(num2.get))
    else:
      echo("Provide two numbers to add them")
  do_EOF:
    write(stdout, "Bye!\n")
    return true
  default:
    write(stdout, "*** Unknown syntax: ", input.text , " ***\n")

let myCmd = newLoki(
  handler=myHandler,
  intro="Welcome to my CLI!\n",
)

myCmd.cmdLoop

And an example run: https://asciinema.org/a/iMA7pIq2f7sy8X44pkCPhNmOt