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

OPTIONS =
{
  # IDs
  certificate: ['Member certificate number', { type: :string }],
  id: ['Member iMIS ID', { type: :integer }],

  # Primary interactions
  on: ['Business Object name', { type: :string }],
  panel: ['Panel name', { type: :string }],
  query: ['IQA Query or Business Object name to query', { type: :string, short: :Q }],
  mapper: ['Interact with mapped fields', { short: :M }],
  map: ["Shorthand for #{'-Mf'.green} to access a single mapped field", { type: :string }],
  business_objects: ['List available Business Objects'],

  # Alternate verbs
  create: ["Send a #{'POST'.cyan} request", { short: :P }],
  delete: ["Send a #{'DELETE'.cyan} request", { short: :D }],

  # Data
  ordinal: ['Ordinal ID within a Panel', { type: :integer }],
  field: ['Specific field to return or update', { type: :string }],
  fields: ['Specific field(s) to return', { type: :strings, short: :F }],
  data: ['JSON string input', { type: :string }],

  # Iteractions for supporting other language wrappers
  auth_token: ['Return an auth token for other language wrappers', { short: :T }],
  token: ['Provide an existing auth token', { type: :string }],

  # General config
  config: ['Path to the JSON/YAML config file to use', { type: :string, short: :C }],
  raw: ['Return raw JSON output, rather than simplified data', { short: :R }],
  include_ids: ["Include any #{'iMIS ID'.yellow} and #{'Ordinal'.yellow} properties in returned data"],
  quiet: ["Suppress logging to #{'STDERR'.red}"],
  log: ["Redirect logging to #{'STDOUT'.red}"],
  log_level: ['Set the logging level', { type: :string, default: 'info', short: :L }]
}.freeze
CONFLICTING_OPTION_GROUPS =
[
  %i[certificate id],
  %i[on panel query mapper map business_objects auth_token],
  %i[field fields map query],
  %i[raw include_ids],
  %i[quiet log_level],
  %i[quiet log],

  %i[create delete],

  %i[create mapper],
  %i[create query],
  %i[create map],
  %i[create field],
  %i[create fields],

  %i[delete mapper],
  %i[delete query],
  %i[delete map],
  %i[delete field],
  %i[delete fields],
  %i[delete data],
  %i[delete raw]
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOptionsParser

Returns a new instance of OptionsParser.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/usps/imis/command_line/options_parser.rb', line 98

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

  Optimist.educate if ARGV.empty? && defaults? # DEV: This shadows setting the --version flag by default

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

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



70
71
72
# File 'lib/usps/imis/command_line/options_parser.rb', line 70

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



70
71
72
# File 'lib/usps/imis/command_line/options_parser.rb', line 70

def options
  @options
end

Class Method Details



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

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


72
73
74
75
76
77
# File 'lib/usps/imis/command_line/options_parser.rb', line 72

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