Class: AtCoderFriends::CLI

Inherits:
Object
  • Object
show all
Includes:
PathUtil
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

Constants included from PathUtil

PathUtil::CASES_DIR, PathUtil::SMP_DIR

Instance Method Summary collapse

Methods included from PathUtil

cases_dir, contest_name, smp_dir, split_prg_path

Instance Method Details

#exec_command(command, path, id = nil) ⇒ Object



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

def exec_command(command, path, id = nil)
  case command
  when 'setup'
    setup(path)
  when 'test-one'
    test_one(path, id)
  when 'test-all'
    test_all(path)
  when 'submit'
    submit(path)
  when 'judge-one'
    judge_one(path, id)
  when 'judge-all'
    judge_all(path)
  when 'open-contest'
    open_contest(path)
  else
    raise ParamError, "unknown command: #{command}"
  end
end

#handle_exiting_optionObject



57
58
59
60
61
62
# File 'lib/at_coder_friends/cli.rb', line 57

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

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

#judge_all(path) ⇒ Object



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

def judge_all(path)
  JudgeTestRunner.new(path, @config).judge_all
end

#judge_one(path, id) ⇒ Object



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

def judge_one(path, id)
  id ||= ''
  JudgeTestRunner.new(path, @config).judge_one(id)
end

#open_contest(path) ⇒ Object



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

def open_contest(path)
  ScrapingAgent.new(contest_name(path), @config).open_contest
end

#parse_options!(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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
  end
  @usage = op.to_s
  @options = {}
  op.parse!(args)
rescue OptionParser::InvalidOption => e
  raise ParamError, e.message
end

#run(args = ARGV) ⇒ Object



24
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 24

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

  @config = ConfigLoader.load_config(args[1])
  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

#setup(path) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/at_coder_friends/cli.rb', line 85

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

  agent = ScrapingAgent.new(contest_name(path), @config)
  parser = FormatParser.new
  rb_gen = RubyGenerator.new
  cxx_gen = CxxGenerator.new
  emitter = Emitter.new(path)
  agent.fetch_all do |pbm|
    parser.process(pbm)
    rb_gen.process(pbm)
    cxx_gen.process(pbm)
    emitter.emit(pbm)
  end
end

#submit(path) ⇒ Object

Raises:



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

def submit(path)
  vf = Verifier.new(path)
  raise AppError, "#{vf.file} has not been tested." unless vf.verified?

  ScrapingAgent.new(contest_name(path), @config).submit(path)
  vf.unverify
end

#test_all(path) ⇒ Object



107
108
109
110
# File 'lib/at_coder_friends/cli.rb', line 107

def test_all(path)
  SampleTestRunner.new(path, @config).test_all
  Verifier.new(path).verify
end

#test_one(path, id) ⇒ Object



102
103
104
105
# File 'lib/at_coder_friends/cli.rb', line 102

def test_one(path, id)
  id ||= 1
  SampleTestRunner.new(path, @config).test_one(id)
end