class RubyNext::CLI

Command line interface for RubyNext

Constants

COMMANDS

Attributes

dry_run[RW]
dry_run?[RW]
verbose[RW]
verbose?[RW]

Public Class Methods

new() click to toggle source
# File lib/ruby-next/cli.rb, line 27
def initialize
end

Public Instance Methods

run(args = ARGV) click to toggle source
# File lib/ruby-next/cli.rb, line 30
def run(args = ARGV)
  maybe_print_version(args)

  command = extract_command(args)

  # Handle top-level help
  unless command
    maybe_print_help
    raise "Command must be specified!"
  end

  args.delete(command)

  args.unshift(*load_args_from_rc(command))

  COMMANDS.fetch(command) do
    raise "Unknown command: #{command}. Available commands: #{COMMANDS.keys.join(",")}"
  end.run(args)
end

Private Instance Methods

extract_command(source_args) click to toggle source
# File lib/ruby-next/cli.rb, line 68
def extract_command(source_args)
  args = source_args.dup
  unknown_args = []
  command = nil
  begin
    command, = optparser.permute!(args)
  rescue OptionParser::InvalidOption => e
    unknown_args += e.args
    args = source_args - unknown_args
    retry
  end
  command
end
load_args_from_rc(command) click to toggle source
# File lib/ruby-next/cli.rb, line 99
def load_args_from_rc(command)
  return [] unless File.file?(".rbnextrc")

  require "yaml"
  command_args = YAML.load_file(".rbnextrc")[command]
  return [] unless command_args

  command_args.lines.flat_map { |line| line.chomp.split(/\s+/) }
end
maybe_print_help() click to toggle source
# File lib/ruby-next/cli.rb, line 61
def maybe_print_help
  return unless @print_help

  $stdout.puts optparser.help
  exit 0
end
maybe_print_version(args) click to toggle source
# File lib/ruby-next/cli.rb, line 52
def maybe_print_version(args)
  args = args.dup
  begin
    optparser.parse!(args)
  rescue OptionParser::InvalidOption
    # skip and pass all args to the command's parser
  end
end
optparser() click to toggle source
# File lib/ruby-next/cli.rb, line 82
def optparser
  @optparser ||= begin
    OptionParser.new do |opts|
      opts.banner = "Usage: ruby-next COMMAND [options]"

      opts.on("-v", "--version", "Print version") do
        $stdout.puts RubyNext::VERSION
        exit 0
      end

      opts.on("-h", "--help", "Print help") do
        @print_help = true
      end
    end
  end
end