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
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[quiet log_level],
  %i[quiet log],

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

  *(SOLO_FLAGS + %i[mapper query map field fields certificate]).map { [:create, it] },
  *(SOLO_FLAGS + %i[mapper query map field fields certificate data raw]).map { [:delete, it] },
  *(SOLO_FLAGS + %i[mapper query 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.



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
# File 'lib/usps/imis/command_line/options_parser.rb', line 75

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?

  # :nocov:
  @options[:data] = read_stdin if stdin?
  # :nocov:

  @options[:data] = JSON.parse(@options[:data]) if @options[:data]

  # Support shorthand for setting `certificate` param on queries
  return unless @options[:query] && @options[:certificate]

  @options[:data] ||= {}
  @options[:data]['certificate'] = @options.delete(:certificate)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



29
30
31
# File 'lib/usps/imis/command_line/options_parser.rb', line 29

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/usps/imis/command_line/options_parser.rb', line 29

def options
  @options
end

Class Method Details



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

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


49
50
51
52
53
54
# File 'lib/usps/imis/command_line/options_parser.rb', line 49

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

.optionsObject



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

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