Last active
May 9, 2016 10:38
-
-
Save schoettl/e70a58024e5a0cf2482ead36f5c1f3f3 to your computer and use it in GitHub Desktop.
A wrapper object around the options Map returned by Docopt.parse(...)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Released under | |
* The MIT License (MIT) | |
* Copyright (c) 2016 Jakob Schöttl | |
*/ | |
package edu.hm.cs.vadere.seating.traingen; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
/** | |
* This is a wrapper class around the options Map returned by <tt>Docopt.parse(...)</tt> method. It | |
* should be seen as a template! Feel free to add getter methods, change method names or | |
* implementation, ... | |
* | |
* Usage: | |
* <tt>DocoptOptionsWrapper opts = new DocoptOptionsWrapper(new Docopt(doc).parse(args));</tt> | |
* | |
* @see org.docopt.Docopt Docopt | |
* @see <a href="http://docopt.org/">docopt.org</a> | |
* @author Jakob Schöttl | |
*/ | |
public class DocoptOptionsWrapper { | |
Map<String, Object> options; | |
/** | |
* Create a wrapper object based on the <tt>Map<String, Object></tt> returned by | |
* <tt>Docopt.parse(...)</tt>. | |
* | |
* @param options | |
*/ | |
public DocoptOptionsWrapper(Map<String, Object> options) { | |
this.options = options; | |
} | |
/** | |
* Return the original Map this wrapper is based on. | |
*/ | |
public Map<String, Object> getOptionMap() { | |
return options; | |
} | |
public int getOptionArgumentInt(String option) { | |
try { | |
return Integer.valueOf((String) options.get(option)); | |
} catch (NumberFormatException e) { | |
throw new IllegalArgumentException("argument for " + option + " must be an integer."); | |
} | |
} | |
public boolean isFlagOptionPresent(String option) { | |
return (boolean) options.get(option); | |
} | |
@SuppressWarnings("unchecked") | |
public List<Stop> getStops() { | |
List<String> list = (List<String>) options.get("--stop"); | |
return list.stream() | |
.map(Stop::parseStopArgument) | |
.collect(Collectors.toList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment