all repos — llm_aggregator @ 29a4710e981512a82abba77ec8d7b43e413453e7

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	Timeout     *int     `arg:"--timeout" help:"LLM request timeout in seconds (default: 300)"`
 39
 40	// Output options
 41	Output          string `arg:"-o,--output" help:"Output format" choice:"text,json,markdown"`
 42	OutputFile      string `arg:"--output-file" help:"Write output to file (default: stdout)"`
 43	IncludeArticles bool   `arg:"--include-articles" help:"Include original articles in JSON output"`
 44	Plain           bool   `arg:"-P,--plain" help:"Output only the raw LLM response without any formatting or metadata"`
 45
 46	// System options
 47	SystemPrompt string `arg:"--system-prompt" help:"Custom system prompt for LLM"`
 48	TUI          bool   `arg:"-t,--tui" help:"Enable TUI interface with progress bar"`
 49	Verbose      bool   `arg:"-v,--verbose" help:"Show verbose output"`
 50	ShowVersion  bool   `arg:"--version" help:"Show version"`
 51	DryRun       bool   `arg:"-D,--dry-run" help:"Validate config, show article statistics, and exit without making LLM API calls"`
 52}
 53
 54// Version returns the version string.
 55func (Args) Version() string {
 56	return fmt.Sprintf("llm_aggregator v%s (built %s)", Version, BuildDate)
 57}
 58
 59// Description returns the program description.
 60func (Args) Description() string {
 61	return "LLM Aggregator - Aggregate RSS feeds and summarise with LLM API"
 62}
 63
 64// ParseKeywords parses comma-separated keywords string into list.
 65func ParseKeywords(keywordString string) []string {
 66	if keywordString == "" {
 67		return nil
 68	}
 69	keywords := strings.Split(keywordString, ",")
 70	result := make([]string, 0, len(keywords))
 71	for _, kw := range keywords {
 72		if trimmed := strings.TrimSpace(kw); trimmed != "" {
 73			result = append(result, trimmed)
 74		}
 75	}
 76	return result
 77}
 78
 79// ParseArgs parses command line arguments.
 80func ParseArgs() (*Args, error) {
 81	var args Args
 82	parser, err := arg.NewParser(arg.Config{
 83		Program: "llm_aggregator",
 84	}, &args)
 85	if err != nil {
 86		return nil, err
 87	}
 88
 89	// Handle help and version flags before checking required fields
 90	if len(os.Args) > 1 {
 91		if os.Args[1] == "-h" || os.Args[1] == "--help" {
 92			WriteHelp(&args, os.Stdout)
 93			os.Exit(0)
 94		}
 95		if os.Args[1] == "--version" {
 96			fmt.Printf("llm_aggregator v%s (built %s)", Version, BuildDate)
 97			os.Exit(0)
 98		}
 99	}
100
101	err = parser.Parse(os.Args[1:])
102	if err != nil {
103		return nil, err
104	}
105
106	return &args, nil
107}
108
109// ToViperMap converts Args to a map for binding to Viper.
110// Only non-nil values (explicitly provided CLI flags) are included.
111//
112// NOTE: FeedsFile and Prompt are strings (not pointers), so they're ALWAYS
113// included if non-empty. This differs from optional fields which use pointer
114// types to detect explicit provision vs. default zero values. See isZero() in
115// config package.
116func (a *Args) ToViperMap() map[string]any {
117	m := map[string]any{}
118	if a.FeedsFile != "" {
119		m["feeds_file"] = a.FeedsFile
120	}
121	if a.Stdin {
122		m["stdin"] = a.Stdin
123	}
124	if a.MaxArticlesPerFeed != nil {
125		m["max_articles_per_feed"] = *a.MaxArticlesPerFeed
126	}
127	if a.MaxDaysOld != nil {
128		m["max_days_old"] = *a.MaxDaysOld
129	}
130	if a.MaxTotalArticles != nil {
131		m["max_total_articles"] = *a.MaxTotalArticles
132	}
133	if a.IncludeKeywords != "" {
134		m["include_keywords"] = a.IncludeKeywords
135	}
136	if a.ExcludeKeywords != "" {
137		m["exclude_keywords"] = a.ExcludeKeywords
138	}
139	if a.APIKey != nil {
140		m["api_key"] = *a.APIKey
141	}
142	if a.BaseURL != nil {
143		m["base_url"] = *a.BaseURL
144	}
145	if a.Model != nil {
146		m["model"] = *a.Model
147	}
148	if a.MaxTokens != nil {
149		m["max_tokens"] = *a.MaxTokens
150	}
151	if a.Temperature != nil {
152		m["temperature"] = *a.Temperature
153	}
154	if a.Timeout != nil {
155		m["timeout"] = *a.Timeout
156	}
157	if a.Prompt != "" {
158		m["prompt"] = a.Prompt
159	}
160	if a.SystemPrompt != "" {
161		m["system_prompt"] = a.SystemPrompt
162	}
163	if a.Output != "" {
164		m["output"] = a.Output
165	}
166	if a.OutputFile != "" {
167		m["output_file"] = a.OutputFile
168	}
169	if a.IncludeArticles {
170		m["include_articles"] = a.IncludeArticles
171	}
172	if a.Plain {
173		m["plain"] = a.Plain
174	}
175	return m
176}