class 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
Public Class Methods
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
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
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
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
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
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
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
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
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
Internal: returns the help message for the `snp` command.
# File lib/snp/cli.rb, line 205 def help_message option_parser.to_s end
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
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
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
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
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.
# File lib/snp/cli.rb, line 161 def print_and_exit(message) printer.out message exit end
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
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