Skip to content
Snippets Groups Projects
Unverified Commit 923b0c03 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial pass at command parser

parent 923b15ab
No related branches found
No related tags found
No related merge requests found
// Package dclish A DCL-like command line parser.
package dclish
import (
"fmt"
"strings"
)
// ActionFunc is the function that a command runs.
type ActionFunc func(string, *Command) error
// Flag is a flag for a command.
type Flag struct {
Name string
Value string
Description string
}
// Flags is the list of flags.
type Flags []*Flag
// Command contains the definition of a command, it's flags and subcommands.
type Command struct {
Command string
Flags Flags
Args []string
Commands []*Command
Action ActionFunc
Description string
}
// Commands is the full list of commands.
type Commands []*Command
// ParseAndRun parses a command line and runs the command.
func (c Commands) ParseAndRun(line string) error {
// TODO: this doesn't handle a DCL command line completely.
words := strings.Fields(line)
fmt.Printf("TODO ParseAndRun sees: %s\n", words)
cmd := strings.ToUpper(words[0])
for i := range c {
if c[i].Command == cmd {
fmt.Printf("Command help:\n%s\n", c[i].Description)
return nil
}
}
fmt.Printf("ERROR: Unknown command '%s'\n", cmd)
return nil
}
This diff is collapsed.
...@@ -3,7 +3,6 @@ package repl ...@@ -3,7 +3,6 @@ package repl
import ( import (
"fmt" "fmt"
"strings"
"github.com/chzyer/readline" "github.com/chzyer/readline"
) )
...@@ -22,13 +21,12 @@ func Loop(user string) error { ...@@ -22,13 +21,12 @@ func Loop(user string) error {
if err != nil { if err != nil {
return err return err
} }
words := strings.Field(strings.ReplaceAll(line, "/", " /")) if len(line) == 0 {
if len(words) {
continue continue
} }
switch strings.ToUpper(words[0]) { err = commands.ParseAndRun(line)
case "ADD": if err != nil {
commandAdd() return err
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment