From 89f83d7a1fe57649fbd59e9a3da2806061cc5be2 Mon Sep 17 00:00:00 2001 From: Nikolaus Gotsche Date: Sun, 16 Sep 2018 03:32:17 +0200 Subject: gnu getopt style flags --- interact.go | 18 ++++---- main.go | 144 ++++++++++++++++++++++++++++++++---------------------------- 2 files changed, 86 insertions(+), 76 deletions(-) diff --git a/interact.go b/interact.go index 7514046..741d622 100644 --- a/interact.go +++ b/interact.go @@ -1,7 +1,7 @@ package main import ( - "os" + //"os" "strings" "strconv" "fmt" @@ -13,7 +13,7 @@ import ( func interact() { - stdOut() + //stdOut() shell := ishell.New() //fmt.Println(os.Args) @@ -22,7 +22,6 @@ func interact() { green := color.New(color.FgGreen).SprintFunc() boldBlue := color.New(color.FgBlue, color.Bold).SprintFunc() boldRed := color.New(color.FgRed, color.Bold).SprintFunc() - boldMag := color.New(color.FgMagenta, color.Bold).SprintFunc() boldCyan := color.New(color.FgCyan, color.Bold).SprintFunc() boldGreen := color.New(color.FgGreen, color.Bold).SprintFunc() @@ -565,23 +564,22 @@ func interact() { }, }) - //shell.Run() - // teardown - //shell.Close() -// when started with "exit" as first argument, assume non-interact ive execution - args := removeStringFromArray(os.Args[1:],"-file",1) + // Decide if interactive mode should be started + //args := removeStringFromArray(os.Args[1:],"-file",1) - if len(args) > 0 { + if len(interArgs) > 0 { //args := removeStringFromArray(os.Args[1:],"-file",1) //fmt.Println(args) - shell.Process(args...) + shell.Process(interArgs...) } else { shell.Println("Starting interactive Shell") + stdOut() //start shell shell.Run() // teardown shell.Close() } + fmt.Println("Laboravi emeritus...") } diff --git a/main.go b/main.go index c34a829..4ba7419 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,15 @@ package main import ( "fmt" - "flag" - _ "path/filepath" + //"flag" + "path/filepath" _ "log" //"bufio" "os" //"strings" _ "os/exec" "github.com/fatih/color" + "github.com/pborman/getopt/v2" ) var ( @@ -17,17 +18,53 @@ var ( compdate string ) //var svar string -var starttime,stoptime,filename string +//var starttime,stoptime,filename string //var projectid int //, edittaskid, editprojectid, editcustomerid int //var deltask,delbill int //var addcustomer bool -var newproject, newtask, stoptask, allproj, allbills, runinter, test, newconfig bool -var billcount int +//var newproject, newtask, stoptask, allproj, allbills, runinter, test, newconfig bool +//var billcount int + +var test,help bool +var filename string +var interArgs []string //var red, green, yellow, cyan color //var boldRed, boldGreen color + +func init() { + + filename="none" + //interArgs = removeStringFromArray(os.Args[1:],"-file",1) + + getopt.FlagLong(&test, "test", 't', "Test some new Functions") + getopt.FlagLong(&help, "help", 'h', "Show Program Usage") + getopt.FlagLong(&filename, "file", 'f', "Load different DB than in config") + //flag.StringVar(&svar, "svar", "bar", "A String Var") + +/* flag.BoolVar(&test, + "test", + false, + "Test Some Functions") + flag.StringVar(&filename, + "file", + "none", + "Open a different Database than specified in Configuration") + // CUSTOMIZE USAGE + flag.Usage = func() { + //fmt.Fprintf(os.Stderr, "Usage of %s Version %s (%s):\n %s [Options] \n\nOptions:\n", os.Args[0],version,compdate,os.Args[0]) + fmt.Fprintf(os.Stdout, "Usage of %s Version %s (%s):\n %s [Options] \n\nOptions:\n", os.Args[0],version,compdate,os.Args[0]) + flag.PrintDefaults() + interArgs = append([]string{"help"},interArgs...) + } + //flag.SetOutput(os.Stdout) + flag.Parse()*/ + +} + + func title(frame bool) { bR := color.New(color.FgRed, color.Bold).SprintFunc() bHiR := color.New(color.FgHiRed, color.Bold).SprintFunc() @@ -50,31 +87,6 @@ func title(frame bool) { } } - -func init() { - - - //flag.StringVar(&svar, "svar", "bar", "A String Var") - - flag.BoolVar(&test, - "test", - false, - "Test Some Functions") - flag.StringVar(&filename, - "file", - "none", - "Open a different Database than specified in Configuration") - // CUSTOMIZE USAGE - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage of %s Version %s (%s):\n %s [Options] \n\nOptions:\n", os.Args[0],version,compdate,os.Args[0]) - flag.PrintDefaults() - } - - flag.Parse() - -} - - func stdOut() { showLastProject() @@ -85,58 +97,58 @@ func stdOut() { func main() { //boldRed := color.New(color.FgRed, color.Bold).SprintFunc() //fmt.Println("Laboravi Started") + getopt.Parse() + interArgs = getopt.Args() + + fmt.Println(interArgs) dbname := "./.mytimes.db" -// if flag.NFlag() > 0 { - title(true) -// }else{ -// title(false) - //fmt.Println("Laboravi Started") -// } - initConf() - dbname = config.Database - if filename != "none" { - dbname = filename - } -/* if len(flag.Args())>0 { - - aar := flag.Args() - var uar []string - haveone := false - for _,car := range aar { - if filepath.Ext(car) == ".db" { - if haveone { - fmt.Println("Only One DB Allowed! Will ignore",car) - } else { - dbname=car - haveone=true - } - }else{ - uar = append(uar,car) - } - } - if len(uar)>0 { - fmt.Println("Unknown Commands: ",boldRed(uar)) - } + if len(interArgs) > 0 { + title(false) + }else{ + title(true) } -*/ - if test { - fmt.Println(Round(12.55,0.5),Round(12.55,0.1),Round(12.55,1),Round(12.55,5)) + if help { + fmt.Printf("Usage of %s Version %s (%s):\n Use Commands as Parameters\n\n", os.Args[0],version,compdate) + getopt.Usage() + interArgs = append([]string{"help"},interArgs...) + + }else if test { + //fmt.Println(Round(12.55,0.5),Round(12.55,0.1),Round(12.55,1),Round(12.55,5)) //fmt.Println(getGitTag()) - //fmt.Println("Nothing to test") + fmt.Println("Nothing to test") //tmpltest() //newBill(1) //c := []int{2,3} //a,b := getUnfinishedList(c) //fmt.Println(a,b) os.Exit(0) + }else{ + initConf() } + + haveone := false + if filename != "none" { + if haveone { + fmt.Println("Only One DB Allowed! Will ignore",filename) + }else{ + haveone = true + dbname = filename + if filepath.Ext(filename) != ".db" || filepath.Ext(filename) != ".sql" { + fmt.Println("Please consider to use a file extension like .db or .sql \n like ~/..db") + } + } + }else{ + dbname = config.Database + } + + if !help { initDB(dbname) getLastProject() - //getProjects() getOpenTask() + } interact() -- cgit v1.2.3