GithubHelp home page GithubHelp logo

jarpa's Introduction

Jarpa – Java ARguments PArser

A lightweight parser of command line arguments.

Features

  • Very lightweight
  • Only throws JarpaExceptions with friendly error messages – you can catch them and show the user the message
  • Completely typesafe – handles parsing for you
  • Extensible in the previous regard

Quick Examples

Note that the following examples use the static import fi.purkka.jarpa.JarpaArg.* to make the code shorter.

Everything begins by obtaining a JarpaArgs instance; args is of type String[]. JarpaArgs implements AutoCloseable to permit use in try-catch blocks; the close() method calls finish() which makes sure that the user hasn't entered any unknown arguments.

try(JarpaArgs jargs = JarpaParser.parsing(args).parse()) {
    // code
} catch(JarpaException e) {
    // handle errors
}

JarpaArgs has a get() method that takes a JarpaArg. The static methods of JarpaArg provide facilities for obtaining values of different types.

String string = jargs.get(string("--name"));
int integer = jargs.get(integer("--id"));
double decimal = jargs.get(decimal("--weight"));
String[] strings = jargs.get(stringArray("--versions"));

Flags are special types of JarpaArg. They are associated with a boolean value that indicates whether they are present. Unlike other types, they will not cause an exception to be thrown if missing).

boolean hasFlag = jargs.get(flag("-f"));

Kind of like flags, optional arguments can freely be omitted. They provide access to an Optional instance.

int repeat = jargs.get(integer("--repeat").optional()).orElse(1);

Aliases can be used to allow the user use shorter or longer versions of arguments as they wish. Only one given alias may be used at the same time; otherwise, an exception is raised.

boolean verbose = jargs.get(flag("--verbose").alias("-v"));

Other types of JarpaArg can be used if a parse method is provided.

MyClass myClass = jargs.get(object("--myclass", MyClass::parse));

Licence

Jarpa is licenced with the Unlicence.

jarpa's People

Contributors

purkkafi avatar

Stargazers

 avatar

Watchers

 avatar

jarpa's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.