Class: RockBooks::Main
- Inherits:
-
Object
- Object
- RockBooks::Main
- Defined in:
- lib/rock_books/cmd_line/main.rb
Instance Method Summary collapse
- #call ⇒ Object
- #options_with_defaults ⇒ Object
-
#parse_command_line ⇒ Object
Parses the command line with Ruby’s internal ‘optparse’.
- #prepend_environment_options ⇒ Object
Instance Method Details
#call ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rock_books/cmd_line/main.rb', line 84 def call begin ARGV << '-h' if ARGV.empty? = parse_command_line CommandLineInterface.new().call rescue => error $stderr.puts \ <<~HEREDOC #{error.backtrace.join("\n")} #{'-' * 79} #{error} #{'-' * 79} HEREDOC exit(-1) binding.pry raise error end end |
#options_with_defaults ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/rock_books/cmd_line/main.rb', line 15 def = OpenStruct.new .input_dir = DEFAULT_INPUT_DIR .output_dir = DEFAULT_OUTPUT_DIR .receipt_dir = DEFAULT_RECEIPT_DIR .do_receipts = true end |
#parse_command_line ⇒ Object
Parses the command line with Ruby’s internal ‘optparse’. OptionParser#parse! removes what it processes from ARGV, which simplifies our command parsing.
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 |
# File 'lib/rock_books/cmd_line/main.rb', line 36 def parse_command_line = OptionParser.new do |parser| parser.on("-h", "--help", "Show help") do |_help_requested| ARGV << 'h' # pass on the request to the command processor .suppress_command_line_validation = true end parser.on('-i', '--input_dir DIR', "Input directory containing source data files, default: '#{DEFAULT_INPUT_DIR}'") do |v| .input_dir = File.(v) end parser.on('-o', '--output_dir DIR', "Output directory to which report files will be written, default: '#{DEFAULT_OUTPUT_DIR}'") do |v| .output_dir = File.(v) end parser.on('-r', '--receipt_dir DIR', "Directory root from which to find receipt filespecs, default: '#{DEFAULT_RECEIPT_DIR}'") do |v| .receipt_dir = File.(v) end parser.on('-s', '--shell', 'Start interactive shell') do |v| .interactive_mode = true end parser.on('-v', '--[no-]verbose', 'Verbose mode') do |v| .verbose_mode = v end parser.on('', '--[no-]receipts', 'Include report on existing and missing receipts.') do |v| .do_receipts = v end end.parse! if .verbose_mode puts "Run Options:" ap .to_h end end |
#prepend_environment_options ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/rock_books/cmd_line/main.rb', line 25 def env_opt_string = ENV['ROCKBOOKS_OPTIONS'] if env_opt_string args_to_prepend = Shellwords.shellsplit(env_opt_string) ARGV.unshift(args_to_prepend).flatten! end end |