143fbe7bbc33392c36b3492f4532d564a6bbbf43 — Philip K 25 days ago 9a1cd34
Rewrote flag parsing to use flag-like interface
3 files changed, 42 insertions(+), 45 deletions(-)

M go.mod
M go.sum
M openring.go
M go.mod => go.mod +1 -1
@@ 3,7 3,7 @@ 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

M go.sum => go.sum +2 -0
@@ 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=

M openring.go => openring.go +39 -44
@@ 8,15 8,35 @@ "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,46 48,21 @@   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:")
- 	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)
- 		}
+ 	getopt.Usage = func() {
+ 		log.Fatalf("Usage: %s [-s https://source.rss...] < in.html > out.html",
+ 			os.Args[0])
  	}
  
- 	if len(os.Args[optind+1:]) != 0 {
- 		log.Fatalf(
- 			"Usage: %s [-s https://source.rss...] < in.html > out.html",
- 			os.Args[0])
+ 	err := getopt.Parse()
+ 	if err != nil {
+ 		panic(err)
  	}
  
  	input, err := ioutil.ReadAll(os.Stdin)


@@ 111,8 106,8 @@ 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 @@ 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 @@ 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,