Class: AtCoderFriends::CLI

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

Overview

command line interface

Constant Summary collapse

EXITING_OPTIONS =
i[version].freeze
OPTION_BANNER =
"Usage:\n  at_coder_friends setup        path/contest       # setup contest folder\n  at_coder_friends test-one     path/contest/src   # run 1st test case\n  at_coder_friends test-all     path/contest/src   # run all test cases\n  at_coder_friends submit       path/contest/src   # submit source code\n  at_coder_friends open-contest path/contest/src   # open contest page\nOptions:\n"
STATUS_SUCCESS =
0
STATUS_ERROR =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



23
24
25
# File 'lib/at_coder_friends/cli.rb', line 23

def ctx
  @ctx
end

Instance Method Details

#exec_command(command, path, *args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/at_coder_friends/cli.rb', line 67

def exec_command(command, path, *args)
  @ctx = Context.new(@options, path)
  case command
  when 'setup'
    setup
  when 'test-one'
    test_one(*args)
  when 'test-all'
    test_all
  when 'submit'
    submit
  when 'judge-one'
    judge_one(*args)
  when 'judge-all'
    judge_all
  when 'open-contest'
    open_contest
  else
    raise ParamError, "unknown command: #{command}"
  end
  ctx.post_process
end

#handle_exiting_optionObject



60
61
62
63
64
65
# File 'lib/at_coder_friends/cli.rb', line 60

def handle_exiting_option
  return unless EXITING_OPTIONS.any? { |o| @options.key? o }

  puts AtCoderFriends::VERSION if @options[:version]
  exit STATUS_SUCCESS
end

#judge_allObject



126
127
128
# File 'lib/at_coder_friends/cli.rb', line 126

def judge_all
  ctx.judge_test_runner.judge_all
end

#judge_one(id = '') ⇒ Object



122
123
124
# File 'lib/at_coder_friends/cli.rb', line 122

def judge_one(id = '')
  ctx.judge_test_runner.judge_one(id)
end

#open_contestObject



130
131
132
# File 'lib/at_coder_friends/cli.rb', line 130

def open_contest
  Launchy.open(ctx.scraping_agent.contest_url)
end

#parse_options!(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/at_coder_friends/cli.rb', line 43

def parse_options!(args)
  op = OptionParser.new do |opts|
    opts.banner = OPTION_BANNER
    opts.on('-v', '--version', 'Display version.') do
      @options[:version] = true
    end
    opts.on('-d', '--debug', 'Display debug info.') do
      @options[:debug] = true
    end
  end
  @usage = op.to_s
  @options = {}
  op.parse!(args)
rescue OptionParser::InvalidOption => e
  raise ParamError, e.message
end

#run(args = ARGV) ⇒ Object



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

def run(args = ARGV)
  parse_options!(args)
  handle_exiting_option
  raise ParamError, 'command or path is not specified.' if args.size < 2

  exec_command(*args)
  STATUS_SUCCESS
rescue AtCoderFriends::ParamError => e
  warn @usage
  warn "error: #{e.message}"
  STATUS_ERROR
rescue AtCoderFriends::AppError => e
  warn e.message
  STATUS_ERROR
rescue SystemExit => e
  e.status
end

#setupObject

Raises:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/at_coder_friends/cli.rb', line 90

def setup
  path = ctx.path
  raise AppError, "#{path} is not empty." \
    if Dir.exist?(path) && !Dir["#{path}/*"].empty?

  rb_gen = RubyGenerator.new
  cxx_gen = CxxGenerator.new
  ctx.scraping_agent.fetch_all do |pbm|
    Parser::Main.process(pbm)
    rb_gen.process(pbm)
    cxx_gen.process(pbm)
    ctx.emitter.emit(pbm)
  end
end

#submitObject

Raises:



114
115
116
117
118
119
120
# File 'lib/at_coder_friends/cli.rb', line 114

def submit
  vf = ctx.verifier
  raise AppError, "#{vf.file} has not been tested." unless vf.verified?

  ctx.scraping_agent.submit
  vf.unverify
end

#test_allObject



109
110
111
112
# File 'lib/at_coder_friends/cli.rb', line 109

def test_all
  ctx.sample_test_runner.test_all
  ctx.verifier.verify
end

#test_one(id = 1) ⇒ Object



105
106
107
# File 'lib/at_coder_friends/cli.rb', line 105

def test_one(id = 1)
  ctx.sample_test_runner.test_one(id)
end