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