Implements helper to split command from arguments

Also tests the helper function (ofc...)
This commit is contained in:
bcleenders 2014-12-29 00:00:31 +01:00
parent 18da497f3f
commit 295a379ed7
2 changed files with 99 additions and 14 deletions

View file

@ -118,29 +118,28 @@ func Repl(h *graph.Handle, queryLanguage string, cfg *config.Config) error {
}
if code == "" {
switch {
case strings.HasPrefix(line, ":debug"):
command, arguments := splitLine(line)
switch command {
case ":debug":
ses.ToggleDebug()
fmt.Println("Debug Toggled")
continue
case strings.HasPrefix(line, ":a "):
quad, err := cquads.Parse(line[3:])
if !quad.IsValid() {
if err != nil {
fmt.Printf("not a valid quad: %v\n", err)
}
case ":a":
quad, err := cquads.Parse(arguments)
if err != nil {
fmt.Printf("Error: not a valid quad: %v\n", err)
continue
}
h.QuadWriter.AddQuad(quad)
continue
case strings.HasPrefix(line, ":d "):
quad, err := cquads.Parse(line[3:])
if !quad.IsValid() {
if err != nil {
fmt.Printf("not a valid quad: %v\n", err)
}
case ":d":
quad, err := cquads.Parse(arguments)
if err != nil {
fmt.Printf("Error: not a valid quad: %v\n", err)
continue
}
h.QuadWriter.RemoveQuad(quad)
@ -163,6 +162,26 @@ func Repl(h *graph.Handle, queryLanguage string, cfg *config.Config) error {
}
}
// Splits a line into a command and its arguments
// e.g. ":a b c d ." will be split into ":a" and " b c d ."
func splitLine(line string) (string, string) {
var command, arguments string
line = strings.TrimSpace(line)
// An empty line/a line consisting of whitespace contains neither command nor arguments
if len(line) > 0 {
command = strings.Fields(line)[0]
// A line containing only a command has no arguments
if len(line) > len(command) {
arguments = line[len(command):]
}
}
return command, arguments
}
func terminal(path string) (*liner.State, error) {
term := liner.NewLiner()