A code autograder for student homework submissions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
754 B

  1. require 'date'
  2. module KGrader
  3. def self.parse_args(raw, num, keywords)
  4. args = []
  5. options = {}
  6. raw.each do |arg|
  7. if arg.include? '='
  8. key, val = arg.split('=', 2)
  9. key = key.to_sym
  10. yield "unknown keyword #{key}" unless keywords.include? key
  11. options[key] = case keywords[key]
  12. when :string
  13. val
  14. when :bool
  15. %w(true yes 1 t y).include? val.downcase
  16. when :array
  17. val.split(",").map! { |x| x.strip.downcase }
  18. when :datetime
  19. DateTime.parse(val)
  20. end
  21. else
  22. args << arg
  23. end
  24. end
  25. yield "too few arguments" if args.size < num
  26. yield "too many arguments" if args.size > num
  27. return args, options
  28. end
  29. end