cmd-shell/cmd_shell.nim

38 lines
1.1 KiB
Nim

import cliutils, cstrutils, docopt, linenoise, os, osproc, sequtils, strutils
import "./version.nim"
proc writeOutput(msg, errMsg: TaintedString, cmd: string): void =
if not msg.isEmptyOrWhitespace: stdout.writeLine(msg)
if not errMsg.isEmptyOrWhitespace: stderr.writeLine(errMsg)
when isMainModule:
let usage = """
Usage:
cmd_shell [--prepend-args] <cmd> [-- <args>...]
"""
# Parse arguments
let args = docopt(usage, version = CMD_SHELL_VERSION)
let cmdPrompt = $args["<cmd>"]
let cmd = '"' & cmdPrompt & '"'
let argsForCmd =
if args["<args>"]: @(args["<args>"]).mapIt("'" & $it & "'").join(" ")
else: ""
discard historySetMaxLen(
if existsEnv("HISTSIZE"): (cint)parseInt(getEnv("HISTSIZE"))
else: (cint)1000)
var line = readLine(cmdPrompt & "> ")
while line != nil and cmpIgnoreCase(line, "exit") != 0:
if args["--prepend-args"]:
discard exec(cmd & " " & argsForCmd & " " & $line, "", [], nil, {poUsePath, poEvalCommand}, writeOutput)
else:
discard exec(cmd & " " & $line & " " & argsForCmd, "", [], nil, {poUsePath, poEvalCommand}, writeOutput)
historyAdd(line)
line = readLine(cmdPrompt & "> ")