Unverified Commit 923b0c03 authored by Kevin Lyda's avatar Kevin Lyda
Browse files

Initial pass at command parser

parent 923b15ab
Loading
Loading
Loading
Loading

dclish/dclish.go

0 → 100644
+49 −0
Original line number Diff line number Diff line
// 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
}
+2512 −263

File changed.

Preview size limit exceeded, changes collapsed.

+4 −6
Original line number Diff line number Diff line
@@ -3,7 +3,6 @@ package repl

import (
	"fmt"
	"strings"

	"github.com/chzyer/readline"
)
@@ -22,13 +21,12 @@ func Loop(user string) error {
		if err != nil {
			return err
		}
		words := strings.Field(strings.ReplaceAll(line, "/", " /"))
		if len(words) {
		if len(line) == 0 {
			continue
		}
		switch strings.ToUpper(words[0]) {
		case "ADD":
			commandAdd()
		err = commands.ParseAndRun(line)
		if err != nil {
			return err
		}
	}