Class: Megar::Shell
- Inherits:
-
Object
- Object
- Megar::Shell
- Defined in:
- lib/megar/shell.rb
Overview
class that groks the megar command line options and invokes the required task
Constant Summary collapse
- OPTIONS =
defines the valid command line options
%w(help verbose email=s password=s)
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
holds the remaining command line arguments.
-
#options ⇒ Object
readonly
holds the parsed options.
Class Method Summary collapse
-
.usage ⇒ Object
prints usage/help information.
Instance Method Summary collapse
-
#email ⇒ Object
Option shortcuts.
-
#initialize(options, args) ⇒ Shell
constructor
initializes the shell with command line argments:.
- #password ⇒ Object
-
#run ⇒ Object
Command: execute the megar task according to the options provided on initialisation.
- #session ⇒ Object
-
#usage ⇒ Object
prints usage/help information.
Constructor Details
#initialize(options, args) ⇒ Shell
initializes the shell with command line argments:
options
is expected to be the hash structure as provided by GetOptions.new(..)
args
is the remaining command line arguments
18 19 20 21 |
# File 'lib/megar/shell.rb', line 18 def initialize(,args) @options = (||{}).each{|k,v| {k => v} } @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
holds the remaining command line arguments
10 11 12 |
# File 'lib/megar/shell.rb', line 10 def args @args end |
#options ⇒ Object (readonly)
holds the parsed options
7 8 9 |
# File 'lib/megar/shell.rb', line 7 def @options end |
Class Method Details
.usage ⇒ Object
prints usage/help information
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/megar/shell.rb', line 47 def usage $stderr.puts <<-EOS Megar v#{Megar::VERSION} =================================== Usage: megar [options] [commands] Options: -h | --help : shows command help -v | --verbose : run with verbose -e= | --email=value : email address for login -p= | --password=value : password for login Commands: (none) : will perform a basic connection test only ls : returns a full file listing Examples: megar [email protected] --password=MyPassword ls megar -e [email protected] -p MyPassword ls EOS end |
Instance Method Details
#email ⇒ Object
Option shortcuts
84 |
# File 'lib/megar/shell.rb', line 84 def email ; [:email] ; end |
#password ⇒ Object
85 |
# File 'lib/megar/shell.rb', line 85 def password ; [:password] ; end |
#run ⇒ Object
Command: execute the megar task according to the options provided on initialisation
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/megar/shell.rb', line 24 def run if email && password $stderr.puts "Connecting to mega as #{email}.." raise "Failed to connect!" unless session.connected? case args.first when /ls/i session.files.each do |file| puts file end else $stderr.puts "Connected!" end else usage end end |
#session ⇒ Object
79 80 81 |
# File 'lib/megar/shell.rb', line 79 def session @session ||= Megar::Session.new(email: email, password: password) end |
#usage ⇒ Object
prints usage/help information
75 76 77 |
# File 'lib/megar/shell.rb', line 75 def usage self.class.usage end |