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 }],
  record_id: ['Specific Record ID', { type: :integer, short: :I }],
  uuid: ['Record UUID', { type: :string }],

  # 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 -- #{'STDIN'.red} takes priority", { 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, or one of the following preset options: ' \
    "`#{'local'.yellow}`, " \
    "`#{'local_dot_config'.yellow}`, " \
    "`#{'local_config'.yellow}`, " \
    "`#{'user'.yellow}`, " \
    "`#{'system'.yellow}`. " \
    'If no option is provided, the first matching preset option will be automatically used.',
    { type: :string, short: :C }
  ],
  show_config: ['Return the active config file path', { short: :X }],
  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"],
  jsonl: ['Format array output as JSONL', { short: :j }],
  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 uuid],
  %i[record_id uuid],
  %i[on panel query mapper map business_objects auth_token show_config],
  %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.



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

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.



84
85
86
# File 'lib/usps/imis/command_line/options_parser.rb', line 84

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



84
85
86
# File 'lib/usps/imis/command_line/options_parser.rb', line 84

def options
  @options
end

Class Method Details



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

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


86
87
88
89
90
91
# File 'lib/usps/imis/command_line/options_parser.rb', line 86

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