Class: Metaclean::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/metaclean/cli.rb', line 24

def initialize(argv)
  # `dup` makes a shallow copy so we can mutate `@argv` without
  # surprising the caller (ARGV itself stays intact).
  @argv = argv.dup

  # All options default to safe/off values. `parse!` flips them
  # selectively as it sees flags.
  @options = {
    recursive:    false,
    in_place:     false,
    force:        false,
    inspect_only: false,
    dry_run:      false
  }
  @paths = []
end

Class Method Details

.start(argv) ⇒ Object

Class-level convenience: ‘Metaclean::CLI.start(ARGV)` reads cleaner than `Metaclean::CLI.new(ARGV).run`.



20
21
22
# File 'lib/metaclean/cli.rb', line 20

def self.start(argv)
  new(argv).run
end

Instance Method Details

#runObject

Top-level dispatcher. Catches our errors and exits with codes that shells/CI can act on:

0  → success
1  → general failure
2  → a required tool (exiftool/mat2/qpdf) is missing (install hint shown)
130→ user pressed Ctrl-C (matches the standard SIGINT exit code)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/metaclean/cli.rb', line 47

def run
  parse!
  # Refuse to run unless all three external tools are present (see
  # Metaclean.ensure_tools!). --help/--version already exited inside parse!,
  # so this only gates an actual inspect/clean.
  Metaclean.ensure_tools!
  runner = Runner.new(@options)
  if @options[:inspect_only]
    runner.inspect_paths(@paths)
  else
    runner.clean_paths(@paths)
  end
rescue ToolsMissing => e
  warn Display.error('Missing required tools')
  warn e.message
  exit 2
rescue Error, SystemCallError => e
  # Errno::* (disk full, permission denied, read-only fs) is a SIBLING of
  # our Error, not a subclass; naming it here gives filesystem failures a
  # clean message + exit 1 instead of a raw backtrace.
  warn Display.error(e.message)
  exit 1
rescue Interrupt
  # Pressing Ctrl-C raises `Interrupt`. Catching it lets us print a
  # clean message instead of a Ruby stack trace.
  warn "\n#{Display.error('Interrupted.')}"
  exit 130
end