Class: Warp::Dir::App::CLI

Inherits:
Object show all
Defined in:
lib/warp/dir/app/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/warp/dir/app/cli.rb', line 12

def initialize(argv)
  self.argv      = argv

  # flags are everything that follows -- and is typically flags for the command.
  # for example: 'wd ls project-point -- -alF' would extract flags = [ '-alF' ]
  self.flags     = extract_suffix_flags(argv.dup)
  self.flags.flatten!
  self.commander = ::Warp::Dir::Commander.instance
  self.config    = ::Warp::Dir::Config.new
  self.opts      = nil
end

Instance Attribute Details

#argvObject

Returns the value of attribute argv.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def argv
  @argv
end

#commanderObject

Returns the value of attribute commander.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def commander
  @commander
end

#configObject

Returns the value of attribute config.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def config
  @config
end

#flagsObject

Returns the value of attribute flags.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def flags
  @flags
end

#optsObject

Returns the value of attribute opts.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def opts
  @opts
end

#storeObject

Returns the value of attribute store.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def store
  @store
end

#validatedObject

Returns the value of attribute validated.



10
11
12
# File 'lib/warp/dir/app/cli.rb', line 10

def validated
  @validated
end

Instance Method Details

#run(&block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/warp/dir/app/cli.rb', line 50

def run(&block)
  validate unless validated?
  response = process_command
  yield response if block_given?
  response
rescue Exception => e
  if config.debug
    STDERR.puts(e.inspect)
    STDERR.puts(e.backtrace.join("\n"))
  end
  on :error do
    message e.message.red
  end
end

#validate {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/warp/dir/app/cli.rb', line 24

def validate
  self.validated = false
  no_arguments   = argv.empty?
  commands       = shift_non_flag_commands
  self.opts      = parse_with_slop(self.argv)
  # merge first few non-flag args which we filtered into `commands` hash
  # merge onto the hash resulting from options
  # and pass on to override the default config opts for the final config.
  config.configure(opts.to_hash.merge(commands))

  self.store     = Warp::Dir::Store.new(config, Warp::Dir::Serializer::Dotfile)

  config.command = :help if (opts.help? || no_arguments)
  config.command = :warp if config.warp || config.command == 'warp'

  String.disable_colors if config.no_color

  if config[:command]
    self.validated = true
  else
    raise Warp::Dir::Errors::InvalidCommand.new('Unable to determine what command to run.'.red)
  end
  yield self if block_given?
  validated
end