Skip to content

Instantly share code, notes, and snippets.

@nixpulvis
Last active November 9, 2017 17:50
Show Gist options
  • Save nixpulvis/3991999 to your computer and use it in GitHub Desktop.
Save nixpulvis/3991999 to your computer and use it in GitHub Desktop.
CLI class that doesn't suck
class CLI
private
attr_accessor :flags
# Sexy Options #
################
def self.parse_flags
ARGV.each_with_index do |arg, i|
if arg[0] == "-"
begin
@flags[arg[1..-1]].call
rescue NoMethodError
puts "#{arg} is not a valid flag".red
exit
end
ARGV.delete arg
end
end
end
# Sexy and Simple Command Parsing #
###################################
def self.parse_commands
ARGV[0] ||= "help"
command = ARGV[0].to_sym
if instance_methods(false).include? command
begin
self.new.public_send command, *ARGV.drop(1)
rescue ArgumentError => e
puts "#{command}: #{e.message}".red
end
else
puts "jarvis: #{command} is not a command.".red
end
end
def self.start
parse_flags
parse_commands
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment