Class: Stamina::Command::StaminaCommand
- Inherits:
-
Object
- Object
- Stamina::Command::StaminaCommand
- Defined in:
- lib/stamina/command/stamina_command.rb
Overview
Helper to create stamina commands
Direct Known Subclasses
Adl2DotCommand, ClassifyCommand, RPNICommand, RedBlueCommand
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Command description.
-
#name ⇒ Object
readonly
Command name.
-
#usage ⇒ Object
readonly
Command usage.
Instance Method Summary collapse
-
#assert_readable_file(file) ⇒ Object
Checks that a given file is readable or raises an ArgumentError.
-
#assert_writable_file(file) ⇒ Object
Checks that a given file is writable or raises an ArgumentError.
-
#initialize(name, usage, description) ⇒ StaminaCommand
constructor
Creates a command with a name, usage and description.
-
#options(&block) ⇒ Object
Creates options.
-
#parse(argv, *variables) ⇒ Object
Parses arguments and install last argument as instance variables.
-
#show_usage(and_exit = true) ⇒ Object
Prints usage (and optionnaly exits).
Constructor Details
#initialize(name, usage, description) ⇒ StaminaCommand
Creates a command with a name, usage and description
19 20 21 22 23 |
# File 'lib/stamina/command/stamina_command.rb', line 19 def initialize(name, usage, description) @name = name @usage = usage @description = description end |
Instance Attribute Details
#description ⇒ Object (readonly)
Command description
13 14 15 |
# File 'lib/stamina/command/stamina_command.rb', line 13 def description @description end |
#name ⇒ Object (readonly)
Command name
10 11 12 |
# File 'lib/stamina/command/stamina_command.rb', line 10 def name @name end |
#usage ⇒ Object (readonly)
Command usage
16 17 18 |
# File 'lib/stamina/command/stamina_command.rb', line 16 def usage @usage end |
Instance Method Details
#assert_readable_file(file) ⇒ Object
Checks that a given file is readable or raises an ArgumentError
52 53 54 55 |
# File 'lib/stamina/command/stamina_command.rb', line 52 def assert_readable_file(file) raise ArgumentError, "File #{file} does not exists" unless File.exists?(file) raise ArgumentError, "File #{file} cannot be read" unless File.readable?(file) end |
#assert_writable_file(file) ⇒ Object
Checks that a given file is writable or raises an ArgumentError
58 59 60 61 |
# File 'lib/stamina/command/stamina_command.rb', line 58 def assert_writable_file(file) raise ArgumentError, "File #{file} cannot be written" \ unless not(File.exists?(file)) or File.writable?(file) end |
#options(&block) ⇒ Object
Creates options
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/stamina/command/stamina_command.rb', line 26 def (&block) OptionParser.new do |opt| opt.program_name = name opt.version = Stamina::VERSION opt.release = nil opt.summary_indent = ' ' * 4 = <<-EOF # usage: #{opt.program_name} #{usage} # #{description} EOF opt. = .gsub(/[ \t]+# /, "") block.call(opt) if block opt.on_tail("-h", "--help", "Show this message") do puts opt exit end end end |
#parse(argv, *variables) ⇒ Object
Parses arguments and install last argument as instance variables
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/stamina/command/stamina_command.rb', line 64 def parse(argv, *variables) rest = .parse(argv) show_usage(true) unless rest.size==variables.size variables.each_with_index do |var,i| self.send("#{var}=".to_sym, rest[i]) end rescue ArgumentError => ex puts ex. puts show_usage(true) end |
#show_usage(and_exit = true) ⇒ Object
Prints usage (and optionnaly exits)
46 47 48 49 |
# File 'lib/stamina/command/stamina_command.rb', line 46 def show_usage(and_exit=true) puts Kernel.exit if and_exit end |