All Downloads are FREE. Search and download functionalities are using the official Maven repository.

caseapp.core.argparser.AccumulatorArgParser.scala Maven / Gradle / Ivy

There is a newer version: 2.0.0-M8
Show newest version
package caseapp.core.argparser

import caseapp.core.Error

final case class AccumulatorArgParser[T](
  description: String,
  parse: (Option[T], String) => Either[Error, T]
) extends ArgParser[T] {

  def apply(current: Option[T], value: String): Either[Error, T] =
    parse(current, value)

}

object AccumulatorArgParser {

  def from[T](description: String)(parse: (Option[T], String) => Either[Error, T]): AccumulatorArgParser[T] =
    AccumulatorArgParser(description, parse)



  // FIXME (former comment, deprecated?) may not be fine with sequences/options of flags

  def list[T](implicit parser: ArgParser[T]): AccumulatorArgParser[List[T]] =
    from(parser.description + "*") { (prevOpt, s) =>
      parser(None, s)
        .right
        .map { t =>
          // inefficient for big lists
          prevOpt.getOrElse(Nil) :+ t
        }
    }

  def vector[T](implicit parser: ArgParser[T]): AccumulatorArgParser[Vector[T]] =
    from(parser.description + "*") { (prevOpt, s) =>
      parser(None, s)
        .right
        .map { t =>
          prevOpt.getOrElse(Vector.empty) :+ t
        }
    }

  def option[T](implicit parser: ArgParser[T]): AccumulatorArgParser[Option[T]] =
    from(parser.description + "?") { (prevOpt, s) =>
      parser(prevOpt.flatten, s)
        .right
        .map(Some(_))
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy