Class: SpreenClean::CLI
- Inherits:
-
Object
- Object
- SpreenClean::CLI
- Defined in:
- lib/spreen_clean/cli.rb,
sig/generated/spreen_clean/cli.rbs
Overview
Command line interface behind the file-clean executable:
file-clean [PATTERN] [--dirname DIR] [--mode d|e] [--yes].
Constant Summary collapse
- MODES =
: Array
%w[d e].freeze
- BULK_DELETE_THRESHOLD =
The execution mode asks for confirmation above this many matches.
100
Instance Attribute Summary collapse
-
#action ⇒ Symbol
readonly
: Symbol.
-
#argv ⇒ Array[String]
readonly
: Array.
-
#dirname ⇒ String
readonly
: String.
-
#mode ⇒ String
readonly
: String.
-
#skip_confirmation ⇒ Boolean
readonly
: bool.
Class Method Summary collapse
Instance Method Summary collapse
- #clean_files ⇒ Integer
-
#confirmed?(application) ⇒ Boolean
Guards the execution mode against oversized sweeps: above BULK_DELETE_THRESHOLD matches it asks for an explicit
y, unless--yeswas given for scripted use. -
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #parser ⇒ OptionParser
- #pattern ⇒ String
- #print_help ⇒ Integer
- #print_version ⇒ Integer
- #run ⇒ Integer
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
24 25 26 27 28 29 30 |
# File 'lib/spreen_clean/cli.rb', line 24 def initialize(argv) @argv = argv.dup @dirname = '.' @mode = 'd' @skip_confirmation = false @action = :clean_files end |
Instance Attribute Details
#action ⇒ Symbol (readonly)
: Symbol
47 48 49 |
# File 'lib/spreen_clean/cli.rb', line 47 def action @action end |
#argv ⇒ Array[String] (readonly)
: Array
43 44 45 |
# File 'lib/spreen_clean/cli.rb', line 43 def argv @argv end |
#dirname ⇒ String (readonly)
: String
44 45 46 |
# File 'lib/spreen_clean/cli.rb', line 44 def dirname @dirname end |
#mode ⇒ String (readonly)
: String
45 46 47 |
# File 'lib/spreen_clean/cli.rb', line 45 def mode @mode end |
#skip_confirmation ⇒ Boolean (readonly)
: bool
46 47 48 |
# File 'lib/spreen_clean/cli.rb', line 46 def skip_confirmation @skip_confirmation end |
Class Method Details
.start(argv = ARGV) ⇒ Integer
18 19 20 |
# File 'lib/spreen_clean/cli.rb', line 18 def self.start(argv = ARGV) new(argv).run end |
Instance Method Details
#clean_files ⇒ Integer
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spreen_clean/cli.rb', line 50 def clean_files application = Application.new(dirname:, pattern:, mode:) application.validate_mode! application.validate_dirname! unless confirmed?(application) warn 'Aborted the execution mode without deleting anything.' return 1 end application.run 0 end |
#confirmed?(application) ⇒ Boolean
Guards the execution mode against oversized sweeps: above
BULK_DELETE_THRESHOLD matches it asks for an explicit y, unless
--yes was given for scripted use.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/spreen_clean/cli.rb', line 68 def confirmed?(application) return true unless mode == 'e' return true if skip_confirmation count = application.files.length return true if count <= BULK_DELETE_THRESHOLD print "About to delete #{count} files (more than #{BULK_DELETE_THRESHOLD}). Type `y` to proceed: " answer = $stdin.gets return false if answer.nil? %w[y yes].include?(answer.strip.downcase) end |
#parser ⇒ OptionParser
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/spreen_clean/cli.rb', line 100 def parser @parser ||= OptionParser.new('Usage: file-clean [PATTERN] [options]') do |opt| opt.on('--dirname DIR', 'Directory tree to clean (default: .)') { |value| @dirname = value } opt.on('--mode MODE', MODES, "Operation mode (#{MODES.join(' or ')}; d = dry run, e = execute, default: d)") { |value| @mode = value } opt.on('--yes', 'Skip the bulk-delete confirmation in the execution mode') { @skip_confirmation = true } opt.on('--version', 'Print the version') { @action = :print_version } opt.on('-h', '--help', 'Print this help') { @action = :print_help } end end |
#pattern ⇒ String
95 96 97 |
# File 'lib/spreen_clean/cli.rb', line 95 def pattern argv.shift || '*' end |
#print_help ⇒ Integer
89 90 91 92 |
# File 'lib/spreen_clean/cli.rb', line 89 def print_help puts parser 0 end |
#print_version ⇒ Integer
83 84 85 86 |
# File 'lib/spreen_clean/cli.rb', line 83 def print_version puts VERSION 0 end |
#run ⇒ Integer
33 34 35 36 37 38 39 |
# File 'lib/spreen_clean/cli.rb', line 33 def run parser.parse!(argv) __send__(action) rescue Application::InvalidModeError, Application::RootDirnameError, OptionParser::ParseError => e warn e. 1 end |