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.



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

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.



47
48
49
# File 'lib/git-style-binary/command.rb', line 47

def constraints
  @constraints
end

#nameObject

Returns the value of attribute name.



49
50
51
# File 'lib/git-style-binary/command.rb', line 49

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



48
49
50
# File 'lib/git-style-binary/command.rb', line 48

def opts
  @opts
end

Class Method Details

.defaultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/git-style-binary/command.rb', line 27

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}

#{"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



189
190
191
# File 'lib/git-style-binary/command.rb', line 189

def [](k)
  opts[k]
end

#argvObject



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

def argv
  parser.leftovers
end

#call_parser_run_blockObject



110
111
112
113
114
115
116
# File 'lib/git-style-binary/command.rb', line 110

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



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

def die arg, msg=nil
  p = parser # create local copy
  Trollop.instance_eval { @p = p }
  Trollop::die(arg, msg)
end

#full_nameObject



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

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)


165
166
167
# File 'lib/git-style-binary/command.rb', line 165

def is_primary?
  false
end

#load_all_parser_constraintsObject



82
83
84
85
86
87
88
89
# File 'lib/git-style-binary/command.rb', line 82

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



91
92
93
# File 'lib/git-style-binary/command.rb', line 91

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

#load_parser_local_constraintsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/git-style-binary/command.rb', line 99

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

  unless self.is_primary? && cur == self
    # TODO TODO - the key lies in this function. figure out when you hav emore engergy
    # 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



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

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

#parserObject



57
58
59
60
61
62
63
# File 'lib/git-style-binary/command.rb', line 57

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

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



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/git-style-binary/command.rb', line 145

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



118
119
120
121
122
123
# File 'lib/git-style-binary/command.rb', line 118

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/git-style-binary/command.rb', line 129

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



69
70
71
72
73
74
75
76
# File 'lib/git-style-binary/command.rb', line 69

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)


78
79
80
# File 'lib/git-style-binary/command.rb', line 78

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

#short_descObject



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

def short_desc 
  parser.short_desc
end