tins2020/cmd/imadapt/args.go

73 lines
1.4 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
func atois(s ...string) ([]int, error) {
ints := make([]int, len(s))
for i, s := range s {
value, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("couldn't convert '%s' to a number; error: %v", s, err)
}
ints[i] = value
}
return ints, nil
}
func scanDir(dir string) ([]string, error) {
var files []string
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir || info.IsDir() {
return nil
}
files = append(files, path)
return nil
})
if err != nil {
return nil, err
}
return files, nil
}
type point struct {
x, y int
}
func parsePoint(s string) (point, error) {
components := strings.Split(s, ",")
if len(components) != 2 {
return point{}, errors.New("expected two components separated by a comma")
}
ints, err := atois(components...)
if err != nil {
return point{}, err
}
return point{x: ints[0], y: ints[1]}, nil
}
type rect struct {
min, max point
}
func parseRect(s string) (rect, error) {
components := strings.Split(s, ",")
if len(components) != 4 {
return rect{}, errors.New("expected four components separated by a comma")
}
ints, err := atois(components...)
if err != nil {
return rect{}, err
}
return rect{min: point{x: ints[0], y: ints[1]}, max: point{x: ints[2], y: ints[3]}}, nil
}