all: updates for new gonuts/commander API

This commit is contained in:
Sebastien Binet 2013-12-19 15:11:03 +01:00
parent b58a64c48d
commit c737f264a3
3 changed files with 21 additions and 22 deletions

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"github.com/gonuts/commander" "github.com/gonuts/commander"
@ -24,23 +24,24 @@ func init() {
cmdDisasm.Flag.UintVar(&disasmAddress, "a", 0, "The starting memory address.") cmdDisasm.Flag.UintVar(&disasmAddress, "a", 0, "The starting memory address.")
} }
func runDisasm(cmd *commander.Command, args []string) { func runDisasm(cmd *commander.Command, args []string) error {
if len(args) != 1 { if len(args) != 1 {
cmd.Usage() cmd.Usage()
return return nil
} }
bytes, err := ioutil.ReadFile(args[0]) bytes, err := ioutil.ReadFile(args[0])
if err != nil { if err != nil {
log.Fatal(err) return nil
} }
if len(bytes) > 0x10000 { if len(bytes) > 0x10000 {
log.Fatalf("File %s is %04X bytes long, which is more than $10000.", args[0], len(bytes)) return fmt.Errorf("File %s is %04X bytes long, which is more than $10000.", args[0], len(bytes))
} }
if int(disasmAddress)+len(bytes) > 0x10000 { if int(disasmAddress)+len(bytes) > 0x10000 {
log.Fatalf("Starting address ($%04X) + file length ($%04X) = $%X, which is > $10000", return fmt.Errorf("Starting address ($%04X) + file length ($%04X) = $%X, which is > $10000",
disasmAddress, len(bytes), int(disasmAddress)+len(bytes)) disasmAddress, len(bytes), int(disasmAddress)+len(bytes))
} }
asm.DisasmBlock(bytes, uint16(disasmAddress), os.Stdout) asm.DisasmBlock(bytes, uint16(disasmAddress), os.Stdout)
return nil
} }

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"log" "fmt"
"github.com/gonuts/commander" "github.com/gonuts/commander"
"github.com/zellyn/goapple2/disk" "github.com/zellyn/goapple2/disk"
@ -22,19 +22,21 @@ func init() {
cmdDiskConvert.Flag.UintVar(&diskVolume, "v", 0, "The volume of the disk, or 0 for default.") cmdDiskConvert.Flag.UintVar(&diskVolume, "v", 0, "The volume of the disk, or 0 for default.")
} }
func runDiskConvert(cmd *commander.Command, args []string) { func runDiskConvert(cmd *commander.Command, args []string) error {
if len(args) != 2 { if len(args) != 2 {
cmd.Usage() cmd.Usage()
return return nil
} }
if diskVolume > 254 { if diskVolume > 254 {
log.Fatalf("disk volume must be 0-254, got %d", diskVolume) return fmt.Errorf("disk volume must be 0-254, got %d", diskVolume)
} }
nyb, err := disk.DiskFromFile(args[0], byte(diskVolume)) nyb, err := disk.DiskFromFile(args[0], byte(diskVolume))
if err != nil { if err != nil {
log.Fatal(err) return err
} }
if err = disk.DiskToFile(args[1], nyb); err != nil { if err = disk.DiskToFile(args[1], nyb); err != nil {
log.Fatal(err) return err
} }
return err
} }

View File

@ -8,25 +8,21 @@ import (
"github.com/gonuts/flag" "github.com/gonuts/flag"
) )
var a2cmd *commander.Commander var a2cmd *commander.Command
func init() { func init() {
a2cmd = &commander.Commander{ a2cmd = &commander.Command{
Name: os.Args[0], UsageLine: "a2",
Commands: []*commander.Command{ Subcommands: []*commander.Command{
cmdDisasm, cmdDisasm,
cmdDiskConvert, cmdDiskConvert,
}, },
Flag: flag.NewFlagSet("a2", flag.ExitOnError), Flag: *flag.NewFlagSet("a2", flag.ExitOnError),
} }
} }
func main() { func main() {
if err := a2cmd.Flag.Parse(os.Args[1:]); err != nil { if err := a2cmd.Dispatch(os.Args[1:]); err != nil {
log.Fatal(err)
}
args := a2cmd.Flag.Args()
if err := a2cmd.Run(args); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }