duanneng2014 2015-02-17 15:48
浏览 73
已采纳

使用cli Go包读取外部文件

I'm using the cli Go package: https://github.com/codegangsta/cli

package main

import (
    "fmt"
    "github.com/codegangsta/cli"
    "io/ioutil"
    "os"
)

func main() {
    app := cli.NewApp()
    app.Name = "m2k"
    app.Usage = "convert markdown to kindle"
    app.Flags = []cli.Flag{
        cli.StringFlag{
            Name:  "file",
            Value: "english",
            Usage: "language for the greeting",
        },
    }

    app.Action = func(c *cli.Context) {
        file := "default"

        if len(c.Args()) > 0 {
            file = c.Args()[0]
        }

        //fmt.Println("file %s", file)
        fmt.Println("file %s", file)

        b, err := ioutil.ReadFile(file)
        if err != nil {
            panic(err)
        }

        err = ioutil.WriteFile("output.txt", b, 0644)
        if err != nil {
            panic(err)
        }
    }

    app.Run(os.Args)
}

I'm a Go beginner. So I probably did something very wrong.

I do this in the command line (I have a file called markdown.txt in the directory):

~/go/io$ go run io.go -file markdown.txt

But I get this error:

created by runtime.main /usr/lib/go/src/pkg/runtime/proc.c:221 exit status 2

EDIT:

I wrote fmt.Println("file %s", file) to app.Action but nothing is printed. Does this tell something about the problem?

  • 写回答

1条回答 默认 最新

  • doujia4619 2015-02-17 16:02
    关注

    cli.Context.Args returns args form the current context. Meaning, not the global arguments supplied. If you print the length of c.Args you'll see it is zero.

    You can either use c.String("file") or remove the app.Flags, supply a command in the format of go run io.go path/to/file.txt and use c.Args.First()

    Example:

    package main
    
    import (
        "github.com/codegangsta/cli"
    
        "io/ioutil"
        "log"
        "os"
        "strings"
    )
    
    func main() {
        app := cli.NewApp()
        app.Name = "m2k"
        app.Usage = "convert markdown to kindle"
        app.Action = mainAction
        app.Run(os.Args)
    }
    
    func mainAction(c *cli.Context) {
        var err error
    
        // use stdin/stdout by default
        in := os.Stdin
        out := os.Stdout
        defer out.Close()
        defer in.Close()
    
        // replace stdin if given
        if c.Args().First() != "" {
            in, err = os.Open(c.Args().First())
            if err != nil {
                log.Fatalln(err)
            }
            log.Printf("Using '%s' as source
    ", c.Args().First())
        }
    
        // replace stdout if given
        if c.Args().Get(1) != "" {
            out = os.NewFile(0666, c.Args().Get(1))
            log.Printf("Writing output to '%s'
    ", c.Args().Get(1))
        }
    
        b, err := ioutil.ReadAll(in)
        if err != nil {
            log.Fatalln(err)
        }
    
        out.Write([]byte(strings.Replace(string(b), "in", "out", 1)))
    }
    

    Result:

    echo "in file content" > in.txt
    go run foo.go in.txt
    // 2015/02/17 11:49:49 Using 'in.txt' as source
    // out file content
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示