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