Class: Usps::Imis::CommandLine::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/usps/imis/command_line/options_parser.rb

Overview

Command line options parser

Constant Summary collapse

SOLO_FLAGS =
%i[show_config auth_token business_objects].freeze
QUERY_FLAGS =
%i[query page page_size offset].freeze
CONFLICTING_OPTION_GROUPS =
[
  %i[certificate id uuid],
  %i[record_id uuid],
  %i[on panel query mapper map] + SOLO_FLAGS,
  %i[field fields map query],
  %i[raw include_ids],
  %i[raw jsonl csv],
  %i[quiet log_level],
  %i[quiet log],
  %i[page offset],

  %i[create delete],
  %i[create ordinal],
  %i[data fields],

  *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map field fields certificate]).map { [:create, it] },
  *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map field fields certificate data raw]).map { [:delete, it] },
  *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map on]).map { [:ordinal, it] },
  *(SOLO_FLAGS + %i[certificate]).map { [:data, it] }
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionsParser

Returns a new instance of OptionsParser.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/usps/imis/command_line/options_parser.rb', line 78

def initialize
  @options = parse_options.compact
  @arguments = ARGV # Not currently used

  # Set the default behavior to --help instead of --version
  #
  # Passing any subcommands as arguments will cause this conditional to fail
  #
  # If this is disabled:
  #
  #   - The default behavior is to print just the version label to the terminal
  #   - CommandLine::Performers#perform! *must* expect a `version:` pattern,
  #     and will return a quoted string of the version label
  #
  Optimist.educate if ARGV.empty? && defaults?

  pre_process_options!
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



32
33
34
# File 'lib/usps/imis/command_line/options_parser.rb', line 32

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/usps/imis/command_line/options_parser.rb', line 32

def options
  @options
end

Class Method Details



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/usps/imis/command_line/options_parser.rb', line 59

def self.banner_contents
  <<~BANNER
    #{'Usage'.underline}

      #{'imis'.bold} #{'[options]'.gray}


    #{'Further Help'.underline}

      For an explanation of how to provide API configuration, more details on the options,
      and usage examples, please refer to the wiki:

      https://github.com/unitedstatespowersquadrons/imis-api-ruby/wiki/Command-Line


    #{'Options'.underline}
  BANNER
end


52
53
54
55
56
57
# File 'lib/usps/imis/command_line/options_parser.rb', line 52

def self.banner_header(version)
  <<~BANNER
    #{version.bold.blue}
    #{'P/R/C Julian Fiander, SN'.gray}\n \n
  BANNER
end

.optionsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/usps/imis/command_line/options_parser.rb', line 34

def self.options
  return @options if @options

  raw_yaml_erb = File.read("#{File.dirname(__FILE__)}/options.yml.erb")
  rendered_yaml = ERB.new(raw_yaml_erb).result.gsub("\x1b", '\u001b')
  options = YAML.safe_load(rendered_yaml)

  @options = options.transform_keys(&:to_sym).transform_values do |description, details|
    next [description] if details.nil?

    details = details.transform_keys(&:to_sym).each_with_object({}) do |(key, value), hash|
      hash[key] = key == :default ? value : value.to_sym
    end

    [description, details]
  end
end