all repos — llm_aggregator @ a0e7d1a442a251ae94645cdddccf30ded92e97a1

A CLI tool to aggregate RSS feeds and summarise them with LLMs

internal/cli/args.go (view raw)

  1package cli
  2
  3import (
  4	"fmt"
  5	"os"
  6	"strings"
  7
  8	"github.com/alexflint/go-arg"
  9)
 10
 11var (
 12	Version   string
 13	BuildDate string
 14)
 15
 16// Args represents command-line arguments.
 17type Args struct {
 18	// Feed input
 19	FeedsFile string `arg:"-f,--feeds-file" help:"Path to file containing RSS feed URLs (one per line)"`
 20	Stdin      bool   `arg:"--stdin" help:"Read a single RSS/Atom feed from stdin (can be combined with --feeds-file)"`
 21	Prompt    string `arg:"-p,--prompt,required" help:"User prompt for summarisation/analysis"`
 22
 23	// Feed aggregation options
 24	MaxArticlesPerFeed *int `arg:"-n,--max-articles-per-feed" help:"Maximum articles to fetch from each feed"`
 25	MaxDaysOld         *int `arg:"-d,--max-days-old" help:"Only include articles from the last N days"`
 26	MaxTotalArticles   *int `arg:"--max-total-articles" help:"Maximum total articles to process"`
 27
 28	// Content filtering
 29	IncludeKeywords string `arg:"-i,--include-keywords" help:"Comma-separated list of keywords to include (case-insensitive)"`
 30	ExcludeKeywords string `arg:"-e,--exclude-keywords" help:"Comma-separated list of keywords to exclude (case-insensitive)"`
 31
 32	// LLM API options
 33	APIKey      *string  `arg:"--api-key" help:"OpenAI-compatible API key (default: read from LLM_AGGREGATOR_API_KEY env var)"`
 34	BaseURL     *string  `arg:"--base-url" help:"API base URL"`
 35	Model       *string  `arg:"-m,--model" help:"LLM model to use"`
 36	MaxTokens   *int     `arg:"--max-tokens" help:"Maximum tokens in response"`
 37	Temperature *float64 `arg:"--temperature" help:"Sampling temperature (0.0 to 1.0)"`
 38
 39	// Output options
 40	Output          string `arg:"-o,--output" help:"Output format" choice:"text,json,markdown"`
 41	OutputFile      string `arg:"--output-file" help:"Write output to file (default: stdout)"`
 42	IncludeArticles bool   `arg:"--include-articles" help:"Include original articles in JSON output"`
 43	Plain           bool   `arg:"-P,--plain" help:"Output only the raw LLM response without any formatting or metadata"`
 44
 45	// System options
 46	SystemPrompt string `arg:"--system-prompt" help:"Custom system prompt for LLM"`
 47	TUI          bool   `arg:"-t,--tui" help:"Enable TUI interface with progress bar"`
 48	Verbose      bool   `arg:"-v,--verbose" help:"Show verbose output"`
 49	ShowVersion  bool   `arg:"--version" help:"Show version"`
 50	DryRun       bool   `arg:"-D,--dry-run" help:"Validate config, show article statistics, and exit without making LLM API calls"`
 51}
 52
 53// Version returns the version string.
 54func (Args) Version() string {
 55	return fmt.Sprintf("llm_aggregator v%s (built %s)", Version, BuildDate)
 56}
 57
 58// Description returns the program description.
 59func (Args) Description() string {
 60	return "LLM Aggregator - Aggregate RSS feeds and summarise with LLM API"
 61}
 62
 63// ParseKeywords parses comma-separated keywords string into list.
 64func ParseKeywords(keywordString string) []string {
 65	if keywordString == "" {
 66		return nil
 67	}
 68	keywords := strings.Split(keywordString, ",")
 69	result := make([]string, 0, len(keywords))
 70	for _, kw := range keywords {
 71		if trimmed := strings.TrimSpace(kw); trimmed != "" {
 72			result = append(result, trimmed)
 73		}
 74	}
 75	return result
 76}
 77
 78// ParseArgs parses command line arguments.
 79func ParseArgs() (*Args, error) {
 80	var args Args
 81	parser, err := arg.NewParser(arg.Config{
 82		Program: "llm_aggregator",
 83	}, &args)
 84	if err != nil {
 85		return nil, err
 86	}
 87
 88	// Handle help and version flags before checking required fields
 89	if len(os.Args) > 1 {
 90		if os.Args[1] == "-h" || os.Args[1] == "--help" {
 91			WriteHelp(&args, os.Stdout)
 92			os.Exit(0)
 93		}
 94		if os.Args[1] == "--version" {
 95			fmt.Printf("llm_aggregator v%s (built %s)", Version, BuildDate)
 96			os.Exit(0)
 97		}
 98	}
 99
100	err = parser.Parse(os.Args[1:])
101	if err != nil {
102		return nil, err
103	}
104
105	return &args, nil
106}
107
108// ToViperMap converts Args to a map for binding to Viper.
109// Only non-nil values (explicitly provided CLI flags) are included.
110//
111// NOTE: FeedsFile and Prompt are strings (not pointers), so they're ALWAYS
112// included if non-empty. This differs from optional fields which use pointer
113// types to detect explicit provision vs. default zero values. See isZero() in
114// config package.
115func (a *Args) ToViperMap() map[string]any {
116	m := map[string]any{}
117	if a.FeedsFile != "" {
118		m["feeds_file"] = a.FeedsFile
119	}
120	if a.Stdin {
121		m["stdin"] = a.Stdin
122	}
123	if a.MaxArticlesPerFeed != nil {
124		m["max_articles_per_feed"] = *a.MaxArticlesPerFeed
125	}
126	if a.MaxDaysOld != nil {
127		m["max_days_old"] = *a.MaxDaysOld
128	}
129	if a.MaxTotalArticles != nil {
130		m["max_total_articles"] = *a.MaxTotalArticles
131	}
132	if a.IncludeKeywords != "" {
133		m["include_keywords"] = a.IncludeKeywords
134	}
135	if a.ExcludeKeywords != "" {
136		m["exclude_keywords"] = a.ExcludeKeywords
137	}
138	if a.APIKey != nil {
139		m["api_key"] = *a.APIKey
140	}
141	if a.BaseURL != nil {
142		m["base_url"] = *a.BaseURL
143	}
144	if a.Model != nil {
145		m["model"] = *a.Model
146	}
147	if a.MaxTokens != nil {
148		m["max_tokens"] = *a.MaxTokens
149	}
150	if a.Temperature != nil {
151		m["temperature"] = *a.Temperature
152	}
153	if a.Prompt != "" {
154		m["prompt"] = a.Prompt
155	}
156	if a.SystemPrompt != "" {
157		m["system_prompt"] = a.SystemPrompt
158	}
159	if a.Output != "" {
160		m["output"] = a.Output
161	}
162	if a.OutputFile != "" {
163		m["output_file"] = a.OutputFile
164	}
165	if a.IncludeArticles {
166		m["include_articles"] = a.IncludeArticles
167	}
168	if a.Plain {
169		m["plain"] = a.Plain
170	}
171	return m
172}