How to resolve the algorithm Execute a system command step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Execute a system command step by step in the Wren programming language
Table of Contents
Problem Statement
Run either the ls system command (dir on Windows), or the pause system command.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Execute a system command step by step in the Wren programming language
Source code in the wren programming language
/* Execute_a_system_command.wren */
class Command {
foreign static exec(name, param) // the code for this is provided by Go
}
Command.exec("ls", "-lt")
System.print()
Command.exec("dir", "")
/* Execute_a_system_command.go*/
package main
import (
wren "github.com/crazyinfin8/WrenGo"
"log"
"os"
"os/exec"
)
type any = interface{}
func execCommand(vm *wren.VM, parameters []any) (any, error) {
name := parameters[1].(string)
param := parameters[2].(string)
var cmd *exec.Cmd
if param != "" {
cmd = exec.Command(name, param)
} else {
cmd = exec.Command(name)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
return nil, nil
}
func main() {
vm := wren.NewVM()
fileName := "Execute_a_system_command.wren"
methodMap := wren.MethodMap{"static exec(_,_)": execCommand}
classMap := wren.ClassMap{"Command": wren.NewClass(nil, nil, methodMap)}
module := wren.NewModule(classMap)
vm.SetModule(fileName, module)
vm.InterpretFile(fileName)
vm.Free()
}
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the Haskell programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the MontiLang programming language
You may also check:How to resolve the algorithm Median filter step by step in the Racket programming language
You may also check:How to resolve the algorithm Array length step by step in the Picat programming language