docopt.go出现奇怪的错误消息

huangapple go评论89阅读模式
英文:

docopt.go weird error message

问题

使用docopt.go对一个旧项目进行重构并最小化代码,程序的样子如下:

package main

import (
	"fmt"
	"github.com/docopt/docopt.go"
)

const Version = `2.0`
const Usage = `
Usage:
	serve [--port] <dir>
	serve help | --help
	serve --version 

Options:
	-p, --port       port for the sever to listen on
	-h, --help       display help information
	-v, --version    display Version
`

func check(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, false)
	check(err)

	port := args["[--port]"].(string)

	fmt.Println(args)
	fmt.Println(port)
}

然而,当我运行go run ./serve.go help以获取帮助信息时,我得到了以下错误:

panic: interface conversion: interface is nil, not string

goroutine 1 [running]:
main.main()
	/Users/jburns/Development/Gopath/src/github.com/nyumal/serve/serve.go:31 +0x148

goroutine 2 [runnable]:
runtime.forcegchelper()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/proc.go:90
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/mgc0.go:82
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/malloc.go:712
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

而运行go run ./serve.go --port 5000返回相同的错误,然而运行go run ./serve.go --port 5000 .返回:

Usage:
	serve [--port] <dir>
	serve help | --help
	serve --version
exit status 1

我错在哪里了?

英文:

using docopt.go to refactor an old project and minimalise code the program looks like this

package main

import (
	&quot;fmt&quot;
	&quot;github.com/docopt/docopt.go&quot;
)

const Version = `2.0`
const Usage = `
Usage:
	serve [--port] &lt;dir&gt;
	serve help | --help
	serve --version 

Options:
	-p, --port       port for the sever to listen on
	-h, --help       display help information
	-v, --version    display Version
`

func check(err error) {
	if err != nil {
		panic(err)
	}
}

func main() {
	args, err := docopt.Parse(Usage, nil, true, Version, false)
	check(err)

	port := args[&quot;[--port]&quot;].(string)

	fmt.Println(args)
	fmt.Println(port)v
}

however when I run the program go run ./serve.go help expecting the help message i get this

panic: interface conversion: interface is nil, not string

goroutine 1 [running]:
main.main()
	/Users/jburns/Development/Gopath/src/github.com/nyumal/serve/serve.go:31 +0x148

goroutine 2 [runnable]:
runtime.forcegchelper()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/proc.go:90
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 3 [runnable]:
runtime.bgsweep()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/mgc0.go:82
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1

goroutine 4 [runnable]:
runtime.runfinq()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/malloc.go:712
runtime.goexit()
	/usr/local/Cellar/go/1.4.1/libexec/src/runtime/asm_amd64.s:2232 +0x1
exit status 2

and running go run ./serve.go --port 5000 it returns the same thing however running go run ./serve.go --port 5000 . return

Usage:
	serve [--port] &lt;dir&gt;
	serve help | --help
	serve --version
exit status 1

where did i go wrong?

答案1

得分: 1

你需要为端口声明一个参数:

const Usage = `
用法:
    serve [--port=<arg>] <dir>
    serve help | --help
    serve --version 

选项:
    -p, --port=<arg> 服务器监听的端口
    -h, --help       显示帮助信息
    -v, --version    显示版本

使用两个值类型断言来处理未设置端口的情况:

port, ok := args["--port"].(string)
if ok {
   // 端口已设置
}

此外,从映射键周围删除 "[]"。

英文:

You need to declare an argument for port:

const Usage = `
Usage:
    serve [--port=&lt;arg&gt;] &lt;dir&gt;
    serve help | --help
    serve --version 

Options:
    -p, --port=&lt;arg&gt; port for the sever to listen on
    -h, --help       display help information
    -v, --version    display Version

Use the two value type assertion to handle the case where port is not set:

port, ok := args[&quot;--port&quot;].(string)
if ok {
   // port is set
}

Also, remove the "[]" from around the map key.

huangapple
  • 本文由 发表于 2015年1月30日 13:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/28229808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定