Class: Commander::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/brief/cli/01_extensions.rb

Instance Method Summary collapse

Instance Method Details

#action(*a, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/brief/cli/01_extensions.rb', line 2

def action(*a, &block)
  Brief.default_cli_options(self)

  when_called do |args, options|
    options.default(root: Brief.pwd, config_filename: 'brief.rb', format: :printed, prefix_output: "")

    root = Pathname(options.root)

    if root.join(options.config_filename).exist?
      Brief.case = lambda do
        Brief::Briefcase.new(root: Pathname(options.root))
      end
    end

    result = block.call(args, options)

    case
    when options.output && options.format.to_sym == :json
      Pathname(options.output).open("w+") do |fh|
        fh.write("#{options.prefix_output}#{result.to_json}")
      end
    when options.format.to_sym == :json
      puts "#{options.prefix_output}#{result.to_json}"
    when options.format.to_sym == :printed
      puts result
    end

  end
end