gexpect

Pure golang expect library, for easily starting and controlling subprocesses

Github星跟蹤圖

Gexpect

Gexpect is a pure golang expect-like module.

It makes it simpler to create and control other terminal applications.

child, err := gexpect.Spawn("python")
if err != nil {
	panic(err)
}
child.Expect(">>>")
child.SendLine("print 'Hello World'")
child.Interact()
child.Close()

Examples

Spawn handles the argument parsing from a string

child.Spawn("/bin/sh -c 'echo \"my complicated command\", tee log, cat > log2'")
child.ReadLine() // ReadLine() (string, error)
child.ReadUntil(' ') // ReadUntil(delim byte) ([]byte, error)

ReadLine, ReadUntil and SendLine send strings from/to stdout/stdin respectively

child, _ := gexpect.Spawn("cat")
child.SendLine("echoing process_stdin") //  SendLine(command string) (error)
msg, _ := child.ReadLine() // msg = echoing process_stdin

Wait and Close allow for graceful and ungraceful termination.

child.Wait() // Waits until the child terminates naturally.
child.Close() // Sends a kill command

AsyncInteractChannels spawns two go routines to pipe into and from stdout/stdin, allowing for some usecases to be a little simpler.

child, _ := gexpect.Spawn("sh")
sender, receiver := child.AsyncInteractChannels()
sender <- "echo Hello World\n" // Send to stdin
line, open := <- receiver // Recieve a line from stdout/stderr
// When the subprocess stops (e.g. with child.Close()) , receiver is closed
if open {
	fmt.Printf("Received %s", line)
}

ExpectRegex uses golang's internal regex engine to wait until a match from the process with the given regular expression (or an error on process termination with no match).

child, _ := gexpect.Spawn("echo accb")	
match, _ := child.ExpectRegex("a..b")
// (match=true)

ExpectRegexFind allows for groups to be extracted from process stdout. The first element is an array of containing the total matched text, followed by each subexpression group match.

child, _ := gexpect.Spawn("echo 123 456 789")
result, _ := child.ExpectRegexFind("\d+ (\d+) (\d+)")
// result = []string{"123 456 789", "456", "789"}

See gexpect_test.go and the examples folder for full syntax

Credits

github.com/kballard/go-shellquote	
github.com/kr/pty
KMP Algorithm: "http://blog.databigbang.com/searching-for-substrings-in-streams-a-slight-modification-of-the-knuth-morris-pratt-algorithm-in-haxe/"

主要指標

概覽
名稱與所有者ThomasRooney/gexpect
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2014-01-20 19:16:22
推送於2018-06-23 17:08:12
最后一次提交2017-01-01 01:01:23
發布數0
用户参与
星數269
關注者數10
派生數79
提交數73
已啟用問題?
問題數18
打開的問題數12
拉請求數13
打開的拉請求數3
關閉的拉請求數4
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?