Class: Prune::CommandLineInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/prune/cli.rb

Overview

The command-line interface for prune is the primary way to interact with it, although it can be invoked programatically as well.

This class defines the arguments and options taken by Prune and how it reacts to various forms of command-line input.

Constant Summary collapse

DEFAULT_OPTIONS =
{ :verbose => false, :did_work => false, :dry_run => false, :prompt => true, :archive => true, :configure => false }

Class Method Summary collapse

Class Method Details

.make_parser(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/prune/cli.rb', line 15

def self.make_parser( options )
  OptionParser.new do |opts|
    opts.banner = "Usage: prune [options] folder"
    opts.on( "-v", "--verbose", "Prints much more frequently during execution about what it's doing." ) { options[:verbose] = true }
    opts.on( "-d", "--dry-run", "Categorizes files, but does not take any actions on them." ) { options[:dry_run] = true }
    opts.on( "-f", "--force", "--no-prompt", "Will take action without asking permissions; useful for automation." ) { options[:prompt] = false }
    opts.on( "-a", "--archive-folder FOLDER", "The folder in which archives should be stored; defaults to <folder>/../<folder-name>-archives." ) { |path| options[:archive_path] = path }
    opts.on( "--no-archive", "Don't perform archival; typically if the files you're pruning are already compressed." ) { options[:archive] = false }
    opts.on( "--config", "Configure the retention policy for the specified folder." ) { options[:configure] = true }
    opts.on_tail( "--version", "Displays version information." ) do
      options[:did_work] = true
      print "Prune #{VERSION}, by Geoffrey Wiseman.\n"
    end
    opts.on_tail( "-?", "--help", "Shows quick help about using prune." ) do
      options[:did_work] = true
      puts opts
    end
  end
end

.parse_and_runObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prune/cli.rb', line 35

def self.parse_and_run
  options = DEFAULT_OPTIONS.dup
  parser = make_parser options
  begin
    parser.parse!

    if ARGV.size == 1 then
      if options[:configure] then
        configurer = Configurer.new( ARGV.first, options )
        configurer.configure
      else
        Pruner.new( options ).prune( ARGV.first )
      end
    else
      print parser.help unless options[:did_work]
    end
  rescue OptionParser::ParseError
    $stderr.print "Error: " + $!.message + "\n"
  end

end