Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This is just an example to get you started. A typical binary package
- # uses this file as the main entry point of the application.
- import streams, terminal, os, parseopt, strutils, termios
- proc writeHelp() =
- echo """
- Ness 0.1.0 (Nim less implementation)
- usage: ness [--file[=<file>]] [--help] [--version] [--verbose]
- Allowed Arguments:
- -h | --help : show help
- -v | --version : show version
- -f | --file : open file
- --verbose : verbose messages
- """
- proc writeVersion() =
- echo "Ness version 0.1.0"
- proc cli*() =
- var
- verbose: bool = false
- input: File
- line = ""
- filename: string
- ss: FileStream
- let ispiped = not isatty(stdin)
- #If there's no arguments and this is just running as a command, show the help screen
- if paramCount() == 0 and not ispiped:
- writeHelp()
- quit(0)
- for kind, key, val in getopt():
- case kind
- of cmdLongOption, cmdShortOption:
- case key
- of "help", "h":
- writeHelp()
- quit()
- of "version", "v":
- writeVersion()
- quit()
- of "verbose":
- verbose = true
- of "file", "f":
- filename = val
- if ispiped:
- echo "Error: Cannot supply both stdin and a file to read"
- quit(0)
- if filename.len == 0:
- echo "Error: No file argument supplied"
- quit()
- elif not existsFile(val):
- echo "Error: File does not exist"
- quit()
- else:
- discard
- if ispiped:
- #discard input.open(0, fmRead)
- ss = newFileStream(stdin)
- else:
- ss = newFileStream(filename, fmRead)
- defer: ss.close()
- for line in ss.lines:
- echo line
- var tty = open("/dev/tty", fmReadWrite)
- tty.write "> "
- tty.flushFile
- var inline = tty.readLine
- tty.close()
- echo inline
- while stdin.readLine(line):
- echo line
- ss.close()
- when isMainModule:
- cli()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement