Class: Reckon::Options
- Inherits:
-
Object
- Object
- Reckon::Options
- Defined in:
- lib/reckon/options.rb
Overview
Singleton class for parsing command line flags
Class Method Summary collapse
Class Method Details
.parse_command_line_options(args = ARGV, stdin = $stdin) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 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 73 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 146 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 |
# File 'lib/reckon/options.rb', line 6 def self.(args = ARGV, stdin = $stdin) cli = HighLine.new = { output_file: $stdout } OptionParser.new do |opts| opts. = "Usage: Reckon.rb [options]" opts.separator "" opts.on("-f", "--file FILE", "The CSV file to parse") do |file| [:file] = file end opts.on("-a", "--account NAME", "The Ledger Account this file is for") do |a| [:bank_account] = a end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| [:verbose] = v end opts.on("-i", "--inverse", "Use the negative of each amount") do |v| [:inverse] = v end opts.on("-p", "--print-table", "Print out the parsed CSV in table form") do |p| [:print_table] = p end opts.on("-o", "--output-file FILE", "The ledger file to append to") do |o| [:output_file] = File.open(o, 'a') end opts.on("-l", "--learn-from FILE", "An existing ledger file to learn accounts from") do |l| [:existing_ledger_file] = l end opts.on("", "--ignore-columns 1,2,5", "Columns to ignore, starts from 1") do |ignore| [:ignore_columns] = ignore.split(",").map(&:to_i) end opts.on("", "--money-column 2", Integer, "Column number of the money column, starts from 1") do |col| [:money_column] = col end opts.on("", "--money-columns 2,3", "Column number of the money columns, starts from 1 (1 or 2 columns)") do |ignore| [:money_columns] = ignore.split(",").map(&:to_i) end opts.on("", "--raw-money", "Don't format money column (for stocks)") do |n| [:raw] = n end opts.on("", "--date-column 3", Integer, "Column number of the date column, starts from 1") do |col| [:date_column] = col end opts.on("", "--contains-header [N]", Integer, "Skip N header rows - default 1") do |hdr| [:contains_header] = 1 [:contains_header] = hdr.to_i end opts.on("", "--contains-footer [N]", Integer, "Skip N footer rows - default 0") do |hdr| [:contains_footer] = hdr.to_i || 0 end opts.on("", "--csv-separator ','", "CSV separator (default ',')") do |sep| [:csv_separator] = sep end opts.on("", "--comma-separates-cents", "Use comma to separate cents ($100,50 vs. $100.50)") do |c| [:comma_separates_cents] = c end opts.on("", "--encoding 'UTF-8'", "Specify an encoding for the CSV file") do |e| [:encoding] = e end opts.on("-c", "--currency '$'", "Currency symbol to use - default $ (ex £, EUR)") do |e| [:currency] = e || '$' end opts.on("", "--date-format FORMAT", "CSV file date format (see `date` for format)") do |d| [:date_format] = d end opts.on("", "--ledger-date-format FORMAT", "Ledger date format (see `date` for format)") do |d| [:ledger_date_format] = d end opts.on("-u", "--unattended", "Don't ask questions and guess all the accounts automatically. Use with --learn-from or --account-tokens options.") do |n| [:unattended] = n end opts.on("-t", "--account-tokens FILE", "YAML file with manually-assigned tokens for each account (see README)") do |a| [:account_tokens_file] = a end opts.on("", "--table-output-file FILE") do |n| [:table_output_file] = n end [:default_into_account] = 'Expenses:Unknown' opts.on("", "--default-into-account NAME", "Default into account") do |a| [:default_into_account] = a end [:default_outof_account] = 'Income:Unknown' opts.on("", "--default-outof-account NAME", "Default 'out of' account") do |a| [:default_outof_account] = a end opts.on("", "--fail-on-unknown-account", "Fail on unmatched transactions.") do |n| [:fail_on_unknown_account] = n end opts.on("", "--suffixed", "Append currency symbol as a suffix.") do |e| [:suffixed] = e end opts.on("", "--ledger-format FORMAT", "Output/Learn format: BEANCOUNT or LEDGER. Default: LEDGER") do |n| [:format] = n end opts.on_tail("-h", "--help", "Show this message") do puts opts exit end opts.on_tail("--version", "Show version") do puts VERSION exit end opts.parse!(args) end if [:file] == '-' unless [:unattended] raise "--unattended is required to use STDIN as CSV source." end [:string] = stdin.read end unless [:file] [:file] = cli.ask("What CSV file should I parse? ") unless [:file].empty? puts "\nYou must provide a CSV file to parse.\n" puts parser exit end end unless [:bank_account] raise "Must specify --account in unattended mode" if [:unattended] [:bank_account] = cli.ask("What is this account named in Ledger?\n") do |q| q.readline = true q.validate = /^.{2,}$/ q.default = "Assets:Bank:Checking" end end return end |