Class: Hawknee::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/hawknee/cli.rb

Overview

You know.. Command Line Interface.. Got it? :)

Defined Under Namespace

Modules: Command

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cli

Initialize new Cli, and.. here we go!

some enlightening:

@command        - ARGV.first
@options        - simple opts handler


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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hawknee/cli.rb', line 30

def initialize(*args)
	
	# Handles options
	@options = {}
	
	# Set up the command and subcommand
	@command, @subcommand = parse_command
	
	begin
		opts = OptionParser.new do |opts|
			opts.banner = "Usage: hawknee COMMAND [OPTIONS]"
			
			opts.separator " "
			
			opts.separator "Installing Hawknee:"
			opts.separator "    rails new app_name                 # Make new Rails application"
			opts.separator "    cd app_name                        # Switch to the app's directory"
			opts.separator "    gedit Gemfile                      # Add: gem 'hawknee', '#{Hawknee::Version::STRING}'"
			opts.separator "    rails generate hawknee:install     # Copies needed files"
			opts.separator "    rails server                       # Runs the server"
			opts.separator "    go to http://localhost:3000/forum"
			opts.separator " "
			opts.separator "    Have fun!"
			
			opts.separator " "
			
			opts.separator "Common options:"
			opts.on('-v', '--version', 'Show version') do
				puts "Hawknee " + Hawknee::Version::STRING
				exit
			end
			
			opts.on('-h', '--help', 'Display this message') do
				puts opts
				exit
			end
		end.parse!
	rescue OptionParser::InvalidOption => e
		raise BadOption
	end
	
	# Here, in Hawknee, commands are simply classes, (kept in files in 'commands' directory) that inherits from Hawknee::Cli::Command.
	# To see it in action, dig a bit in lib/commands/new.rb file
	# 
	begin
		# We make sure the command (class) exists.
		run if command_exists?
	# When user typed some class (command) that doesn't exists.
	rescue BadCommand => e
		puts e.message
	# When typed ^C
	rescue Interrupt
		puts "\n[" + "interrupted".bold.colorize(:red) + "]"
	end
	
end

Instance Method Details

#parse_commandObject

run



95
96
97
98
99
100
101
102
103
104
# File 'lib/hawknee/cli.rb', line 95

def parse_command
	case ARGV.length
		when 1
			return ARGV.first.to_s, 'init'
		when 2
			if ARGV[1] != nil
				return ARGV.first.to_s, ARGV[1].to_s
			end
	end
end

#runObject

initialize



87
88
89
90
91
92
93
# File 'lib/hawknee/cli.rb', line 87

def run
	begin
		eval "Hawknee::Cli::Command::#{@command.capitalize}.new.#{@subcommand}"
	rescue NameError, NoMethodError
		raise BadCommand
	end
end