Advertisement
Guest User

Untitled

a guest
Nov 8th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.93 KB | None | 0 0
  1. # This is just an example to get you started. A typical binary package
  2. # uses this file as the main entry point of the application.
  3.  
  4. import streams, terminal, os, parseopt, strutils, termios
  5.  
  6. proc writeHelp() =
  7.   echo """
  8. Ness 0.1.0 (Nim less implementation)
  9.  
  10. usage: ness [--file[=<file>]] [--help] [--version] [--verbose]
  11.  
  12. Allowed Arguments:
  13.  -h | --help     : show help
  14.  -v | --version  : show version
  15.  -f | --file     : open file
  16.  
  17.  --verbose       : verbose messages
  18.  """
  19.  
  20. proc writeVersion() =
  21.   echo "Ness version 0.1.0"
  22.  
  23. proc cli*() =
  24.   var
  25.     verbose: bool = false
  26.     input: File
  27.     line = ""
  28.     filename: string
  29.     ss: FileStream
  30.  
  31.   let ispiped = not isatty(stdin)
  32.  
  33.   #If there's no arguments and this is just running as a command, show the help screen
  34.   if paramCount() == 0 and not ispiped:
  35.     writeHelp()
  36.     quit(0)
  37.  
  38.   for kind, key, val in getopt():
  39.     case kind
  40.     of cmdLongOption, cmdShortOption:
  41.       case key
  42.       of "help", "h":
  43.         writeHelp()
  44.         quit()
  45.       of "version", "v":
  46.         writeVersion()
  47.         quit()
  48.       of "verbose":
  49.         verbose = true
  50.       of "file", "f":
  51.         filename = val
  52.         if ispiped:
  53.           echo "Error: Cannot supply both stdin and a file to read"
  54.           quit(0)
  55.  
  56.         if filename.len == 0:
  57.           echo "Error: No file argument supplied"
  58.           quit()
  59.         elif not existsFile(val):
  60.           echo "Error: File does not exist"
  61.           quit()
  62.     else:
  63.       discard
  64.  
  65.  
  66.   if ispiped:
  67.     #discard input.open(0, fmRead)
  68.     ss = newFileStream(stdin)
  69.   else:
  70.     ss = newFileStream(filename, fmRead)
  71.  
  72.   defer: ss.close()
  73.  
  74.   for line in ss.lines:
  75.     echo line
  76.  
  77.   var tty = open("/dev/tty", fmReadWrite)
  78.   tty.write "> "
  79.   tty.flushFile
  80.   var inline = tty.readLine
  81.   tty.close()
  82.  
  83.   echo inline
  84.  
  85.  
  86.   while stdin.readLine(line):
  87.     echo line
  88.  
  89.   ss.close()
  90.  
  91. when isMainModule:
  92.   cli()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement