Sha256: 89da45fd548abe9e10441535a0c0ad07aeb2dc33f2c85f387effb77f76a85c61

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

class Commander::Command
  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)

      root = Pathname(options.root)

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

      result = block.call(args, options)

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

    end
  end
end

module Brief
  def self.default_cli_options(c)
    c.option '--root DIRECTORY', String, 'The root for the briefcase'
    c.option '--config FILE', String, 'Path to the config file for this briefcase'
    c.option '--config-filename', String, 'The default filename for a briefcase config: brief.rb'
    c.option '--output FILE', String, 'Save the output in the specified path'
    c.option '--format FORMAT', String, 'How to format the CLI output: defaults to printed, accepts printed,json'
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
brief-1.9.4 lib/brief/cli/01_extensions.rb
brief-1.9.3 lib/brief/cli/01_extensions.rb
brief-1.9.2 lib/brief/cli/01_extensions.rb
brief-1.9.1 lib/brief/cli/01_extensions.rb