From a971bc1040a236b0ae54d65c7e21565a638ffc49 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Fri, 18 Jul 2014 14:42:14 +0100 Subject: Added argument parsing --- blavote.go | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'blavote.go') diff --git a/blavote.go b/blavote.go index 808b7af..f493b1b 100644 --- a/blavote.go +++ b/blavote.go @@ -2,14 +2,46 @@ package main import "fmt" import "github.com/mxk/go-sqlite/sqlite3" +import "github.com/jessevdk/go-flags" +import "os" +import "io/ioutil" +import "strings" var version string func main() { - version = "0.01" + version = "0.02" + + //Command line arguments + var opts struct { + Version bool `short:"v" long:"version" description:"Show program version"` + Add bool `short:"a" long:"add" description:"Add a new poll"` + New bool `short:"n" long:"new" description:"Add a new poll"` + Args struct { + Title string + Rest []string + } `positional-args:"yes"` + } + + var args []string + + //If there are no command line arguments, read them from stdin + if (len(os.Args) > 1) { + flags.Parse(&opts) + } else { + bytes, err := ioutil.ReadAll(os.Stdin) + if (err == nil) { + args = strings.Split(strings.Split(string(bytes),"\n")[0], " ") + flags.ParseArgs(&opts, args) + } else { + fmt.Println(err) + //No args given, deal with it + } - _, err := connectDb("blavote.db") + } + + db, err := connectDb("blavote.db") if (err != nil) { fmt.Print("Could not connect to vote database: ") @@ -17,6 +49,12 @@ func main() { return } + if (opts.Version) { + fmt.Println("v" + version) + } else if (opts.Add || opts.New) { + addPoll(db, opts.Args.Title, opts.Args.Rest) + } + } func connectDb(name string) (*sqlite3.Conn, error) { @@ -51,4 +89,9 @@ func initTables(db *sqlite3.Conn) { db.Exec("create table votes(id int, user_id int, poll_id int, option_id int)") +} + +func addPoll(db *sqlite3.Conn, title string, options []string) { + + } \ No newline at end of file -- cgit v1.2.3