module BaseChip::Cli::InstanceMethods
Public Instance Methods
cli()
click to toggle source
# File lib/base_chip/cli.rb, line 283 def cli ; self.class.cli end
options()
click to toggle source
# File lib/base_chip/cli.rb, line 284 def options ; self.class.options end
print_table(table, delimeter="\t")
click to toggle source
# File lib/base_chip/cli.rb, line 346 def print_table(table, delimeter="\t") column = 0 table2 = [] sizes = [] # determine size table.each do |row| column = 0 table2 << row.map do |c| tmp = c.to_s tmpsize = tmp.size sizes[column] = tmpsize if sizes[column].nil? or tmpsize > sizes[column] column += 1 tmp end end # map to sized columns table2.each do |row| column = 0 puts (row.map! { |c| "%-#{sizes[(column += 1) - 1]}s" % c }).join(" => ").strip end end
run_cli(arguments = ARGV)
click to toggle source
# File lib/base_chip/cli.rb, line 290 def run_cli(arguments = ARGV) run_cli_no_command if arguments.size == 0 pass_through = nil options. append_arguments = nil options.replace_arguments = nil arguments.keep_if do |a| case a when '--' ; pass_through = (options. append_arguments ||= []); false when '---' ; pass_through = (options.replace_arguments ||= []); false else if pass_through pass_through << a false else true end end end cli.options.each do |o,h| if h[:value] options.send("#{h[:name]}=",h[:value]) end end cli.mine_options(options,arguments) arguments.size.times do |i| case a = arguments[i].to_sym when *(cli.sub_commands.keys) arguments.delete_at i cli.sub_commands[a].new.run_cli(arguments) exit when *(cli.tasks.keys) arguments.delete_at i task = cli.tasks[a] task.mine_options(options,arguments,true) if task.required_argument_size > arguments.size help task.name fault "\n#{task.name} is missing required arguments" end send a, *arguments break else if a[0] == '-' fault("Could not determine what to run from \"#{arguments.join(' ')}\". Please consider placing dash options at the end of the command line.") end task = cli.default_task or fault("Could not determine what to run from \"#{arguments.join(' ')}\".") task.mine_options(options,arguments,true) if task.required_argument_size > arguments.size puts "\n#{task.name} is missing required arguments" help task.name exit 1 end send task.name, *arguments break end end end
run_cli_no_command()
click to toggle source
# File lib/base_chip/cli.rb, line 285 def run_cli_no_command puts "\nPlease specify a sub command" + ( cli.name ? " for #{cli.name}" : '' ) help exit 1 end