picocli.CommandLine.PicocliException Java Examples

The following examples show how to use picocli.CommandLine.PicocliException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Main.java    From depends with MIT License 6 votes vote down vote up
public static void main(String[] args) {
	try {
		LangRegister langRegister = new LangRegister();
		langRegister.register();
		DependsCommand app = CommandLine.populateCommand(new DependsCommand(), args);
		if (app.help) {
			CommandLine.usage(new DependsCommand(), System.out);
			System.exit(0);
		}
		executeCommand(app);
	} catch (Exception e) {
		if (e instanceof PicocliException) {
			CommandLine.usage(new DependsCommand(), System.out);
		} else if (e instanceof ParameterException){
			System.err.println(e.getMessage());
		}else {
			System.err.println("Exception encountered. If it is a design error, please report issue to us." );
			e.printStackTrace();
		}
		System.exit(0);
	}
}
 
Example #2
Source File: OptionMethodImplTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testPicocliExceptionFromMethod() {
    class App {
        @Option(names = "--pico")
        public void picocliException(String value) { throw new PicocliException("Pico!"); }
    }

    CommandLine parser = new CommandLine(new App());
    try {
        parser.parseArgs("--pico", "abc");
        fail("Expected exception");
    } catch (ParameterException ex) {
        assertNotNull(ex.getCause());
        assertTrue(ex.getCause() instanceof PicocliException);
        assertEquals("Pico!", ex.getCause().getMessage());
        assertEquals("PicocliException: Pico! while processing argument at or before arg[1] 'abc' in [--pico, abc]: picocli.CommandLine$PicocliException: Pico!", ex.getMessage());
    }
}
 
Example #3
Source File: ConjureCliTest.java    From conjure with Apache License 2.0 5 votes vote down vote up
@Test
public void throwsWhenSubCommandIsNotCompile() {
    String[] args = {
        "compiles", folder.getRoot().getAbsolutePath(), folder.getRoot().getAbsolutePath(),
    };
    assertThatThrownBy(() -> CommandLine.populateCommand(new ConjureCli(), args))
            .isInstanceOf(PicocliException.class)
            .hasMessageContaining("Unmatched arguments");
}
 
Example #4
Source File: ModelFieldBindingTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldBindingDoesNotSetAccessible() throws Exception {
    Field f = ModelMethodBindingBean.class.getDeclaredField("x");
    FieldBinding binding = new FieldBinding(new ModelMethodBindingBean(), f);
    try {
        binding.get();
        fail("Expected exception");
    } catch (PicocliException ok) {
        assertThat("not accessible", ok.getCause() instanceof IllegalAccessException);
    }
}
 
Example #5
Source File: ModelMethodBindingTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodMustBeAccessible() throws Exception {
    Method setX = ModelMethodBindingBean.class.getDeclaredMethod("setX", int.class);
    MethodBinding binding = new MethodBinding(new ObjectScope(new ModelMethodBindingBean()), setX, CommandSpec.create());
    try {
        binding.set(1);
        fail("Expected exception");
    } catch (PicocliException ok) {
        assertThat("not accessible", ok.getCause() instanceof IllegalAccessException);
    }
}
 
Example #6
Source File: ModelUnmatchedArgsBindingTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnmatchedArgsBinding_forStringCollectionSupplier_exceptionsRethrownAsPicocliException() {
    class ThrowingGetter implements IGetter {
        public <T> T get() { throw new RuntimeException("test"); }
    }
    try {
        UnmatchedArgsBinding.forStringCollectionSupplier(new ThrowingGetter()).addAll(new String[0]);
        fail("Expected exception");
    } catch (PicocliException ex) {
        assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not add unmatched argument array '[]' to collection returned by getter ("));
        assertTrue(ex.getMessage(), ex.getMessage().endsWith("): java.lang.RuntimeException: test"));
    }
}
 
Example #7
Source File: ModelUnmatchedArgsBindingTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnmatchedArgsBinding_forStringArrayConsumer_exceptionsRethrownAsPicocliException() {
    class ThrowingSetter implements ISetter {
        public <T> T set(T value) { throw new RuntimeException("test"); }
    }
    try {
        UnmatchedArgsBinding.forStringArrayConsumer(new ThrowingSetter()).addAll(new String[0]);
        fail("Expected exception");
    } catch (PicocliException ex) {
        assertTrue(ex.getMessage(), ex.getMessage().startsWith("Could not invoke setter "));
        assertTrue(ex.getMessage(), ex.getMessage().contains(") with unmatched argument array '[]': java.lang.RuntimeException: test"));
    }
}
 
Example #8
Source File: GenerateOzoneRequiredConfigurations.java    From hadoop-ozone with Apache License 2.0 4 votes vote down vote up
/**
 * Generate ozone-site.xml at specified path.
 * @param path
 * @throws PicocliException
 * @throws JAXBException
 */
public static void generateConfigurations(String path) throws
    PicocliException, JAXBException, IOException {

  if (!isValidPath(path)) {
    throw new PicocliException("Invalid directory path.");
  }

  if (!canWrite(path)) {
    throw new PicocliException("Insufficient permission.");
  }

  OzoneConfiguration oc = new OzoneConfiguration();

  ClassLoader cL = Thread.currentThread().getContextClassLoader();
  if (cL == null) {
    cL = OzoneConfiguration.class.getClassLoader();
  }
  URL url = cL.getResource("ozone-default.xml");

  List<OzoneConfiguration.Property> allProperties =
      oc.readPropertyFromXml(url);

  List<OzoneConfiguration.Property> requiredProperties = new ArrayList<>();

  for (OzoneConfiguration.Property p : allProperties) {
    if (p.getTag() != null && p.getTag().contains("REQUIRED")) {
      if (p.getName().equalsIgnoreCase(
          OzoneConfigKeys.OZONE_METADATA_DIRS)) {
        p.setValue(System.getProperty(OzoneConsts.JAVA_TMP_DIR));
      } else if (p.getName().equalsIgnoreCase(
          OMConfigKeys.OZONE_OM_ADDRESS_KEY)
          || p.getName().equalsIgnoreCase(ScmConfigKeys.OZONE_SCM_NAMES)
          || p.getName().equalsIgnoreCase(
            ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY)) {
        p.setValue(OzoneConsts.LOCALHOST);
      }

      requiredProperties.add(p);
    }
  }

  OzoneConfiguration.XMLConfiguration requiredConfig =
      new OzoneConfiguration.XMLConfiguration();
  requiredConfig.setProperties(requiredProperties);

  File output = new File(path, "ozone-site.xml");
  if(output.createNewFile()){
    JAXBContext context =
        JAXBContext.newInstance(OzoneConfiguration.XMLConfiguration.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(requiredConfig, output);

    System.out.println("ozone-site.xml has been generated at " + path);
  } else {
    System.out.printf("ozone-site.xml already exists at %s and " +
        "will not be overwritten%n", path);
  }

}