如何将日期传递给命令行?

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

How to pass a date to command line?

问题

Go语言的flag包中有一些预定义的类型。我该如何传递一个日期?

我模仿了URL解析的示例,但看起来有点冗长,我可能漏掉了一些东西。

package main

import (
	"flag"
	"fmt"
	"time"
)

const dateLayout = "2006-01-02"

type DateValue struct {
	Date *time.Time
}

func (v DateValue) String() string {
	if v.Date != nil {
		return v.Date.Format(dateLayout)
	}
	return ""
}

func (v DateValue) Set(str string) error {
	if t, err := time.Parse(dateLayout, str); err != nil {
		return err
	} else {
		*v.Date = t
	}
	return nil
}

func main() {
	var d = &time.Time{}

	flag.Var(&DateValue{d}, "date", "Date part of ISO")
	flag.Parse([]string{"--date", "2022-08-08"})
	fmt.Printf(`{date: %q}`, d)
}

不幸的是,这会导致错误。你可以在playground上看到错误信息。

./prog.go:35:13: flag.Parse调用中的参数过多

有 ([]string)

需要 ()

英文:

Go has a few predefined types in its flag package. How would I pass a date?

I have mimicked the URL parsing example but it looks a bit verbose and I am missing smth.

package main

import (
	"flag"
	"fmt"
	"time"
)

const dateLayout = "2006-01-02"

type DateValue struct {
	Date *time.Time
}

func (v DateValue) String() string {
	if v.Date != nil {
		return v.Date.Format(dateLayout)
	}
	return ""
}

func (v DateValue) Set(str string) error {
	if t, err := time.Parse(dateLayout, str); err != nil {
		return err
	} else {
		*v.Date = t
	}
	return nil
}

func main() {
	var d = &time.Time{}

	flag.Var(&DateValue{d}, "date", "Date part of ISO")
	flag.Parse([]string{"--date", "2022-08-08"})
	fmt.Printf(`{date: %q}`, d)
}

Alas, this fails with an error. You can see it at the playground

> ./prog.go:35:13: too many arguments in call to flag.Parse
>
> have ([]string)
>
> want ()

答案1

得分: 3

Parseos.Args[1:] 解析命令行标志,需要在定义顶级标志后调用。要构建子命令,例如 prog --date <date-val>,请使用 Flagset

从方法签名可以看出,它不接受任何参数,这就是你在参数不匹配时立即出现错误的原因(pkg.go.dev 是你的朋友)

根据你的代码,创建一个默认的 Flagset 并分配新的标志和相关值

func main() {
	var d = &time.Time{}
	fs := flag.NewFlagSet("DateValue", flag.ExitOnError)
	
	fs.Var(&DateValue{d}, "date", "Date part of ISO")
	fs.Parse([]string{"--date", "2022-08-08"})

	fmt.Printf(`{date: %q}`, d)
}

go-playground

英文:

Parse parses the command-line flags from os.Args[1:] and needs to be called after the top level flags are defined. To build sub-commands e.g. prog --date <date-val>, use a Flagset.

You can notice from the method signature, that it does not take any arguments, which is the reason for your immediate error on the argument mismatch (pkg.go.dev is your friend)

With your code as shown, create a default Flagset and assign new flags and associated values

func main() {
	var d = &time.Time{}
	fs := flag.NewFlagSet("DateValue", flag.ExitOnError)
	
	fs.Var(&DateValue{d}, "date", "Date part of ISO")
	fs.Parse([]string{"--date", "2022-08-08"})

	fmt.Printf(`{date: %q}`, d)
}

go-playground

huangapple
  • 本文由 发表于 2022年8月8日 15:46:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/73274432.html
匿名

发表评论

匿名网友

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

确定