Class: SocialSnippet::CommandLine::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/command_line/command.rb

Overview

Usage:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_args, new_streams = {}) ⇒ Command

Returns a new instance of Command


21
22
23
24
25
26
27
28
29
# File 'lib/social_snippet/command_line/command.rb', line 21

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

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args


13
14
15
# File 'lib/social_snippet/command_line/command.rb', line 13

def args
  @args
end

#input_streamObject (readonly)

Returns the value of attribute input_stream


18
19
20
# File 'lib/social_snippet/command_line/command.rb', line 18

def input_stream
  @input_stream
end

#opt_parserObject (readonly)

Returns the value of attribute opt_parser


16
17
18
# File 'lib/social_snippet/command_line/command.rb', line 16

def opt_parser
  @opt_parser
end

#optionsObject (readonly)

Returns the value of attribute options


15
16
17
# File 'lib/social_snippet/command_line/command.rb', line 15

def options
  @options
end

#output_streamObject (readonly)

Returns the value of attribute output_stream


19
20
21
# File 'lib/social_snippet/command_line/command.rb', line 19

def output_stream
  @output_stream
end

#streamsObject (readonly)

Returns the value of attribute streams


17
18
19
# File 'lib/social_snippet/command_line/command.rb', line 17

def streams
  @streams
end

#tokensObject (readonly)

Returns the value of attribute tokens


14
15
16
# File 'lib/social_snippet/command_line/command.rb', line 14

def tokens
  @tokens
end

Instance Method Details

#define_option(name, info = {}) ⇒ Object

Define an option

Parameters:

  • name (Symbol)

    :hello_world => “–hello-world”

  • info (:type) (defaults to: {})

    :flag or :string

  • info (:default) (defaults to: {})
    Any

    default value

  • info (:short) (defaults to: {})
    Boolean

    enable short flag


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/social_snippet/command_line/command.rb', line 41

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_optionsObject


58
59
60
# File 'lib/social_snippet/command_line/command.rb', line 58

def define_options
  raise "not implement"
end

#initObject


62
63
64
65
66
67
68
# File 'lib/social_snippet/command_line/command.rb', line 62

def init
  init_version
  init_banner
  define_options
  parse_line_options
  @tokens = args
end

#runObject


70
71
72
# File 'lib/social_snippet/command_line/command.rb', line 70

def run
  raise "not implement"
end

#social_snippetObject


31
32
33
# File 'lib/social_snippet/command_line/command.rb', line 31

def social_snippet
  @social_snippet ||= ::SocialSnippet::Core.new(input_stream, output_stream)
end