scalop

Scala option parser

License

License

GroupId

GroupId

com.loopfor.scalop
ArtifactId

ArtifactId

scalop_2.12
Last Version

Last Version

2.2
Release Date

Release Date

Type

Type

jar
Description

Description

scalop
Scala option parser
Project URL

Project URL

https://github.com/davidledwards/scalop
Project Organization

Project Organization

com.loopfor.scalop
Source Code Management

Source Code Management

https://github.com/davidledwards/scalop

Download scalop_2.12

How to add to project

<!-- https://jarcasting.com/artifacts/com.loopfor.scalop/scalop_2.12/ -->
<dependency>
    <groupId>com.loopfor.scalop</groupId>
    <artifactId>scalop_2.12</artifactId>
    <version>2.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.loopfor.scalop/scalop_2.12/
implementation 'com.loopfor.scalop:scalop_2.12:2.2'
// https://jarcasting.com/artifacts/com.loopfor.scalop/scalop_2.12/
implementation ("com.loopfor.scalop:scalop_2.12:2.2")
'com.loopfor.scalop:scalop_2.12:jar:2.2'
<dependency org="com.loopfor.scalop" name="scalop_2.12" rev="2.2">
  <artifact name="scalop_2.12" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.loopfor.scalop', module='scalop_2.12', version='2.2')
)
libraryDependencies += "com.loopfor.scalop" % "scalop_2.12" % "2.2"
[com.loopfor.scalop/scalop_2.12 "2.2"]

Dependencies

compile (1)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.12.2

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.12 jar 3.0.1

Project Modules

There are no modules declared in this project.

Overview

Yet another option parser for Scala, but this one is incredibly simple and rich enough in features to satisfy most needs.

The project page with links to documentation can be found at https://davidedwards.io/scalop.

Usage

Define

val opts =
  // no argument, type is Boolean
  // sets value to true if --help or -? detected
  // default value is false
  ("help", '?') ~> just(true) ~~ false ::

  // single argument, type is String
  // no default value
  ("host", 'h') ~> as[String] ::

  // single argument, type is Int
  // default value is 7777
  ("port", 'p') ~> as[Int] ~~ 7777 ::

  // single argument, type is Duration
  // default value is Duration.Inf
  ("timeout", 't') ~> as { arg: Long => Math.max(arg, 0).seconds } ~~ Duration.Inf ::

  // optional single argument, type is Option[Int]
  // value is None if -X not followed by argument
  // otherwise default value is Some(0)
  'X' ~> maybe[Int] ~~ Some(0) ::

  // single argument, type is Seq[File]
  // multiple appearances of --file or -f append argument to sequence
  // default value is empty sequence
  ("file", 'f') ~>+ as[File] ~~ Seq.empty ::
  Nil

Parse

def main(args: Array[String]): Unit = {
  val optr = try opts <~ args catch {
    case e: OptException => ...
  }
  ...
}

Read

val help: Boolean = optr("help")
val host: String = optr.get("host") getOrElse "localhost"
val port: Int = optr("port")
val timeout: Duration = optr("timeout")
val x = optr[Option[Int]]("X") getOrElse { Random.nextInt() }
val files: Seq[File] = optr("file")

Documentation

API documentation can be found here.

Dependency Information

This library is published in the Maven Central Repository. Dependency information can be found here.

License

Copyright 2020 David Edwards

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Versions

Version
2.2