Class: Ncn::CliParser
- Inherits:
-
Object
- Object
- Ncn::CliParser
- Defined in:
- lib/ncn/cli_parser.rb
Constant Summary collapse
- COMMON_OPTIONS =
[ ["--help", "-h", GetoptLong::NO_ARGUMENT], ["--tags", "-t", GetoptLong::REQUIRED_ARGUMENT], ["--path", GetoptLong::REQUIRED_ARGUMENT], ["--number", "-n", GetoptLong::REQUIRED_ARGUMENT] ].freeze
- NORMALIZERS =
{ "--tags" => ->(value) { value.to_s.split(",").map(&:strip).uniq(&:downcase) }, "--number" => ->(value) { value.match?(/^\d+$/) ? Integer(value) : nil } }.freeze
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize ⇒ CliParser
constructor
A new instance of CliParser.
Constructor Details
#initialize ⇒ CliParser
Returns a new instance of CliParser.
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ncn/cli_parser.rb', line 17 def initialize = GetoptLong.new(*COMMON_OPTIONS) @options = {}.tap do |result| .each do |option, argument| result[option] = NORMALIZERS.key?(option) ? NORMALIZERS[option].call(argument) : argument end end @command, *@arguments = ARGV end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
3 4 5 |
# File 'lib/ncn/cli_parser.rb', line 3 def arguments @arguments end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
3 4 5 |
# File 'lib/ncn/cli_parser.rb', line 3 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/ncn/cli_parser.rb', line 3 def @options end |