Skip to content

Instantly share code, notes, and snippets.

@nasitra
Created January 14, 2024 01:20
Show Gist options
  • Save nasitra/f45a0b61427c89dfaf0121f6ef87c2d5 to your computer and use it in GitHub Desktop.
Save nasitra/f45a0b61427c89dfaf0121f6ef87c2d5 to your computer and use it in GitHub Desktop.
Handle command line input implemented with Go
package main
import (
"fmt"
"os"
"github.com/mattn/go-shellwords"
"github.com/peterh/liner"
)
func handle(args []string) error {
/* TODO */
fmt.Println(args)
return nil
}
func main() {
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
for {
prompt := "> "
l, err := line.Prompt(prompt)
if err != nil {
break
}
line.AppendHistory(l)
args, err := shellwords.Parse(l)
if err != nil {
fmt.Fprintln(os.Stderr, err)
continue
}
if len(args) == 0 {
continue
}
err = handle(args)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment