Skip to content

Strnadj/JavaOptParser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Option Parser

Build Status Coverage Status

Info

Java Simple Option parser is inspired from ruby OptParser class (rdoc/OptionParser). This work has been created as part of Semester Work on University of West Bohemia (zcu.cz).

Installation

Add java source files in to your project and import them in files in which you want to use them.

Usage

You can find usage in tests or in example bellow. JavaOpt class provides basic fluent interfaces for creating option definitions.

Difference between Option and Path/Expression

  • Option - classic option (ex: -v, --verbose)
  • Path/Expression - string or path after option fields
cd *path/expression*
cd ~/strnadj/Projects
cp *option* *path/expression* *path/expression*
cp -R ~/strnadj/Projects ~/strnadj/Projects_bacup/

Example

We want basic ls command with options:

  • Only files
  • Only directories
  • With hidden files
  • Help
  • And optional path

Create parser definition

OptParser options = OptParser.createOptionParser("ls", "Show directory
contents")
    .addOption('f', "files", OptParser.OPTIONAL, "", "Just files")
    .addOption('d', "directories", OptParser.OPTIONAL, "", "Just
directories")
    .addOption('h', "help", OptParser.OPTIONAL, "", "Show this help")
    .addPathOrExpression("path", OptParser.OPTIONAL, ".", "Directory to
listing");

Create option with required value?

.addOptionRequiredValue('t', "target", OptParser.OPTIONAL, null, "Target
folder")

Get help?

System.out.println(options.getHelp());
Usage: ls [options] "path"

Optional options:
	-f, --files         Just files
	-d, --directories   Just directories	
	-h, --help          Show this help

Parse parameters

try {
    options.parseArguments(args);
} catch(Exception e) {
    System.err.println(e.getMessage());
    System.exit(-1);
}

Getting parameters

// Get if parameter was set
if (options.getOption("help") != null) {
    // Parameter help was set
}

// Values?
if (options.getOption("directories") != null) {
    options.getOption("directories").value();
}

Licence (DWTFYWTPL)

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE, Version 2, December 1991

Copyright (C) 2014 Jan Strnadek [email protected]

Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  1. You just DO WHAT THE FUCK YOU WANT TO.

I renounce responsibility for any damage or whatever this library caused to who use it.

How to contribute?

  1. Fork it!
  2. Do your changes!
  3. Create pull-request and open issue!

Thanks Strnadj :)

About

Java Option parser inspired by OptParser ruby class

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages