bugfix CLI parsing

This commit is contained in:
AJ ONeal 2020-08-15 06:07:47 -06:00
parent 6af1527e7a
commit d2076da9a0
1 changed files with 13 additions and 9 deletions

View File

@ -12,7 +12,7 @@ import (
func help() { func help() {
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println("\thashcash new [subject *] [expires in 5m] [difficulty 10]") fmt.Println("\thashcash new [subject *] [difficulty 10] [expires in 5m]")
fmt.Println("\thashcash parse <hashcash>") fmt.Println("\thashcash parse <hashcash>")
fmt.Println("\thashcash solve <hashcash>") fmt.Println("\thashcash solve <hashcash>")
fmt.Println("\thashcash verify <hashcash> [subject *]") fmt.Println("\thashcash verify <hashcash> [subject *]")
@ -20,8 +20,9 @@ func help() {
func main() { func main() {
args := os.Args[:] args := os.Args[:]
nargs := len(args)
if len(args) < 2 { if nargs < 2 {
help() help()
os.Exit(1) os.Exit(1)
return return
@ -30,27 +31,30 @@ func main() {
switch args[1] { switch args[1] {
case "new": case "new":
var subject string var subject string
if len(args) > 3 { if nargs >= 3 {
subject = args[2] subject = args[2]
} }
var difficulty int var difficulty int
if len(args) > 4 { if nargs >= 4 {
var err error var err error
difficulty, err = strconv.Atoi(args[3]) difficulty, err = strconv.Atoi(args[3])
if nil != err { if nil != err {
help() help()
os.Exit(1) os.Exit(1)
return
} }
} }
var expIn time.Duration var expIn time.Duration
if len(args) > 5 { if nargs >= 5 {
var err error var err error
expIn, err = time.ParseDuration(args[4]) expIn, err = time.ParseDuration(args[4])
if nil != err { if nil != err {
fmt.Println(err)
help() help()
os.Exit(1) os.Exit(1)
return
} }
} else { } else {
expIn = 5 * time.Minute expIn = 5 * time.Minute
@ -66,7 +70,7 @@ func main() {
return return
case "parse": case "parse":
var token string var token string
if 3 != len(args) { if 3 != nargs {
help() help()
os.Exit(1) os.Exit(1)
return return
@ -82,7 +86,7 @@ func main() {
return return
case "solve": case "solve":
var token string var token string
if 3 != len(args) { if 3 != nargs {
help() help()
os.Exit(1) os.Exit(1)
return return
@ -105,7 +109,7 @@ func main() {
return return
case "verify": case "verify":
var token string var token string
if len(args) < 3 { if nargs < 3 {
help() help()
os.Exit(1) os.Exit(1)
return return
@ -118,7 +122,7 @@ func main() {
} }
subject := "*" subject := "*"
if len(args) > 3 { if nargs >= 4 {
subject = args[3] subject = args[3]
} }