aboutsummaryrefslogtreecommitdiff
path: root/internal/cli/cli.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/cli/cli.go')
-rw-r--r--internal/cli/cli.go42
1 files changed, 38 insertions, 4 deletions
diff --git a/internal/cli/cli.go b/internal/cli/cli.go
index 364e080..f65774f 100644
--- a/internal/cli/cli.go
+++ b/internal/cli/cli.go
@@ -61,6 +61,11 @@ Flags:
--pdf export the readings as PDF (to --out or stdout)
--out FILE write --md/--pdf/--calendar output to FILE
--calendar YYYY-MM export the month as a printable A4 PDF calendar
+ --format WHICH json|ical: emit the computed calendar and exit
+ --from DATE range start YYYY-MM-DD (with --to; needs --format)
+ --to DATE range end YYYY-MM-DD (with --from; needs --format)
+ --year YYYY emit the whole year YYYY (needs --format)
+ --form WHICH override calendar form old|new (needs --format)
-v, --version print the version and exit
-h, --help this help
@@ -129,6 +134,7 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
var calNew, calCheck string
var corpusCheck string
var jsonOut bool
+ var format, fromFlag, toFlag, yearFlag, formFlag string
fs := flag.NewFlagSet("lectio", flag.ContinueOnError)
fs.SetOutput(stderr)
@@ -179,6 +185,11 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
fs.StringVar(&calCheck, "cal-check", "", "validate the calendar-layer file NAME.ini and exit")
fs.StringVar(&corpusCheck, "corpus-check", "", "validate a bible corpus (code or path to <code>.tsv) and exit")
fs.BoolVar(&jsonOut, "json", false, "with --corpus-check, print the report as JSON")
+ fs.StringVar(&format, "format", "", "json|ical: emit the computed calendar and exit")
+ fs.StringVar(&fromFlag, "from", "", "range start YYYY-MM-DD (with --to; needs --format)")
+ fs.StringVar(&toFlag, "to", "", "range end YYYY-MM-DD (with --from; needs --format)")
+ fs.StringVar(&yearFlag, "year", "", "emit the whole year YYYY (needs --format)")
+ fs.StringVar(&formFlag, "form", "", "override calendar form: old|new (needs --format)")
if err := fs.Parse(rest); err != nil {
return 2
@@ -235,6 +246,10 @@ func Run(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
cfg.UILanguage = config.NormalizeUILanguage(uiLang)
}
+ if format != "" || fromFlag != "" || toFlag != "" || yearFlag != "" || formFlag != "" {
+ return runFeed(cfg, format, date, fromFlag, toFlag, yearFlag, formFlag, stdout, stderr)
+ }
+
if liturgy {
return runLiturgy(cfg, date, stdout, stderr)
}
@@ -553,15 +568,34 @@ func wantsVersion(args []string) bool {
return false
}
+// rangeDateFlags are the flags whose own value is itself a YYYY-MM-DD date
+// (--from/--to, in either one- or two-dash spelling -- Go's flag package
+// treats them the same). extractDate must not mistake such a value for the
+// positional DATE token.
+var rangeDateFlags = map[string]bool{"--from": true, "-from": true, "--to": true, "-to": true}
+
// extractDate pulls the single positional DATE token (YYYY-MM-DD, matching
// dateRe) out of args, wherever it appears, and returns it along with the
-// remaining tokens for flag.FlagSet to parse. No flag's own value can match
-// dateRe's shape (width is an int, versions/lists/lang/lectionary are short
-// codes), so scanning raw tokens this way is unambiguous. Defaults to
-// today() when no date token is present; errors if more than one is found.
+// remaining tokens for flag.FlagSet to parse. No other flag's own value can
+// match dateRe's shape (width is an int, versions/lists/lang/lectionary are
+// short codes) except --from/--to's, which are skipped explicitly via
+// rangeDateFlags, so scanning raw tokens this way is unambiguous. Defaults
+// to today() when no date token is present; errors if more than one is
+// found.
func extractDate(args []string) (date string, rest []string, err error) {
found := false
+ skipNext := false
for _, a := range args {
+ if skipNext {
+ rest = append(rest, a)
+ skipNext = false
+ continue
+ }
+ if rangeDateFlags[a] {
+ rest = append(rest, a)
+ skipNext = true
+ continue
+ }
if dateRe.MatchString(a) {
if found {
return "", nil, fmt.Errorf("multiple dates given (%q and %q)", date, a)