How to get a list of values into a flag in Golang?

后端 未结 4 1235
失恋的感觉
失恋的感觉 2020-12-13 03:53

What is Golang\'s equivalent of the below python commands ?

import argparse
parser = argparse.ArgumentParser(description=\"something\")
parser.add_argument(\         


        
4条回答
  •  失恋的感觉
    2020-12-13 04:11

    You can at least have a list of arguments on the end of you command by using the flag.Args() function.

    package main
    
    import (
        "flag"
        "fmt"
    )
    
    var one string
    
    func main() {
        flag.StringVar(&one, "o", "default", "arg one")
        flag.Parse()
        tail := flag.Args()
        fmt.Printf("Tail: %+q\n", tail)
    }
    

    my-go-app -o 1 this is the rest will print Tail: ["this" "is" "the" "rest"]

提交回复
热议问题