class Cukedep::CLI::CmdLine
Manages the application command-line interface (CLI). It is merely a thin wrapper around the OptionParser library. Responsibilities:
-
Specify the command-line syntax,
-
Return the result of the command-line parsing
Attributes
options[R]
A Hash with the result of the command-line parse.
parser[R]
OptionParser object
Public Class Methods
new()
click to toggle source
Constructor.
# File lib/cukedep/cli/cmd-line.rb, line 25 def initialize @options = {} @parser = OptionParser.new do |opts| opts.banner = <<-HELP Usage: cukedep [options] The command-line options are: HELP # No argument. Check dry_txt1 = 'Check the feature file dependencies' dry_txt2 = ' without running the feature files.' opts.on(nil, '--dry-run', dry_txt1 + dry_txt2) do options[:dryrun] = true end # No argument. Create .cukedep.yml file setup_txt = 'Create a default .cukedep.yml file in current dir.' opts.on(nil, '--setup', setup_txt) do options[:setup] = true end # Mandatory argument msg_p1 = 'Run the Cucumber project at given path ' msg_p2 = 'with features from current dir.' opts.on('--project PROJ_PATH', msg_p1 + msg_p2) do |project_path| options[:project] = validated_project(project_path) end # No argument, shows at tail. This will print an options summary. opts.on_tail('-h', '--help', 'Show this message') do puts opts options[:help] = true end opts.on_tail('--version', 'Show application version number') do puts Cukedep::Version options[:version] = true end end end
Public Instance Methods
parse!(theCmdLineArgs)
click to toggle source
Perform the command-line parsing
# File lib/cukedep/cli/cmd-line.rb, line 68 def parse!(theCmdLineArgs) begin parser.parse!(theCmdLineArgs) rescue OptionParser::MissingArgument => exc err_msg = '' exc.args.each do |arg| err_msg << "No argument provided with command line option: #{arg}\n" end err_msg << 'To see the command-line syntax, do:\ncukedep --help' raise StandardError, err_msg end # Some options stop the application exit if options[:version] || options[:help] return options end
Private Instance Methods
validated_project(theProjectPath)
click to toggle source
# File lib/cukedep/cli/cmd-line.rb, line 88 def validated_project(theProjectPath) unless Dir.exist?(theProjectPath) raise StandardError, "Cannot find the directory '#{theProjectPath}'." end # If current dir is /features and project dir is parent of it # then we have an error current_path = Pathname.getwd if current_path.to_s =~ /features$/ if current_path.parent == Pathname.new(theProjectPath) msg_prefix = "Don't place original feature file in 'features'" msg_suffix = ' subdir of project dir.' raise StandardError, msg_prefix + msg_suffix end end return theProjectPath end