+
Skip to content

riinr/loki

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loki

loki: line oriented (k)ommand interpreter

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

Example

import loki, strutils, options

loki(myHandler, input):
  do_greet name:
   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

Compile with something like:

nim c --threads:on cmd.nim

And an example run:

$ ./cmd 
Welcome to my CLI!

(loki) greet
Hello there!

(loki) greet Beshr
Hello Beshr!

(loki) add
Provide two numbers to add them

(loki) add 1 2
Result is 3

About

A small library for writing line-oriented command interpreters in Nim.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Nim 97.1%
  • Makefile 2.9%
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载