Class: Aef::Linebreak::CLI::EncodeCommand

Inherits:
UserChoices::Command
  • Object
show all
Includes:
UserChoices
Defined in:
lib/aef/linebreak/cli/commands/encode.rb

Overview

Command-line sub-command for re-ncoding of files.

Instance Method Summary collapse

Instance Method Details

#add_choices(builder) ⇒ Object

Define configuration options



43
44
45
46
47
48
49
50
51
# File 'lib/aef/linebreak/cli/commands/encode.rb', line 43

def add_choices(builder)
  systems = Aef::Linebreak::BREAK_BY_SYSTEM.keys.map{|key| key.to_s}
  builder.add_choice(:system, :default => 'unix', :type => systems) do |cli|
    cli.uses_option('-s', '--system SYSTEM',
      "Output encoding system. Possible settings: #{systems.join(', ')}")
  end
      
  builder.add_choice(:files, :length => 0..2, :type => :pathname) {|cli| cli.uses_arglist}
end

#add_sources(builder) ⇒ Object

Prepare configuration sources



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/aef/linebreak/cli/commands/encode.rb', line 29

def add_sources(builder)
  builder.add_source(
    CommandLineSource, :usage,
    "Usage: #$PROGRAM_NAME encode [OPTIONS] [INPUT] [OUTPUT]\n\n",
    "Convert all linebreak encodings of a file.\n",
    "Use the environment variable LINEBREAK_SYSTEM to specify a default output encoding.\n"
  )
      
  builder.add_source(EnvironmentSource, :mapping,
    :system => 'LINEBREAK_SYSTEM'
  )
end

#executeObject

Main program



69
70
71
72
73
74
75
# File 'lib/aef/linebreak/cli/commands/encode.rb', line 69

def execute
  @user_choices[:input].each_line do |line|
    @user_choices[:output] << Aef::Linebreak.encode(line, @user_choices[:system].to_sym)
  end
      
  [:input, :output].each {|stream| @user_choices[stream].close }
end

#postprocess_user_choicesObject

Manual option post processing



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aef/linebreak/cli/commands/encode.rb', line 54

def postprocess_user_choices
  if @user_choices[:files][0] and @user_choices[:files][0] != Pathname('-')
    @user_choices[:input] = @user_choices[:files][0].open('r')
  else
    @user_choices[:input] = STDIN
  end
      
  if @user_choices[:files][1] and @user_choices[:files][1] != Pathname('-')
    @user_choices[:output] = @user_choices[:files][1].open('w')
  else
    @user_choices[:output] = STDOUT
  end
end