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

de.halcony.argparse.parsing.FlagArgument.scala Maven / Gradle / Ivy

Go to download

A re-imagined implementation of the handy python-argparse functionality for scala

There is a newer version: 2.0.3
Show newest version
package de.halcony.argparse.parsing

import de.halcony.argparse.ParsingResult

class FlagArgument protected (override val name: String,
                              shortName: Char,
                              longName: String = "",
                              override val description: String = "")
    extends ParsedArgument[Boolean](
      name,
      description,
      _ => throw new RuntimeException("no parsing of flag arguments")) {

  val short: String = s"-$shortName"
  val long: String = s"--$longName"

  override def parse(args: Iterable[String])(
      implicit result: ParsingResult): Iterable[String] = {
    if (result.getOptional[Boolean](name).isEmpty) {
      result.addResult(name, new ResultValue[Boolean](value = false))
    }
    if (args.nonEmpty) {
      if (short == args.head || long == args.head) {
        result.addResult(name, new ResultValue[Boolean](value = true))
        args.tail
      } else {
        args
      }
    } else {
      args
    }
  }

  override def help(): String = s"$short/$long $description"

}

object FlagArgument {

  def apply(name: String,
            short: Char,
            long: String,
            description: String = ""): FlagArgument = {
    new FlagArgument(name, short, long, description)
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy