summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Robinson <joe@mumsnet.com>2014-07-18 14:42:14 +0100
committerJoe Robinson <joe@mumsnet.com>2014-07-18 14:42:14 +0100
commita971bc1040a236b0ae54d65c7e21565a638ffc49 (patch)
tree8d3150abcef4d04ebe5e37a89df5bf1f4e1f84d5
parent08a33eb0174a10cf9f616048be4d45b5d0510469 (diff)
Added argument parsing
-rw-r--r--blavote.go47
1 files changed, 45 insertions, 2 deletions
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