Class: Unparser::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/unparser/cli.rb,
lib/unparser/cli/differ.rb,
lib/unparser/cli/source.rb

Overview

Unparser CLI implementation

Defined Under Namespace

Classes: Differ, Source

Constant Summary collapse

EXIT_SUCCESS =
0
EXIT_FAILURE =
1

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object

Parameters:

  • arguments (Array<String>)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unparser/cli.rb', line 40

def initialize(arguments)
  @sources, @ignore = [], Set.new

  @success   = true
  @fail_fast = false
  @verbose   = false

  opts = OptionParser.new do |builder|
    add_options(builder)
  end

  opts.parse!(arguments).each do |name|
    @sources.concat(sources(name))
  end
end

Class Method Details

.run(*arguments) ⇒ Fixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run CLI

Parameters:

  • arguments (Array<String>)

Returns:

  • (Fixnum)

    the exit status



28
29
30
# File 'lib/unparser/cli.rb', line 28

def self.run(*arguments)
  new(*arguments).exit_status
end

Instance Method Details

#add_options(builder) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Add options

Parameters:

  • builder (Optparse::Builder)

Returns:

  • (undefined)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/unparser/cli.rb', line 64

def add_options(builder)
  builder.banner = 'usage: unparse [options] FILE [FILE]'
  builder.separator('')
  builder.on('-e', '--evaluate SOURCE') do |source|
    @sources << Source::String.new(source)
  end
  builder.on('--start-with FILE') do |file|
    @start_with = sources(file).first
  end
  builder.on('-v', '--verbose') do
    @verbose = true
  end
  builder.on('--ignore FILE') do |file|
    @ignore.merge(sources(file))
  end
  builder.on('--fail-fast') do
    @fail_fast = true
  end
end

#exit_statusFixnum

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return exit status

Returns:

  • (Fixnum)


90
91
92
93
94
95
96
97
98
# File 'lib/unparser/cli.rb', line 90

def exit_status
  effective_sources.each do |source|
    next if @ignore.include?(source)
    process_source(source)
    break unless @success if @fail_fast
  end

  @success ? EXIT_SUCCESS : EXIT_FAILURE
end