下面列出了怎么用org.apache.commons.cli.UnrecognizedOptionException的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
public static String process(ParseException e) {
if (e instanceof MissingOptionException) {
StringBuilder sb = new StringBuilder();
Iterator<String> options = ((MissingOptionException) e).getMissingOptions().iterator();
while (options.hasNext()) {
sb.append(options.next());
if (options.hasNext()) {
sb.append(", ");
}
}
return String.format("Missing required option(s) %s.", sb.toString());
}
else if (e instanceof MissingArgumentException) {
return String.format("%s is missing a required argument.",
((MissingArgumentException) e).getOption());
}
else if (e instanceof UnrecognizedOptionException) {
return String.format("%s is not a valid option.",
((UnrecognizedOptionException) e).getOption());
}
else {
return String.format("%s.", e.getMessage());
}
}
/**
* Checks if the specifying some random argument work throws an exception.
*/
@Test
public void checkRandomArgDoesNotWork() throws KeywordOptimizerException {
thrown.expect(KeywordOptimizerException.class);
thrown.expectCause(isA(UnrecognizedOptionException.class));
KeywordOptimizer.run("-x");
}
@Test(expected = UnrecognizedOptionException.class)
public void testParseArgumentsUnrecognizedOptionException() throws ParseException
{
ArgumentParser argParser = new ArgumentParser("");
argParser.addArgument("a", "configured_option", false, "Some flag parameter", false);
argParser.parseArguments(new String[] {"-b", "unrecognized_option"});
}
@Override
public void execute(CommandLine options) throws Exception {
String[] args = options.getArgs();
if (args.length < min) {
throw new MissingArgumentException("missing required arguments");
}
if (args.length > max) {
throw new UnrecognizedOptionException("unknown extra argument \"" + args[max] + "\"");
}
}
@Override
public void execute(CommandLine options) throws Exception {
String[] args = options.getArgs();
if (args.length < min) {
throw new MissingArgumentException("missing required arguments");
}
if (args.length > max) {
throw new UnrecognizedOptionException("unknown extra argument \"" + args[max] + "\"");
}
}
@Test(expected = UnrecognizedOptionException.class)
public void testUnknownOptionIsPresent() throws Exception
{
VividusInitializationChecker.main(new String[] { "--any" });
}
@Test
public void testUnknownOptionIsPresent()
{
assertThrows(UnrecognizedOptionException.class,
() -> BddStepsCounter.main(new String[] { "--any", DIR_VALUE }));
}
@Test
public void testUnknownOptionIsPresent()
{
assertThrows(UnrecognizedOptionException.class, () ->
KnownIssueValidator.main(new String[]{"--any"}));
}
@Test
public void testUnknownOptionIsPresent()
{
assertThrows(UnrecognizedOptionException.class, () ->
BddScenariosCounter.main(new String[] { "--any", DIR_VALUE}));
}
/**
* rdflint entry point.
*/
public static void main(String[] args) throws ParseException, IOException {
// Parse CommandLine Parameter
Options options = new Options();
options.addOption("baseuri", true, "RDF base URI");
options.addOption("targetdir", true, "Target Directory Path");
options.addOption("outputdir", true, "Output Directory Path");
options.addOption("origindir", true, "Origin Dataset Directory Path");
options.addOption("config", true, "Configuration file Path");
options.addOption("suppress", true, "Suppress problems file Path");
options.addOption("minErrorLevel", true,
"Minimal logging level which is considered an error, e.g. INFO, WARN, ERROR");
options.addOption("i", false, "Interactive mode");
options.addOption("ls", false, "Language Server mode (experimental)");
options.addOption("h", false, "Print usage");
options.addOption("v", false, "Print version");
options.addOption("vv", false, "Verbose logging (for debugging)");
CommandLine cmd = null;
try {
CommandLineParser parser = new DefaultParser();
cmd = parser.parse(options, args);
} catch (UnrecognizedOptionException e) {
System.out.println("Unrecognized option: " + e.getOption()); // NOPMD
System.exit(1);
}
// print version
if (cmd.hasOption("v")) {
System.out.println("rdflint " + VERSION); // NOPMD
return;
}
// print usage
if (cmd.hasOption("h")) {
HelpFormatter f = new HelpFormatter();
f.printHelp("rdflint [options]", options);
return;
}
// verbose logging mode
if (cmd.hasOption("vv")) {
Logger.getLogger("com.github.imas.rdflint").setLevel(Level.TRACE);
}
// Execute Language Server Mode
if (cmd.hasOption("ls")) {
RdfLintLanguageServer server = new RdfLintLanguageServer();
Launcher<LanguageClient> launcher = LSPLauncher
.createServerLauncher(server, System.in, System.out);
LanguageClient client = launcher.getRemoteProxy();
server.connect(client);
launcher.startListening();
return;
}
// Set parameter
Map<String, String> cmdOptions = new ConcurrentHashMap<>();
for (String key :
Arrays.asList("targetdir", "config", "suppress", "outputdir", "baseuri", "origindir")) {
if (cmd.hasOption(key)) {
cmdOptions.put(key, cmd.getOptionValue(key));
}
}
// Main procedure
if (cmd.hasOption("i")) {
// Execute Interactive mode
InteractiveMode imode = new InteractiveMode();
imode.execute(cmdOptions);
} else {
// Execute linter
RdfLint lint = new RdfLint();
RdfLintParameters params = ConfigurationLoader.loadParameters(cmdOptions);
LintProblemSet problems = lint.lintRdfDataSet(params, params.getTargetDir());
if (problems.hasProblem()) {
Path problemsPath = Paths.get(params.getOutputDir() + "/rdflint-problems.yml");
LintProblemFormatter.out(System.out, problems);
LintProblemFormatter.yaml(Files.newOutputStream(problemsPath), problems);
final String minErrorLevel = cmd.getOptionValue("minErrorLevel", "WARN");
final LintProblem.ErrorLevel errorLevel = LintProblem.ErrorLevel.valueOf(minErrorLevel);
if (problems.hasProblemOfLevelOrWorse(errorLevel)) {
System.exit(1);
}
}
}
}
@Test(expected = UnrecognizedOptionException.class)
public void mainPrintUsage5Test() throws Exception {
Main.main(new String[]{"-nifi", "http://localhost:8080/nifi-api", "-branch", "\"root>N2\"", "-conf", "adr", "-m", "undeploy", "-userErr", "user"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testRunInvalid() throws Exception {
new HalyardBulkDelete().run(new String[]{"-invalid"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testRunInvalid() throws Exception {
new HalyardElasticIndexer().run(new String[]{"-invalid"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testInvalid() throws Exception {
HalyardMain.main(new String[]{"invalid"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testRunInvalid() throws Exception {
new HalyardSummary().run(new String[]{"-invalid"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testRunInvalid() throws Exception {
new HalyardStats().run(new String[]{"-invalid"});
}
@Test(expected = UnrecognizedOptionException.class)
public void testRunInvalid() throws Exception {
new HalyardBulkExport().run(new String[]{"--invalid"});
}