doushan3511 2017-06-09 09:46
浏览 45

使用codegangsta / cli捕获命令标志变量

I try to write my first CLI App in Go.

I use codegangsta/cli for the structure.

My problem :

I have some commands :

Add  --name 

Show --all

Delete  --force

From the file /commands/add.go I can't get the variable StackName from /commands.go set like this :

package main
var StackName string
var Commands = []cli.Command{
{
    Name:   "add",
    Usage:  "",
    Action: command.CmdAdd,

    Flags:  []cli.Flag{
        cli.StringFlag{
            Name: "name, n",
            Value: "default",
            Usage: "Stack name",
            Destination: &StackName,
        },

And in my add.go file, I try to get $stackName but I don't find how to do this :

package command

func CmdAdd(c *cli.Context) {
    fmt.PrintF(StackName)
}

I got this : command/add.go:25: undefined: StackName.

Thanks to help to find where I'm wrong.

  • 写回答

1条回答 默认 最新

  • dongxian6285 2017-06-10 03:38
    关注

    What does your file hierarchy look like? Because you may need to import the commands.go file import "github.com/YourGithubName/YourDirectory/commands". I say this because you are trying to call the variable in the command package when it is declared in the main package.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)