Giter VIP home page Giter VIP logo

gexpect's Introduction

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/"

gexpect's People

Contributors

thomasrooney avatar steveej avatar krnowak avatar heyitsanthony avatar iaguis avatar alban avatar samhug avatar jonboulle avatar lucab avatar ppalucki avatar svendowideit avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.