From f012eeebbffd75f7d0ea49089110936b65a42714 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 4 Jan 2024 21:14:45 +0100 Subject: [PATCH] Multi-args instead of comma-separated --- README.md | 10 +++++----- cmd/deadlinks/flag.go | 29 +++++++++++++++++++++++++++++ cmd/deadlinks/main.go | 18 ++++++------------ 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 cmd/deadlinks/flag.go diff --git a/README.md b/README.md index 7c9d8f6..31e0c76 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ The command-line utility can be installed using `go install`: go install code.betamike.com/mediocregopher/deadlinks/cmd/deadlinks ``` -The `-urls` parameter is required. Given one or more URLs it will check each one -for any dead links: +The `-url` parameter is required. Given a URL it will check it for +any dead links. Can be specified more than once: ``` -deadlinks -urls 'https://mediocregopher.com,gemini://mediocregopher.com' +deadlinks -url='https://mediocregopher.com' -url='gemini://mediocregopher.com' ``` Any links which are dead will be output to stdout as YAML objects, each @@ -36,8 +36,8 @@ so on): ``` deadlinks \ - -urls 'https://mediocregopher.com,gemini://mediocregopher.com' \ - -patterns '://mediocregopher.com' + -url='https://mediocregopher.com' -url='gemini://mediocregopher.com' \ + -pattern='://mediocregopher.com' ``` There are further options available which affect the utility's behavior, see diff --git a/cmd/deadlinks/flag.go b/cmd/deadlinks/flag.go new file mode 100644 index 0000000..7f76e4e --- /dev/null +++ b/cmd/deadlinks/flag.go @@ -0,0 +1,29 @@ +package main + +import ( + "flag" + "strings" +) + +// Created so that multiple inputs can be accecpted +type arrayFlags struct { + strs *[]string +} + +func flagStrings(name, usage string) arrayFlags { + f := arrayFlags{new([]string)} + flag.Var(&f, name, usage) + return f +} + +func (i arrayFlags) String() string { + if i.strs == nil { + return "" + } + return strings.Join(*i.strs, ", ") +} + +func (i arrayFlags) Set(value string) error { + *i.strs = append(*i.strs, strings.TrimSpace(value)) + return nil +} diff --git a/cmd/deadlinks/main.go b/cmd/deadlinks/main.go index 552dd65..67bb6a4 100644 --- a/cmd/deadlinks/main.go +++ b/cmd/deadlinks/main.go @@ -8,7 +8,6 @@ import ( "os" "os/signal" "runtime" - "strings" "time" "code.betamike.com/mediocregopher/deadlinks" @@ -33,21 +32,16 @@ func main() { var ( storePath = flag.String("store-path", "", "Path to sqlite storage file. If not given then a temporary in-memory storage is used") maxAge = flag.Duration("max-age", 0, "Maximum duration since last check of a resource, before it must be checked again. Must be used with -store-path") - urls = flag.String("urls", "", `Comma-separated list of URLs which are always checked. At least one is required`) - patternsStr = flag.String("patterns", "", "Comma-separated list of regexps. All URLs which match one of these will have their links checked as well") + urls = flagStrings("url", "URL which is always checked. Must be given at least once") + patterns = flagStrings("pattern", "URLs matching this regex will have their links checked as well. Can be specified multiple times") concurrency = flag.Int("concurrency", runtime.NumCPU()/2, "Number simultaneous requests to make at a time") httpUserAgent = flag.String("http-user-agent", "", "User-agent to use for http requests") ) flag.Parse() - if *urls == "" { - log.Fatal("-urls is required") - } - - var patterns []string - if *patternsStr != "" { - patterns = strings.Split(*patternsStr, ",") + if len(*urls.strs) == 0 { + log.Fatal("at least one -url is required") } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) @@ -61,8 +55,8 @@ func main() { dl, err := deadlinks.New( ctx, store, - strings.Split(*urls, ","), - patterns, + *urls.strs, + *patterns.strs, &deadlinks.Opts{ NewClient: func() deadlinks.Client { return loggingClient{deadlinks.NewClient(&deadlinks.ClientOpts{