Class: GitStyleBinary::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/git-style-binary/command.rb

Direct Known Subclasses

Primary

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(o = {}) ⇒ Command

Returns a new instance of Command.



55
56
57
58
59
# File 'lib/git-style-binary/command.rb', line 55

def initialize(o={})
  o.each do |k,v|
    eval "@#{k.to_s}= v"
  end
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



51
52
53
# File 'lib/git-style-binary/command.rb', line 51

def constraints
  @constraints
end

#nameObject

Returns the value of attribute name.



53
54
55
# File 'lib/git-style-binary/command.rb', line 53

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



52
53
54
# File 'lib/git-style-binary/command.rb', line 52

def opts
  @opts
end

Class Method Details

.defaultsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/git-style-binary/command.rb', line 31

def defaults
  lambda do
    name_desc "#{command.full_name}\#{command.short_desc ? ' - ' + command.short_desc : ''}" # eval jit
    version_string = defined?(VERSION) ? VERSION : "0.0.1"
    version "#{version_string} (c) #{Time.now.year}"
    banner <<-EOS
#{"SYNOPSIS".colorize(:red)}
#{command.full_name.colorize(:light_blue)} #{all_options_string} COMMAND [ARGS]

#{"SUBCOMMANDS".colorize(:red)}
   \#{GitStyleBinary.pretty_known_subcommands.join("\n   ")}

  See '#{command.full_name} help COMMAND' for more information on a specific command.
  EOS

    opt :verbose,  "verbose", :default => false
  end
end

Instance Method Details

#[](k) ⇒ Object

Helper to return the option



200
201
202
# File 'lib/git-style-binary/command.rb', line 200

def [](k)
  opts[k]
end

#argvObject



174
175
176
# File 'lib/git-style-binary/command.rb', line 174

def argv
  parser.leftovers
end

#call_parser_run_blockObject



115
116
117
118
119
120
121
# File 'lib/git-style-binary/command.rb', line 115

def call_parser_run_block
  runs = GitStyleBinary.current_command.parser.runs

  parser.run_callbacks(:before_run, self)
  parser.runs.last.call(self) # ... not too happy with this
  parser.run_callbacks(:after_run, self)
end

#die(arg, msg = nil) ⇒ Object

die basically ripped out of trollop, because it can’t be called without first calling ‘Trollop.options’



189
190
191
192
193
194
195
196
197
# File 'lib/git-style-binary/command.rb', line 189

def die arg, msg=nil
  if msg
    $stderr.puts "Error: #{arg} - #{msg}."
  else
    $stderr.puts "Error: #{arg}."
  end
  $stderr.puts "Try --help for help."
  exit(-1)
end

#full_nameObject



182
183
184
185
# File 'lib/git-style-binary/command.rb', line 182

def full_name
  # ugly, should be is_primary?
  GitStyleBinary.primary_name == name ? GitStyleBinary.primary_name : GitStyleBinary.primary_name + "-" + name
end

#is_primary?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/git-style-binary/command.rb', line 170

def is_primary?
  false
end

#load_all_parser_constraintsObject



86
87
88
89
90
91
92
93
# File 'lib/git-style-binary/command.rb', line 86

def load_all_parser_constraints
  @loaded_all_parser_constraints ||= begin
    load_parser_default_constraints
    load_parser_primary_constraints
    load_parser_local_constraints
    true
  end
end

#load_parser_default_constraintsObject



95
96
97
# File 'lib/git-style-binary/command.rb', line 95

def load_parser_default_constraints
  parser.consume_all([self.class.defaults])
end

#load_parser_local_constraintsObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/git-style-binary/command.rb', line 103

def load_parser_local_constraints
  cur = GitStyleBinary.current_command # see, why isn't 'this' current_command?

  unless self.is_primary? && cur == self
    # TODO - the key lies in this function. figure out when you
    # have more energy soo UGLY. see #process_parser! unify with
    # that method parser.consume_all(constraints) rescue
    # ArgumentError
    parser.consume_all(cur.constraints)
  end
end

#load_parser_primary_constraintsObject



99
100
101
# File 'lib/git-style-binary/command.rb', line 99

def load_parser_primary_constraints
  parser.consume_all(GitStyleBinary.primary_command.constraints)
end

#parserObject



61
62
63
64
65
66
67
# File 'lib/git-style-binary/command.rb', line 61

def parser
  @parser ||= begin
                p = Parser.new
                p.command = self
                p
              end
end

#process_args(args = ARGV, *a, &b) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/git-style-binary/command.rb', line 150

def process_args(args = ARGV, *a, &b)
  p = parser
  begin
    vals = p.parse args
    args.clear
    p.leftovers.each { |l| args << l }
    vals # ugly todo
  rescue Trollop::CommandlineError => e
    $stderr.puts "Error: #{e.message}."
    $stderr.puts "Try --help for help."
    exit(-1)
  rescue Trollop::HelpNeeded
    p.educate
    exit
  rescue Trollop::VersionNeeded
    puts p.version
    exit
  end
end

#process_args_with_subcmd(args = ARGV, *a, &b) ⇒ Object



123
124
125
126
127
128
# File 'lib/git-style-binary/command.rb', line 123

def process_args_with_subcmd(args = ARGV, *a, &b)
  cmd = GitStyleBinary.current_command_name
  vals = process_args(args, *a, &b)
  parser.leftovers.shift if parser.leftovers[0] == cmd
  vals
end

#process_parser!Object

TOOooootally ugly! why? bc load_parser_local_constraints doesn’t work when loading the indivdual commands because it depends on #current_command. This really sucks and is UGLY. the todo is to put in ‘load_all_parser_constraints’ and this works



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/git-style-binary/command.rb', line 134

def process_parser!
  # load_all_parser_constraints

  load_parser_default_constraints
  load_parser_primary_constraints
  # load_parser_local_constraints
  parser.consume_all(constraints)

  # hack
  parser.consume {
    opt :version, "Print version and exit" if @version unless @specs[:version] || @long["version"]
    opt :help, "Show this message" unless @specs[:help] || @long["help"]
    resolve_default_short_options!
  } # hack
end

#runObject



73
74
75
76
77
78
79
80
# File 'lib/git-style-binary/command.rb', line 73

def run
  GitStyleBinary.load_primary    unless is_primary?
  GitStyleBinary.load_subcommand if is_primary? && running_subcommand?
  load_all_parser_constraints
  @opts = process_args_with_subcmd
  call_parser_run_block
  self
end

#running_subcommand?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/git-style-binary/command.rb', line 82

def running_subcommand?
  GitStyleBinary.valid_subcommand?(GitStyleBinary.current_command_name)
end

#short_descObject



178
179
180
# File 'lib/git-style-binary/command.rb', line 178

def short_desc
  parser.short_desc
end