class SocialSnippet::CommandLine::Command

Usage:

class SomeCommand < Command; end cli = SomeCommand.new [arg1, arg2, …], stream_opts cli.init cli.run

Attributes

args[R]
input_stream[R]
opt_parser[R]
options[R]
output_stream[R]
streams[R]
tokens[R]

Public Class Methods

new(new_args, new_streams = {}) click to toggle source
# File lib/social_snippet/command_line/command.rb, line 19
def initialize(new_args, new_streams = {})
  @streams        = new_streams
  @input_stream   = streams[:input_stream]  || STDIN
  @output_stream  = streams[:output_stream] || STDOUT
  @args = new_args.clone
  @options = {}
  @tokens = [] # init after parse options
  @opt_parser = ::OptionParser.new
end

Public Instance Methods

core() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 29
def core
  @core ||= ::SocialSnippet::Core.new(input_stream, output_stream)
end
define_option(name, info = {}) click to toggle source

Define an option

@param name [Symbol] :hello_world => “–hello-world” @param info :flag or :string @param info [Any] default value @param info [Boolean] enable short flag

# File lib/social_snippet/command_line/command.rb, line 39
def define_option(name, info = {})
  info[:type] ||= :string
  info[:default] ||= nil
  info[:short] ||= false
  long_opt = to_long_option(name, info)
  options[name] = info[:default]
  if info[:short]
    opt_parser.on to_short_option(name, info), long_opt do |v|
      options[name] = v
    end
  else
    opt_parser.on long_opt do |v|
      options[name] = v
    end
  end
end
define_options() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 56
def define_options
  raise "not implement"
end
init() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 60
def init
  init_version
  init_banner
  define_options
  parse_line_options
  @tokens = args
  core.api.on :message do |message|
    core.logger.say message
  end
end
run() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 71
def run
  raise "not implement"
end

Private Instance Methods

has_next_token?() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 155
def has_next_token?
  not tokens.empty?
end
has_subcommand?() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 144
def has_subcommand?
  return false if args.empty?
  return false if args[0].start_with?("-")
  return true
end
help() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 77
def help
  opt_parser.parse ["--help"]
end
init_banner() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 81
def init_banner
  opt_parser.banner = usage
end
init_version() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 89
def init_version
  opt_parser.version = ::SocialSnippet::VERSION
end
is_line_option?(s) click to toggle source
# File lib/social_snippet/command_line/command.rb, line 134
def is_line_option?(s)
  return true if /^-[a-zA-Z0-9]$/ === s
  return true if /^--/ === s
  return false
end
is_not_line_option?(s) click to toggle source
# File lib/social_snippet/command_line/command.rb, line 140
def is_not_line_option?(s)
  is_line_option?(s) === false
end
last_line_option_index() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 118
def last_line_option_index
  args.index do |arg|
    is_not_line_option?(arg)
  end
end
next_token() click to toggle source
–opt1, –opt2, token1, token2

> token1

# File lib/social_snippet/command_line/command.rb, line 151
def next_token
  tokens.shift
end
parse_line_options() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 107
def parse_line_options
  return if args.empty?
  last_ind = last_line_option_index
  if last_ind.nil?
    parsed = args.clone
  else
    parsed = args[0 .. last_ind]
  end
  @args = opt_parser.parse(parsed).concat(args[last_ind + 1..-1])
end
to_command_class_sym(s) click to toggle source

hello -> HelloCommand

# File lib/social_snippet/command_line/command.rb, line 125
def to_command_class_sym(s)
  "#{s.capitalize}Command".to_sym
end
to_command_name(sym) click to toggle source

:HelloCommand -> hello

# File lib/social_snippet/command_line/command.rb, line 130
def to_command_name(sym)
  sym.to_s.gsub(/Command$/, '').downcase
end
to_long_option(sym, info = {}) click to toggle source
# File lib/social_snippet/command_line/command.rb, line 93
def to_long_option(sym, info = {})
  if info[:type] == :flag
    "--[no-]#{sym.to_s.gsub("_", "-")}"
  elsif info[:type] == :string
    "--#{sym.to_s.gsub("_", "-")} value"
  else
    "--#{sym.to_s.gsub("_", "-")}"
  end
end
to_short_option(sym, info = {}) click to toggle source
# File lib/social_snippet/command_line/command.rb, line 103
def to_short_option(sym, info = {})
  "-#{sym.to_s[0]}"
end
usage() click to toggle source
# File lib/social_snippet/command_line/command.rb, line 85
def usage
  raise "not implement"
end