<commit_before>package com.trueaccord.scalapb

import protocbridge.ProtocBridge

import scalapb.ScalaPbCodeGenerator

object ScalaPBC {
  def main(args: Array[String]): Unit = {
    val (versionFlag, protocArgs) =
      if (args.length >= 1 && args(0).startsWith("-v")) {
        (args.head, args.tail)
      } else {
        ("-v300", args)
      }

    val code = ProtocBridge.runWithGenerators(
      a => com.github.os72.protocjar.Protoc.runProtoc(versionFlag +: a.toArray),
      protocArgs,
      Seq("scala" -> ScalaPbCodeGenerator))

    sys.exit(code)
  }
}<commit_msg>Scalapbc: Add --throw to throw on error instead of invoking sys.exit<commit_after>package com.trueaccord.scalapb

import protocbridge.ProtocBridge

import scalapb.ScalaPbCodeGenerator

case class Config(
  version: String = "-v300",
  throwException: Boolean = false,
  args: Seq[String] = Seq.empty)

class ScalaPbcException(msg: String) extends RuntimeException(msg)

object ScalaPBC {
  def processArgs(args: Array[String]): Config = {
    case class State(cfg: Config, passThrough: Boolean)

    args.foldLeft(State(Config(), false)) {
      case (state, item) =>
        (state.passThrough, item) match {
          case (false, v) if v.startsWith("-v") => state.copy(cfg = state.cfg.copy(version = v))
          case (false, "--throw") => state.copy(cfg = state.cfg.copy(throwException = true))
          case (_, other) => state.copy(
            passThrough = true, cfg=state.cfg.copy(args = state.cfg.args :+ other))
        }
    }.cfg
  }

  def main(args: Array[String]): Unit = {
    val config = processArgs(args)

    val code = ProtocBridge.runWithGenerators(
      a => com.github.os72.protocjar.Protoc.runProtoc(config.version +: a.toArray),
      config.args,
      Seq("scala" -> ScalaPbCodeGenerator))

    if (!config.throwException) {
      sys.exit(code)
    } else {
      if (code != 0) {
        throw new ScalaPbcException(s"Exit with code $code")
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.13539553752535496, 'fraction_numerical': 0.006592292089249493, 'mean_word_length': 3.0596707818930042, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '13962', 'n_tokens_mistral': 688, 'n_tokens_neox': 646, 'n_words': 139}