Class: GitStyleBinary::Command
- Defined in:
- lib/git-style-binary/command.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#name ⇒ Object
Returns the value of attribute name.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](k) ⇒ Object
Helper to return the option.
- #argv ⇒ Object
- #call_parser_run_block ⇒ Object
-
#die(arg, msg = nil) ⇒ Object
die basically ripped out of trollop, because it can’t be called without first calling ‘Trollop.options’.
- #full_name ⇒ Object
-
#initialize(o = {}) ⇒ Command
constructor
A new instance of Command.
- #is_primary? ⇒ Boolean
- #load_all_parser_constraints ⇒ Object
- #load_parser_default_constraints ⇒ Object
- #load_parser_local_constraints ⇒ Object
- #load_parser_primary_constraints ⇒ Object
- #parser ⇒ Object
- #process_args(args = ARGV, *a, &b) ⇒ Object
- #process_args_with_subcmd(args = ARGV, *a, &b) ⇒ Object
-
#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.
- #run ⇒ Object
- #running_subcommand? ⇒ Boolean
- #short_desc ⇒ Object
Constructor Details
#initialize(o = {}) ⇒ Command
Returns a new instance of Command.
54 55 56 57 58 |
# File 'lib/git-style-binary/command.rb', line 54 def initialize(o={}) o.each do |k,v| eval "@#{k.to_s}= v" end end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
50 51 52 |
# File 'lib/git-style-binary/command.rb', line 50 def constraints @constraints end |
#name ⇒ Object
Returns the value of attribute name.
52 53 54 |
# File 'lib/git-style-binary/command.rb', line 52 def name @name end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
51 52 53 |
# File 'lib/git-style-binary/command.rb', line 51 def opts @opts end |
Class Method Details
.defaults ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/git-style-binary/command.rb', line 30 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}" <<-EOS #{"SYNOPSIS".colorize(:red)} #{command.full_name.colorize(:light_blue)} #{} #{"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
199 200 201 |
# File 'lib/git-style-binary/command.rb', line 199 def [](k) opts[k] end |
#argv ⇒ Object
173 174 175 |
# File 'lib/git-style-binary/command.rb', line 173 def argv parser.leftovers end |
#call_parser_run_block ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/git-style-binary/command.rb', line 114 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’
188 189 190 191 192 193 194 195 196 |
# File 'lib/git-style-binary/command.rb', line 188 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_name ⇒ Object
181 182 183 184 |
# File 'lib/git-style-binary/command.rb', line 181 def full_name # ugly, should be is_primary? GitStyleBinary.primary_name == name ? GitStyleBinary.primary_name : GitStyleBinary.primary_name + "-" + name end |
#is_primary? ⇒ Boolean
169 170 171 |
# File 'lib/git-style-binary/command.rb', line 169 def is_primary? false end |
#load_all_parser_constraints ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/git-style-binary/command.rb', line 85 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_constraints ⇒ Object
94 95 96 |
# File 'lib/git-style-binary/command.rb', line 94 def load_parser_default_constraints parser.consume_all([self.class.defaults]) end |
#load_parser_local_constraints ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/git-style-binary/command.rb', line 102 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_constraints ⇒ Object
98 99 100 |
# File 'lib/git-style-binary/command.rb', line 98 def load_parser_primary_constraints parser.consume_all(GitStyleBinary.primary_command.constraints) end |
#parser ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/git-style-binary/command.rb', line 60 def parser @parser ||= begin p = Parser.new p.command = self p end end |
#process_args(args = ARGV, *a, &b) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/git-style-binary/command.rb', line 149 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.}." $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
122 123 124 125 126 127 |
# File 'lib/git-style-binary/command.rb', line 122 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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/git-style-binary/command.rb', line 133 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"] } # hack end |
#run ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/git-style-binary/command.rb', line 72 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
81 82 83 |
# File 'lib/git-style-binary/command.rb', line 81 def running_subcommand? GitStyleBinary.valid_subcommand?(GitStyleBinary.current_command_name) end |
#short_desc ⇒ Object
177 178 179 |
# File 'lib/git-style-binary/command.rb', line 177 def short_desc parser.short_desc end |