Class: HledgerForecast::Cli
- Inherits:
-
Object
- Object
- HledgerForecast::Cli
- Defined in:
- lib/hledger_forecast/cli.rb
Overview
The Command Line Interface for the application Takes user arguments and translates them into actions
Class Method Summary collapse
- .compare(options) ⇒ Object
- .generate(options) ⇒ Object
- .parse_commands(args = ARGV, _stdin = $stdin) ⇒ Object
- .parse_compare_options(args) ⇒ Object
- .parse_generate_options(args) ⇒ Object
- .parse_summarize_options(args) ⇒ Object
- .run(command, options) ⇒ Object
- .summarize(options) ⇒ Object
Class Method Details
.compare(options) ⇒ Object
296 297 298 299 300 301 302 |
# File 'lib/hledger_forecast/cli.rb', line 296 def self.compare() if !File.exist?([:file1]) || !File.exist?([:file2]) return puts("\nError: ".bold.red + "One or more of the files could not be found to compare") end puts(Comparator.compare([:file1], [:file2])) end |
.generate(options) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/hledger_forecast/cli.rb', line 254 def self.generate() forecast = File.read([:forecast_file]) begin transactions = Generator.generate(forecast, ) rescue StandardError => e puts("An error occurred while generating transactions: #{e.}") exit(1) end output_file = [:output_file] if File.exist?(output_file) && ![:force] print("\nFile '#{output_file}' already exists. Overwrite? (y/n): ") overwrite = gets.chomp.downcase if overwrite == "y" File.write(output_file, transactions) puts("\nSuccess: ".bold.green + "File '#{output_file}' has been overwritten.") else puts("\nInfo: ".bold.blue + "Operation aborted. File '#{output_file}' was not overwritten.") end else File.write(output_file, transactions) puts("\nSuccess: ".bold.green + "File '#{output_file}' has been created") end end |
.parse_commands(args = ARGV, _stdin = $stdin) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hledger_forecast/cli.rb', line 19 def self.parse_commands(args = ARGV, _stdin = $stdin) command = nil = {} global = OptionParser.new do |opts| opts. = "Usage: hledger-forecast [command] [options]" opts.separator("") opts.separator("Commands:") opts.separator(" generate Generate a forecast from a file") opts.separator(" summarize Summarize the forecast file and output to the terminal") opts.separator(" compare Compare and highlight the differences between two CSV files") opts.separator("") opts.separator("Options:") opts.on_tail("-h", "--help", "Show this help message") do puts(opts) exit end opts.on_tail("-v", "--version", "Show the installed version") do puts(VERSION) exit end end if args.empty? puts(global) exit(1) end begin global.order!(args) command = args.shift || "generate" rescue OptionParser::InvalidOption => e puts(e) puts(global) exit(1) end case command when "generate" = (args) when "summarize" = (args) when "compare" = (args) else puts("Unknown command: #{command}") puts(global) exit(1) end return command, end |
.parse_compare_options(args) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/hledger_forecast/cli.rb', line 227 def self.(args) = {} global = OptionParser.new do |opts| opts. = "Usage: hledger-forecast compare [path/to/file1.csv] [path/to/file2.csv]" opts.separator("") end begin global.parse!(args) rescue OptionParser::InvalidOption => e puts(e) puts(global) exit(1) end if args[0].nil? || args[1].nil? puts(global) exit(1) end [:file1] = args[0] [:file2] = args[1] end |
.parse_generate_options(args) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/hledger_forecast/cli.rb', line 74 def self.(args) = {} global = OptionParser.new do |opts| opts. = "Usage: hledger-forecast generate [options]" opts.separator("") opts.on( "-f", "--forecast FILE", "The path to the FORECAST csv file to generate from" ) do |file| [:forecast_file] = file [:output_file] ||= file.sub("csv", "journal") end opts.on( "-o", "--output-file FILE", "The path to the OUTPUT file to create" ) do |file| [:output_file] = file end opts.on( "-v", "--verbose", "Do not group transactions in the output file" ) do [:verbose] = true end opts.on( "-t", "--tags TAGS", "Only include transactions with given tags (comma-separated)" ) do || [:tags] = .split(",").map(&:strip) end opts.on( "--force", "Force an overwrite of the output file" ) do [:force] = true end opts.on_tail("-h", "--help", "Show this help message") do puts(opts) exit end end begin global.parse!(args) rescue OptionParser::InvalidOption => e puts(e) puts(global) exit(1) end if .empty? puts(global) exit(1) end [:forecast_file] ||= "forecast.csv" [:file_type] ||= "csv" [:output_file] ||= "forecast.journal" end |
.parse_summarize_options(args) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/hledger_forecast/cli.rb', line 147 def self.(args) = {} global = OptionParser.new do |opts| opts. = "Usage: hledger-forecast summarize [options]" opts.separator("") opts.on( "-f", "--forecast FILE", "The path to the FORECAST csv file to summarize" ) do |file| [:forecast_file] = file end opts .on( "-r", "--roll-up PERIOD", "The period to roll-up your forecasts into. One of:", "[yearly], [half-yearly], [quarterly], [monthly], [weekly], [daily]" ) do |rollup| [:roll_up] = rollup end opts.on( "-t", "--tags TAGS", "Only include transactions with given tags (comma-separated)" ) do || [:tags] = .split(",").map(&:strip) end opts.on( "-v", "--verbose", "Show additional information in the summary" ) do |_| [:verbose] = true end # opts.on("--from DATE", # "Include transactions that start FROM a given DATE [yyyy-mm-dd]") do |from| # options[:from] = from # end # # opts.on("--to DATE", # "Include transactions that run TO a given DATE [yyyy-mm-dd]") do |to| # options[:to] = to # end # opts.on("-s", "--scenario \"NAMES\"", # "Include transactions from given scenarios, e.g.:", # "\"base, rennovation, car purchase\"") do |_scenario| # # Loop through scenarios, seperated by a comma # options[:scenario] = {} # end opts.on_tail("-h", "--help", "Show this help message") do puts(opts) exit end end begin global.parse!(args) rescue OptionParser::InvalidOption => e puts(e) puts(global) exit(1) end if .empty? puts(global) exit(1) end end |
.run(command, options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/hledger_forecast/cli.rb', line 5 def self.run(command, ) case command when "generate" generate() when "summarize" summarize() when "compare" compare() else puts("Unknown command: #{command}") exit(1) end end |
.summarize(options) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/hledger_forecast/cli.rb', line 282 def self.summarize() config = File.read([:forecast_file]) config = HledgerForecast::CSVParser.parse(config) if [:file_type] == "csv" begin summarizer = Summarizer.summarize(config, ) rescue StandardError => e puts("An error occurred while summarizing transactions: #{e.}") exit(1) end puts(SummarizerFormatter.format(summarizer[:output], summarizer[:settings])) end |