1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-11-22 19:40:04 -05:00
ofxgo/cmd/ofx/main.go

82 lines
1.3 KiB
Go
Raw Normal View History

2017-03-22 20:29:08 -04:00
package main
import (
"fmt"
"os"
"strconv"
)
var commands = []Command{
getAccountsCommand,
downloadCommand,
ccDownloadCommand,
invDownloadCommand,
bankTransactionsCommand,
ccTransactionsCommand,
invTransactionsCommand,
detectSettingsCommand,
2017-03-22 20:29:08 -04:00
}
func usage() {
fmt.Println(`The ofxgo command-line client provides a simple interface to
query, parse, and display financial data via the OFX specification.
Usage:
ofx command [arguments]
The commands are:`)
maxlen := 0
for _, cmd := range commands {
if len(cmd.Name) > maxlen {
maxlen = len(cmd.Name)
}
}
formatString := " %-" + strconv.Itoa(maxlen) + "s %s\n"
for _, cmd := range commands {
fmt.Printf(formatString, cmd.Name, cmd.Description)
}
}
func runCmd(c *Command) {
err := c.Flags.Parse(os.Args[2:])
if err != nil {
fmt.Printf("Error parsing flags: %s\n", err)
c.Usage()
os.Exit(1)
}
if !c.CheckFlags() {
fmt.Println()
c.Usage()
os.Exit(1)
}
c.Do()
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Error: Please supply a sub-command. Usage:\n")
usage()
os.Exit(1)
}
cmdName := os.Args[1]
for _, cmd := range commands {
if cmd.Name == cmdName {
runCmd(&cmd)
os.Exit(0)
}
}
switch cmdName {
case "-h", "-help", "--help", "help":
usage()
default:
fmt.Println("Error: Invalid sub-command. Usage:")
usage()
os.Exit(1)
}
}