diff options
| -rw-r--r-- | blavote.go | 47 | 
1 files changed, 45 insertions, 2 deletions
| @@ -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 | 
