Class: Imapcli::OptionValidator
- Inherits:
-
Object
- Object
- Imapcli::OptionValidator
- Defined in:
- lib/imapcli/option_validator.rb
Overview
Utility class to validate user options
Invalid options will trigger a runtime exception (which is handled by GLI).
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #global_options_valid?(global_options) ⇒ Boolean
-
#initialize ⇒ OptionValidator
constructor
A new instance of OptionValidator.
- #pass? ⇒ Boolean
-
#stats_options_valid?(options, args) ⇒ true false
Validates options for the stats command.
- #warnings? ⇒ Boolean
Constructor Details
#initialize ⇒ OptionValidator
Returns a new instance of OptionValidator.
8 9 10 11 |
# File 'lib/imapcli/option_validator.rb', line 8 def initialize @errors = [] @warnings = [] end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/imapcli/option_validator.rb', line 6 def errors @errors end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/imapcli/option_validator.rb', line 6 def @options end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
6 7 8 |
# File 'lib/imapcli/option_validator.rb', line 6 def warnings @warnings end |
Instance Method Details
#global_options_valid?(global_options) ⇒ Boolean
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/imapcli/option_validator.rb', line 13 def () if [:s].nil? || [:s].empty? @errors << 'missing server name (use -s option or set IMAP_SERVER environment variable)' end if [:u].nil? || [:u].empty? @errors << 'missing user name (use -u option or set IMAP_USER environment variable)' end if [:P] && [:p] @errors << '-p and -P options do not agree' end pass? end |
#pass? ⇒ Boolean
61 62 63 |
# File 'lib/imapcli/option_validator.rb', line 61 def pass? @errors.empty? end |
#stats_options_valid?(options, args) ⇒ true false
Validates options for the stats command.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/imapcli/option_validator.rb', line 30 def (, args) @options = {} raise 'incompatible options -r/--recurse and -R/--no_recurse' if [:r] && [:R] if [:recurse] if args.empty? @warnings << 'warning: superfluous -r/--recurse option; will recurse from root by default' else @options[:depth] = -1 end elsif [:no_recurse] if args.empty? @options[:depth] = 0 else @warnings << 'warning: superfluous -R/--no_recurse option; will not recurse from non-root mailbox by default' end end if [:sort] = %w(count total_size min_size q1 median_size q3 max_size) if .include? [:sort].downcase @options[:sort] = [:sort].to_sym else # GLI did not print the available options even with the :must_match option @errors << "sort option must be one of: #{.join(', ')}" end end pass? end |
#warnings? ⇒ Boolean
65 66 67 |
# File 'lib/imapcli/option_validator.rb', line 65 def warnings? @warnings.count > 0 end |