From 143fbe7bbc33392c36b3492f4532d564a6bbbf43 Mon Sep 17 00:00:00 2001 From: Philip K Date: Fri, 21 Jun 2019 21:56:03 +0200 Subject: [PATCH] Rewrote flag parsing to use flag-like interface --- go.mod | 2 +- go.sum | 2 ++ openring.go | 83 +++++++++++++++++++++++++---------------------------- 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/go.mod b/go.mod index 8cc268f..fc1fcc1 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.sr.ht/~sircmpwn/openring go 1.12 require ( - git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7 + git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0 github.com/SlyMarbo/rss v1.0.1 github.com/mattn/go-runewidth v0.0.4 github.com/microcosm-cc/bluemonday v1.0.2 diff --git a/go.sum b/go.sum index cf9b43b..b6cc966 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7 h1:xTFH5S/3ltiRvAtETLLDFWm5nVIouT5GeCPHm8UaVEU= git.sr.ht/~sircmpwn/getopt v0.0.0-20190214165041-9a4f886f9fc7/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= +git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0 h1:gUeOEsT0mhoCKxKYJk8HeYtUZME686xs70eG2l80W5U= +git.sr.ht/~sircmpwn/getopt v0.0.0-20190621174457-292febf82fd0/go.mod h1:wMEGFFFNuPos7vHmWXfszqImLppbc0wEhh6JBfJIUgw= github.com/SlyMarbo/rss v1.0.1 h1:fiaIU5UhcXauVOniHOIocWG7uj8Ej6pHNarMGPJilzA= github.com/SlyMarbo/rss v1.0.1/go.mod h1:JNF+T33oj4m5WLCQXpBTCgO+SxRbYVgdiiimHNgzcbA= github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ= diff --git a/openring.go b/openring.go index 8c7ffdb..11950bc 100644 --- a/openring.go +++ b/openring.go @@ -8,15 +8,35 @@ import ( "net/url" "os" "sort" - "strconv" + "strings" "time" + "git.sr.ht/~sircmpwn/getopt" + "github.com/SlyMarbo/rss" "github.com/mattn/go-runewidth" "github.com/microcosm-cc/bluemonday" - "git.sr.ht/~sircmpwn/getopt" ) +type urlSlice []*url.URL + +func (us *urlSlice) String() string { + var str []string + for _, u := range *us { + str = append(str, u.String()) + } + return strings.Join(str, ", ") +} + +func (us *urlSlice) Set(val string) error { + u, err := url.Parse(val) + if err != nil { + return err + } + *us = append(*us, u) + return nil +} + type Article struct { Date time.Time Link string @@ -28,47 +48,22 @@ type Article struct { func main() { var ( - narticles int = 3 - perSource int = 1 - summaryLen int = 256 + narticles = getopt.Int("n", 3, "article count") + perSource = getopt.Int("p", 1, "articles to take from each source") + summaryLen = getopt.Int("l", 256, "length of summaries") sources []*url.URL ) + getopt.Var((*urlSlice)(&sources), "s", "list of sources") - opts, optind, err := getopt.Getopts(os.Args[1:], "l:n:p:s:") + getopt.Usage = func() { + log.Fatalf("Usage: %s [-s https://source.rss...] < in.html > out.html", + os.Args[0]) + } + + err := getopt.Parse() if err != nil { panic(err) } - for _, opt := range opts { - switch opt.Option { - case 'l': - summaryLen, err = strconv.Atoi(opt.Value) - if err != nil { - panic(err) - } - case 'n': - narticles, err = strconv.Atoi(opt.Value) - if err != nil { - panic(err) - } - case 'p': - perSource, err = strconv.Atoi(opt.Value) - if err != nil { - panic(err) - } - case 's': - u, err := url.Parse(opt.Value) - if err != nil { - panic(err) - } - sources = append(sources, u) - } - } - - if len(os.Args[optind+1:]) != 0 { - log.Fatalf( - "Usage: %s [-s https://source.rss...] < in.html > out.html", - os.Args[0]) - } input, err := ioutil.ReadAll(os.Stdin) if err != nil { @@ -111,8 +106,8 @@ func main() { continue } items := feed.Items - if len(items) > perSource { - items = items[:perSource] + if len(items) > *perSource { + items = items[:*perSource] } for _, item := range items { raw_summary := item.Summary @@ -120,7 +115,7 @@ func main() { raw_summary = html.UnescapeString(item.Content) } summary := runewidth.Truncate( - policy.Sanitize(raw_summary), summaryLen, "…") + policy.Sanitize(raw_summary), *summaryLen, "…") articles = append(articles, &Article{ Date: item.Date, SourceLink: feed.Link, @@ -134,11 +129,11 @@ func main() { sort.Slice(articles, func(i, j int) bool { return articles[i].Date.After(articles[j].Date) }) - if len(articles) < narticles { - narticles = len(articles) + if len(articles) < *narticles { + *narticles = len(articles) } - articles = articles[:narticles] - err = tmpl.Execute(os.Stdout, struct{ + articles = articles[:*narticles] + err = tmpl.Execute(os.Stdout, struct { Articles []*Article }{ Articles: articles,