Add a little clarity

This commit is contained in:
Neale Pickett 2020-09-16 17:54:29 -06:00
parent 7f326ed90b
commit 0a60a649a1
3 changed files with 23 additions and 8 deletions

View File

@ -32,17 +32,21 @@ func nothing() error {
return nil
}
func usage(w io.Writer) {
fmt.Fprintln(w, "Usage: transpile COMMAND [flags]")
fmt.Fprintln(w, "")
fmt.Fprintln(w, " mothball: Compile a mothball")
fmt.Fprintln(w, " inventory: Show category inventory")
fmt.Fprintln(w, " open: Open a file for a puzzle")
fmt.Fprintln(w, " answer: Check correctness of an answer")
}
// ParseArgs parses arguments and runs the appropriate action.
func (t *T) ParseArgs() (Command, error) {
var cmd Command
if len(t.Args) == 1 {
fmt.Fprintln(t.Stderr, "Usage: transpile COMMAND [flags]")
fmt.Fprintln(t.Stderr, "")
fmt.Fprintln(t.Stderr, " mothball: Compile a mothball")
fmt.Fprintln(t.Stderr, " inventory: Show category inventory")
fmt.Fprintln(t.Stderr, " open: Open a file for a puzzle")
fmt.Fprintln(t.Stderr, " answer: Check correctness of an answer")
usage(t.Stderr)
return nothing, nil
}
@ -60,7 +64,11 @@ func (t *T) ParseArgs() (Command, error) {
case "answer":
flags.StringVar(&t.answer, "answer", "", "Answer to check")
cmd = t.CheckAnswer
case "help":
usage(t.Stderr)
return nothing, nil
default:
usage(t.Stderr)
return nothing, fmt.Errorf("%s is not a valid command", t.Args[1])
}
@ -69,6 +77,7 @@ func (t *T) ParseArgs() (Command, error) {
return nothing, err
}
if *directory != "" {
log.Println(*directory)
t.fs = afero.NewBasePathFs(t.BaseFs, *directory)
} else {
t.fs = t.BaseFs
@ -101,6 +110,7 @@ func (t *T) PrintInventory() error {
}
// DumpFile writes a file to the writer.
// BUG(neale): The "open" and "answer" actions don't work on categories with an "mkcategory" executable.
func (t *T) DumpFile() error {
puzzle := transpile.NewFsPuzzle(t.fs)
@ -160,6 +170,7 @@ func main() {
Stdout: os.Stdout,
Stderr: os.Stderr,
Args: os.Args,
BaseFs: afero.NewOsFs(),
}
cmd, err := t.ParseArgs()
if err != nil {

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os/exec"
"path"
@ -134,7 +135,9 @@ func (c FsCommandCategory) run(command string, args ...string) ([]byte, error) {
// Inventory returns a list of point values for this category.
func (c FsCommandCategory) Inventory() ([]int, error) {
stdout, err := c.run("inventory")
if err != nil {
if exerr, ok := err.(*exec.ExitError); ok {
return nil, fmt.Errorf("inventory: %s: %s", err, string(exerr.Stderr))
} else if err != nil {
return nil, err
}

View File

@ -29,7 +29,8 @@ func FsInventory(fs afero.Fs) (Inventory, error) {
c := NewFsCategory(fs, name)
puzzles, err := c.Inventory()
if err != nil {
return nil, err
log.Printf("Inventory: %s: %s", name, err)
continue
}
sort.Ints(puzzles)
inv[name] = puzzles