mirror of
https://github.com/aclindsa/ofxgo.git
synced 2024-11-22 11:30:05 -05:00
81 lines
1.3 KiB
Go
81 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
var commands = []Command{
|
||
|
getAccountsCommand,
|
||
|
downloadCommand,
|
||
|
ccDownloadCommand,
|
||
|
invDownloadCommand,
|
||
|
bankTransactionsCommand,
|
||
|
ccTransactionsCommand,
|
||
|
invTransactionsCommand,
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
}
|