class Snp::CLI

Snp::CLI

This class is responsible for parsing command line options passed to `snp` and retrieve the template name to be compiled and possibly options to override data for the template.

Example

CLI.parse_options # => ['/Users/john/.snp/jquery.html.erb', { version: '1.9' }]

Attributes

printer[R]
template_name[R]

Public Class Methods

new(params, printer = Printer.new) click to toggle source

Internal: creates a new `Snp::CLI` instance.

params - array of arguments. printer - the printer object through which feedback messages are sent to.

The passed object must respond to `out` and `err` for normal
and error situations, respectively.
# File lib/snp/cli.rb, line 70
def initialize(params, printer = Printer.new)
  @params  = params
  @options = {}
  @printer = printer
end
run(arguments = ARGV.dup) click to toggle source

Public: extract template name and other data that should be used to compile the template.

arguments - an array of arguments that should be parsed. Defaults to `ARGV`.

This method generates the snippet and fires your text editor in case it is set up, or prints the snippet to the standard output.

# File lib/snp/cli.rb, line 58
def self.run(arguments = ARGV.dup)
  new(arguments).start
end

Public Instance Methods

parse() click to toggle source

Internal: parses command line options.

Returns the template name and extra options to be used when compiling the snippet, extracted from command line arguments.

# File lib/snp/cli.rb, line 92
def parse
  help_and_exit if no_options_passed?

  template_name = parse_static_options
  template_data = parse_dynamic_options

  [template_name, template_data]
end
start() click to toggle source

Internal: actually does the parsing job and compiles the snippet.

# File lib/snp/cli.rb, line 77
def start
  @template_name, template_data = parse

  snippet = Compiler.build(template_name, template_data)

  edit(snippet) || printer.out(snippet)
rescue => exception
  printer.err exception.message
  help_and_exit
end

Private Instance Methods

dynamic_parser() click to toggle source

Internal: builds the dynamic option parser, that creates options on the fly according to the passed command line options.

# File lib/snp/cli.rb, line 152
def dynamic_parser
  @_dynamic_parser ||= Slop.new(autocreate: true)
end
edit(snippet) click to toggle source

Internal: Opens the preferred text editor with the content of a snippet.

snippet - a string with the snippet content that should be edited.

# File lib/snp/cli.rb, line 176
def edit(snippet)
  if editor && !editor.empty?
    snippet_file = file_for(snippet)
    Process.exec "#{editor} '#{snippet_file}'"
  end
end
editor() click to toggle source

Internal: returns the editor that should be used when editing a generated snippet. Looks for editor names in the `SNP_EDITOR` and `EDITOR` environment variables, respectively.

# File lib/snp/cli.rb, line 169
def editor
  @_editor ||= ENV['SNP_EDITOR'] || ENV['EDITOR']
end
file_for(content) click to toggle source

Internal: creates a file in the working directory to wihch the snippet contents will be written to, allowing it to be edited.

content - the content of the final snippet.

# File lib/snp/cli.rb, line 187
def file_for(content)
  "snp_#{template_name}".tap do |file_name|
    File.open(file_name, "w+") { |f| f.write(content) }
  end
end
help_and_exit() click to toggle source

Internal: prints help message and exits with a failure exit status.

# File lib/snp/cli.rb, line 199
def help_and_exit
  printer.err help_message
  exit 1
end
help_message() click to toggle source

Internal: returns the help message for the `snp` command.

# File lib/snp/cli.rb, line 205
def help_message
  option_parser.to_s
end
no_options_passed?() click to toggle source

Internal: checks whether or not any arguments were passed on the command line.

# File lib/snp/cli.rb, line 210
def no_options_passed?
  @params.empty?
end
option_parser() click to toggle source

Internal: builds the static option parser. Recognizes `-V` for version and `-h` for help.

# File lib/snp/cli.rb, line 136
def option_parser
  @_option_parser ||= Slop.new do |command|
    command.banner "Usage: #{program_name} [options] [template_name]"

    command.on('-V', 'Shows version and exits') do
      print_and_exit Snp::VERSION
    end

    command.on('-h', 'Shows this message') do
      print_and_exit command.to_s
    end
  end
end
parse_dynamic_options() click to toggle source

Internal: parses dynamic options, creating options according to the arguments passed on the command line.

Example

# command is '--project snp --language ruby template_name'
parse_dynamic_options # => { 'project' => 'snp', 'language' => 'ruby' }
# File lib/snp/cli.rb, line 117
def parse_dynamic_options
  if no_options_passed?
    {}
  else
    dynamic_parser.parse!(@params)

    data = dynamic_parser.to_hash
    invalid_key = data.find { |key, value| value.nil? }

    if invalid_key
      raise InvalidOptions.new(invalid_key.first)
    end

    data
  end
end
parse_static_options() click to toggle source

Internal: parses the command line options to check for static options (version number and help).

# File lib/snp/cli.rb, line 105
def parse_static_options
  option_parser.parse!(@params)
  @params.pop
end
print_and_exit(message) click to toggle source

Internal: prints a message and exits.

message - the message to be printed before exiting.

This method finishes the current process with a success exit status.

program_name() click to toggle source

Internal: the program name to be used when generating output to the user.

# File lib/snp/cli.rb, line 194
def program_name
  File.basename($0, '.*')
end
template_extension() click to toggle source

Internal: returns the extension of the template name, to be used in the generated snippet.

# File lib/snp/cli.rb, line 216
def template_extension
  @_extension ||= begin
    match_data = template_name.match(/.+(\..+)/)

    # set it to the empty string in case there is no extension to allow for
    # proper memoization of its value
    match_data ? match_data[1] : ""
  end
end