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
- #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.
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
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
47 48 49 |
# File 'lib/git-style-binary/command.rb', line 47 def constraints @constraints end |
#name ⇒ Object
Returns the value of attribute name.
49 50 51 |
# File 'lib/git-style-binary/command.rb', line 49 def name @name end |
#opts ⇒ Object (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
.defaults ⇒ Object
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}" <<-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
189 190 191 |
# File 'lib/git-style-binary/command.rb', line 189 def [](k) opts[k] end |
#argv ⇒ Object
169 170 171 |
# File 'lib/git-style-binary/command.rb', line 169 def argv parser.leftovers end |
#call_parser_run_block ⇒ Object
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_name ⇒ Object
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
165 166 167 |
# File 'lib/git-style-binary/command.rb', line 165 def is_primary? false end |
#load_all_parser_constraints ⇒ Object
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_constraints ⇒ Object
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_constraints ⇒ Object
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_constraints ⇒ Object
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 |
#parser ⇒ Object
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.}." $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"] } # hack end |
#run ⇒ Object
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
78 79 80 |
# File 'lib/git-style-binary/command.rb', line 78 def running_subcommand? GitStyleBinary.valid_subcommand?(GitStyleBinary.current_command_name) end |
#short_desc ⇒ Object
173 174 175 |
# File 'lib/git-style-binary/command.rb', line 173 def short_desc parser.short_desc end |