下面列出了怎么用org.apache.commons.cli.MissingOptionException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String name = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
final String url = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_URL);
final String desc = getArg(properties, CommandOption.REGISTRY_CLIENT_DESC);
final RegistryDTO registryDTO = new RegistryDTO();
registryDTO.setName(name);
registryDTO.setUri(url);
registryDTO.setDescription(desc);
final RegistryClientEntity clientEntity = new RegistryClientEntity();
clientEntity.setComponent(registryDTO);
clientEntity.setRevision(getInitialRevisionDTO());
final RegistryClientEntity createdEntity = client.getControllerClient().createRegistryClient(clientEntity);
return new StringResult(createdEntity.getId(), getContext().isInteractive());
}
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String processGroupName = getRequiredArg(properties, CommandOption.PG_NAME);
final FlowClient flowClient = client.getFlowClient();
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final String rootPgId = flowClient.getRootGroupId();
final ProcessGroupDTO processGroupDTO = new ProcessGroupDTO();
processGroupDTO.setParentGroupId(rootPgId);
processGroupDTO.setName(processGroupName);
final ProcessGroupEntity pgEntity = new ProcessGroupEntity();
pgEntity.setComponent(processGroupDTO);
pgEntity.setRevision(getInitialRevisionDTO());
final ProcessGroupEntity createdEntity = pgClient.createProcessGroup(
rootPgId, pgEntity);
return new StringResult(createdEntity.getId(), getContext().isInteractive());
}
@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());
}
}
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String paramContextName = getRequiredArg(properties, CommandOption.PARAM_CONTEXT_NAME);
final String paramContextDesc = getArg(properties, CommandOption.PARAM_CONTEXT_DESC);
final ParameterContextDTO paramContextDTO = new ParameterContextDTO();
paramContextDTO.setName(paramContextName);
paramContextDTO.setParameters(Collections.emptySet());
if (!StringUtils.isBlank(paramContextDesc)) {
paramContextDTO.setDescription(paramContextDesc);
}
final ParameterContextEntity paramContextEntity = new ParameterContextEntity();
paramContextEntity.setComponent(paramContextDTO);
paramContextEntity.setRevision(getInitialRevisionDTO());
final ParamContextClient paramContextClient = client.getParamContextClient();
final ParameterContextEntity createdParamContext = paramContextClient.createParamContext(paramContextEntity);
return new StringResult(createdParamContext.getId(), isInteractive());
}
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String inputFile = getRequiredArg(properties, CommandOption.INPUT_SOURCE);
final URI uri = Paths.get(inputFile).toAbsolutePath().toUri();
final String contents = IOUtils.toString(uri, StandardCharsets.UTF_8);
final ObjectMapper objectMapper = JacksonUtils.getObjectMapper();
final ReportingTaskEntity deserializedTask = objectMapper.readValue(contents, ReportingTaskEntity.class);
if (deserializedTask == null) {
throw new IOException("Unable to deserialize reporting task from " + inputFile);
}
deserializedTask.setRevision(getInitialRevisionDTO());
final ControllerClient controllerClient = client.getControllerClient();
final ReportingTaskEntity createdEntity = controllerClient.createReportingTask(deserializedTask);
return new StringResult(String.valueOf(createdEntity.getId()), getContext().isInteractive());
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String rtId = getArg(properties, CommandOption.RT_ID);
final Set<ReportingTaskEntity> reportingTaskEntities = new HashSet<>();
if (StringUtils.isBlank(rtId)) {
final ReportingTasksEntity reportingTasksEntity = client.getFlowClient().getReportingTasks();
reportingTaskEntities.addAll(reportingTasksEntity.getReportingTasks());
} else {
reportingTaskEntities.add(client.getReportingTasksClient().getReportingTask(rtId));
}
activate(client, properties, reportingTaskEntities, "STOPPED");
return VoidResult.getInstance();
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final String paramContextId = getRequiredArg(properties, CommandOption.PARAM_CONTEXT_ID);
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final ProcessGroupEntity pgEntity = pgClient.getProcessGroup(pgId);
final ParameterContextReferenceEntity parameterContextReference = new ParameterContextReferenceEntity();
parameterContextReference.setId(paramContextId);
final ProcessGroupDTO updatedDTO = new ProcessGroupDTO();
updatedDTO.setId(pgId);
updatedDTO.setParameterContext(parameterContextReference);
final ProcessGroupEntity updatedEntity = new ProcessGroupEntity();
updatedEntity.setId(pgId);
updatedEntity.setComponent(updatedDTO);
updatedEntity.setRevision(pgEntity.getRevision());
pgClient.updateProcessGroup(updatedEntity);
return VoidResult.getInstance();
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String csId = getArg(properties, CommandOption.CS_ID);
final Set<ControllerServiceEntity> serviceEntities = new HashSet<>();
if (StringUtils.isBlank(csId)) {
final ControllerServicesEntity servicesEntity = client.getFlowClient().getControllerServices();
serviceEntities.addAll(servicesEntity.getControllerServices());
} else {
serviceEntities.add(client.getControllerServicesClient().getControllerService(csId));
}
activate(client, properties, serviceEntities, "ENABLED");
return VoidResult.getInstance();
}
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String inputFile = getRequiredArg(properties, CommandOption.INPUT_SOURCE);
final URI uri = Paths.get(inputFile).toAbsolutePath().toUri();
final String contents = IOUtils.toString(uri, StandardCharsets.UTF_8);
final ObjectMapper objectMapper = JacksonUtils.getObjectMapper();
final ControllerServiceEntity deserializedService = objectMapper.readValue(contents, ControllerServiceEntity.class);
if (deserializedService == null) {
throw new IOException("Unable to deserialize controller service version from " + inputFile);
}
deserializedService.setRevision(getInitialRevisionDTO());
final ControllerClient controllerClient = client.getControllerClient();
final ControllerServiceEntity createdEntity = controllerClient.createControllerService(deserializedService);
return new StringResult(String.valueOf(createdEntity.getId()), getContext().isInteractive());
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String csId = getArg(properties, CommandOption.CS_ID);
final Set<ControllerServiceEntity> serviceEntities = new HashSet<>();
if (StringUtils.isBlank(csId)) {
final ControllerServicesEntity servicesEntity = client.getFlowClient().getControllerServices();
serviceEntities.addAll(servicesEntity.getControllerServices());
} else {
serviceEntities.add(client.getControllerServicesClient().getControllerService(csId));
}
activate(client, properties, serviceEntities, "DISABLED");
return VoidResult.getInstance();
}
@Override
public StringResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String processorGroupId = getRequiredArg(properties, CommandOption.PG_ID);
final String inputFile = getRequiredArg(properties, CommandOption.INPUT_SOURCE);
final URI uri = Paths.get(inputFile).toAbsolutePath().toUri();
final String contents = IOUtils.toString(uri, StandardCharsets.UTF_8);
final ObjectMapper objectMapper = JacksonUtils.getObjectMapper();
final ControllerServiceEntity deserializedService = objectMapper.readValue(contents, ControllerServiceEntity.class);
if (deserializedService == null) {
throw new IOException("Unable to deserialize controller service version from " + inputFile);
}
deserializedService.setRevision(getInitialRevisionDTO());
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final ControllerServiceEntity createdEntity = pgClient.createControllerService(
processorGroupId, deserializedService);
return new StringResult(String.valueOf(createdEntity.getId()), getContext().isInteractive());
}
@Test
public void testParseArgument_missingInput() throws ParseException {
try {
DashboardArguments.readCommandLine();
Assert.fail("The argument should validate missing input");
} catch (MissingOptionException ex) {
// pass
}
}
public static CommandLine tryParse(
CommandLineParser parser,
Options options,
String[] args) {
try {
return parser.parse(options, args);
} catch (MissingOptionException moe) {
logger.error(moe.getMessage());
throw new CliParser.ParsingException(moe);
} catch (ParseException e) {
logger.error("Failed parsing options", e);
throw new CliParser.ParsingException(e);
}
}
@Test
public void testExportWithoutTransportIdRaisesException() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: tID");
Commands.main(new String[] {
"-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"export-transport"
});
}
@Test
public void testImportTransportWithoutTargetSystemRaisesException() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: ts");
Commands.main(new String[] {
"-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"import-transport",
"-tID", "999"
});
}
@Test
public void testImportTransportWithoutTransportIdRaisesException() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: tID");
Commands.main(new String[] {
"-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"import-transport",
"-ts", "A5X"
});
}
@Test
public void testUserNotProvided() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: u");
Commands.main(new String[]
{ "-e", "http://example.org:8000/endpoint",
"-p", "openSesame",
"-t", "CTS",
"get-transport-owner",
"-tID", "999"});
}
@Override
public VariableRegistryResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final ProcessGroupClient pgClient = client.getProcessGroupClient();
final VariableRegistryEntity varEntity = pgClient.getVariables(pgId);
return new VariableRegistryResult(getResultType(properties), varEntity);
}
@Test
public void testPasswordNotProvided() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: p");
Commands.main(new String[]
{ "-e", "http://example.org:8000/endpoint",
"-u", "me",
"-t", "CTS",
"get-transport-owner",
"-tID", "999"});
}
@Test
public void testTransportIdNotProvided() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: tID");
Commands.main(new String[]
{ "-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"get-transport-owner"});
}
@Test
public void testTransportIdNotProvided() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: tID");
Commands.main(new String[]
{ "-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"get-transport-target-system"});
}
@Override
public NodeResult doExecute(NiFiClient client, Properties properties) throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String nodeId = getRequiredArg(properties, CommandOption.NIFI_NODE_ID);
final ControllerClient controllerClient = client.getControllerClient();
NodeDTO nodeDto = new NodeDTO();
nodeDto.setNodeId(nodeId);
// TODO There are no constants for the OFFLOAD node statuses
nodeDto.setStatus("OFFLOADING");
NodeEntity nodeEntity = new NodeEntity();
nodeEntity.setNode(nodeDto);
NodeEntity nodeEntityResult = controllerClient.offloadNode(nodeId, nodeEntity);
return new NodeResult(getResultType(properties), nodeEntityResult);
}
@Test
public void getChangeTransportModifiableWithoutProvidingTransportId() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("tID");
Commands.main(new String[] {
"-u", SERVICE_USER,
"-p", SERVICE_PASSWORD,
"-e", SERVICE_ENDPOINT,
"-t", "SOLMAN",
"is-transport-modifiable",
"-cID", "8000038673"});
}
@Override
public VersionControlInfoResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final VersionControlInformationEntity entity = client.getVersionsClient().getVersionControlInfo(pgId);
if (entity.getVersionControlInformation() == null) {
throw new NiFiClientException("Process group is not under version control");
}
return new VersionControlInfoResult(getResultType(properties), entity);
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException, CommandException {
final ControllerClient controllerClient = client.getControllerClient();
final String id = getRequiredArg(properties, CommandOption.REGISTRY_CLIENT_ID);
final RegistryClientEntity existingRegClient = controllerClient.getRegistryClient(id);
if (existingRegClient == null) {
throw new CommandException("Registry client does not exist for id " + id);
}
final String name = getArg(properties, CommandOption.REGISTRY_CLIENT_NAME);
final String url = getArg(properties, CommandOption.REGISTRY_CLIENT_URL);
final String desc = getArg(properties, CommandOption.REGISTRY_CLIENT_DESC);
if (StringUtils.isBlank(name) && StringUtils.isBlank(url) && StringUtils.isBlank(desc)) {
throw new CommandException("Name, url, and desc were all blank, nothing to update");
}
if (StringUtils.isNotBlank(name)) {
existingRegClient.getComponent().setName(name);
}
if (StringUtils.isNotBlank(url)) {
existingRegClient.getComponent().setUri(url);
}
if (StringUtils.isNotBlank(desc)) {
existingRegClient.getComponent().setDescription(desc);
}
final String clientId = getContext().getSession().getNiFiClientID();
existingRegClient.getRevision().setClientId(clientId);
controllerClient.updateRegistryClient(existingRegClient);
return VoidResult.getInstance();
}
@Test
public void testCreateTransportWithoutTargetSystemRaisesException() throws Exception {
thrown.expect(MissingOptionException.class);
thrown.expectMessage("Missing required option: ts");
Commands.main(new String[]
{ "-e", "http://example.org:8000/endpoint",
"-u", "me",
"-p", "openSesame",
"-t", "CTS",
"create-transport",
"-tt", "K",
"-d", "desc"});
}
@Override
public VoidResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, MissingOptionException {
final String pgId = getRequiredArg(properties, CommandOption.PG_ID);
final ScheduleComponentsEntity entity = new ScheduleComponentsEntity();
entity.setId(pgId);
entity.setState(ScheduleComponentsEntity.STATE_STOPPED);
final FlowClient flowClient = client.getFlowClient();
final ScheduleComponentsEntity resultEntity = flowClient.scheduleProcessGroupComponents(pgId, entity);
return VoidResult.getInstance();
}
@Override
public final int run(String[] args) throws Exception {
try {
CommandLine cmd = new PosixParser(){
@Override
protected void checkRequiredOptions() throws MissingOptionException {
if (!cmd.hasOption('h') && !cmd.hasOption('v')) {
super.checkRequiredOptions();
}
}
}.parse(options, args, cmdMoreArgs);
if (args.length == 0 || cmd.hasOption('h')) {
printHelp();
return -1;
}
if (cmd.hasOption('v')) {
System.out.println(name + " version " + getVersion());
return 0;
}
if (!cmdMoreArgs && !cmd.getArgList().isEmpty()) {
throw new ParseException("Unknown arguments: " + cmd.getArgList().toString());
}
for (String opt : singleOptions) {
String s[] = cmd.getOptionValues(opt);
if (s != null && s.length > 1) throw new ParseException("Multiple values for option: " + opt);
}
return run(cmd);
} catch (Exception exp) {
System.out.println(exp.getMessage());
printHelp();
throw exp;
}
}
@Override
public AccessPolicyResult doExecute(final NiFiClient client, final Properties properties)
throws NiFiClientException, IOException, CommandException, MissingOptionException {
final PoliciesClient policyClient = client.getPoliciesClient();
final String resource = getRequiredArg(properties, CommandOption.POLICY_RESOURCE);
final AccessPolicyAction actionType = AccessPolicyAction.valueOf(
getRequiredArg(properties, CommandOption.POLICY_ACTION).toUpperCase().trim());
return new AccessPolicyResult(getResultType(properties), policyClient.getAccessPolicy(
resource, actionType.toString().toLowerCase()));
}
private String getRegistryClientId(final NiFiClient nifiClient, final String registryClientBaseUrl, final boolean isInteractive)
throws NiFiClientException, IOException, MissingOptionException {
final Properties getRegClientProps = new Properties();
getRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_URL.getLongName(), registryClientBaseUrl);
String registryClientId;
try {
final RegistryClientIDResult registryClientResult = getRegistryClientId.doExecute(nifiClient, getRegClientProps);
registryClientId = registryClientResult.getResult().getId();
if (isInteractive) {
println();
println("Found existing registry client '" + registryClientResult.getResult().getName() + "'...");
}
} catch (Exception e) {
registryClientId = null;
}
if (registryClientId == null) {
final Properties createRegClientProps = new Properties();
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_NAME.getLongName(), REG_CLIENT_NAME);
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_DESC.getLongName(), REG_CLIENT_DESC + new Date().toString());
createRegClientProps.setProperty(CommandOption.REGISTRY_CLIENT_URL.getLongName(), registryClientBaseUrl);
final StringResult createdRegClient = createRegistryClient.doExecute(nifiClient, createRegClientProps);
registryClientId = createdRegClient.getResult();
if (isInteractive) {
println();
println("Created new registry client '" + REG_CLIENT_NAME + "'...");
}
}
return registryClientId;
}