List of usage examples for org.apache.commons.cli OptionGroup OptionGroup
OptionGroup
From source file:org.dspace.eperson.EPerson.java
/** * Tool for manipulating user accounts.//from w ww. j a va2 s . co m */ public static void main(String argv[]) throws ParseException, SQLException { final OptionGroup VERBS = new OptionGroup(); VERBS.addOption(VERB_ADD); VERBS.addOption(VERB_DELETE); VERBS.addOption(VERB_LIST); VERBS.addOption(VERB_MODIFY); final Options globalOptions = new Options(); globalOptions.addOptionGroup(VERBS); globalOptions.addOption("h", "help", false, "explain options"); GnuParser parser = new GnuParser(); CommandLine command = parser.parse(globalOptions, argv, true); Context context = new Context(); // Disable authorization since this only runs from the local commandline. context.turnOffAuthorisationSystem(); int status = 0; if (command.hasOption(VERB_ADD.getOpt())) { status = cmdAdd(context, argv); } else if (command.hasOption(VERB_DELETE.getOpt())) { status = cmdDelete(context, argv); } else if (command.hasOption(VERB_MODIFY.getOpt())) { status = cmdModify(context, argv); } else if (command.hasOption(VERB_LIST.getOpt())) { status = cmdList(context, argv); } else if (command.hasOption('h')) { new HelpFormatter().printHelp("user [options]", globalOptions); } else { System.err.println("Unknown operation."); new HelpFormatter().printHelp("user [options]", globalOptions); context.abort(); status = 1; throw new IllegalArgumentException(); } if (context.isValid()) { try { context.complete(); } catch (SQLException ex) { System.err.println(ex.getMessage()); } } }
From source file:org.dspace.eperson.EPerson.java
/** Command to create an EPerson. */ private static int cmdAdd(Context context, String[] argv) { Options options = new Options(); options.addOption(VERB_ADD);/* w w w .ja va2 s. c om*/ final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption(OPT_GIVENNAME); options.addOption(OPT_SURNAME); options.addOption(OPT_PHONE); options.addOption(OPT_LANGUAGE); options.addOption(OPT_REQUIRE_CERTIFICATE); Option option = new Option("p", "password", true, "password to match the EPerson name"); options.addOption(option); options.addOption("h", "help", false, "explain --add options"); // Rescan the command for more details. GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --add [options]", options); return 0; } // Check that we got sufficient credentials to define a user. if ((!command.hasOption(OPT_EMAIL.getOpt())) && (!command.hasOption(OPT_NETID.getOpt()))) { System.err.println("You must provide an email address or a netid to identify the new user."); return 1; } if (!command.hasOption('p')) { System.err.println("You must provide a password for the new user."); return 1; } // Create! EPerson eperson = null; try { eperson = create(context); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } eperson.setCanLogIn(true); eperson.setSelfRegistered(false); eperson.setEmail(command.getOptionValue(OPT_EMAIL.getOpt())); eperson.setFirstName(command.getOptionValue(OPT_GIVENNAME.getOpt())); eperson.setLastName(command.getOptionValue(OPT_SURNAME.getOpt())); eperson.setLanguage(command.getOptionValue(OPT_LANGUAGE.getOpt(), Locale.getDefault().getLanguage())); eperson.setMetadata("phone", command.getOptionValue(OPT_PHONE.getOpt())); eperson.setNetid(command.getOptionValue(OPT_NETID.getOpt())); eperson.setPassword(command.getOptionValue('p')); if (command.hasOption(OPT_REQUIRE_CERTIFICATE.getOpt())) { eperson.setRequireCertificate( Boolean.valueOf(command.getOptionValue(OPT_REQUIRE_CERTIFICATE.getOpt()))); } else { eperson.setRequireCertificate(false); } try { eperson.update(); context.commit(); System.out.printf("Created EPerson %d\n", eperson.getID()); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } return 0; }
From source file:org.dspace.eperson.EPerson.java
/** Command to delete an EPerson. */ private static int cmdDelete(Context context, String[] argv) { Options options = new Options(); options.addOption(VERB_DELETE);// w ww . ja v a 2s . co m final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption("h", "help", false, "explain --delete options"); GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --delete [options]", options); return 0; } // Delete! EPerson eperson = null; try { if (command.hasOption(OPT_NETID.getOpt())) { eperson = findByNetid(context, command.getOptionValue(OPT_NETID.getOpt())); } else if (command.hasOption(OPT_EMAIL.getOpt())) { eperson = findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt())); } else { System.err.println("You must specify the user's email address or netid."); return 1; } } catch (SQLException e) { System.err.append(e.getMessage()); return 1; } catch (AuthorizeException e) { /* XXX SNH */ } if (null == eperson) { System.err.println("No such EPerson"); return 1; } try { eperson.delete(); context.commit(); System.out.printf("Deleted EPerson %d\n", eperson.getID()); } catch (SQLException ex) { System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { System.err.println(ex.getMessage()); return 1; } catch (EPersonDeletionException ex) { System.err.println(ex.getMessage()); return 1; } return 0; }
From source file:org.dspace.eperson.EPerson.java
/** Command to modify an EPerson. */ private static int cmdModify(Context context, String[] argv) { Options options = new Options(); options.addOption(VERB_MODIFY);/*from ww w . j a v a2 s . c om*/ final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption(OPT_GIVENNAME); options.addOption(OPT_SURNAME); options.addOption(OPT_PHONE); options.addOption(OPT_LANGUAGE); options.addOption(OPT_REQUIRE_CERTIFICATE); options.addOption(OPT_CAN_LOGIN); options.addOption(OPT_NEW_EMAIL); options.addOption(OPT_NEW_NETID); options.addOption("h", "help", false, "explain --modify options"); GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --modify [options]", options); return 0; } // Modify! EPerson eperson = null; try { if (command.hasOption(OPT_NETID.getOpt())) { eperson = findByNetid(context, command.getOptionValue(OPT_NETID.getOpt())); } else if (command.hasOption(OPT_EMAIL.getOpt())) { eperson = findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt())); } else { System.err.println("No EPerson selected"); return 1; } } catch (SQLException e) { System.err.append(e.getMessage()); return 1; } catch (AuthorizeException e) { /* XXX SNH */ } boolean modified = false; if (null == eperson) { System.err.println("No such EPerson"); return 1; } else { if (command.hasOption(OPT_NEW_EMAIL.getOpt())) { eperson.setEmail(command.getOptionValue(OPT_NEW_EMAIL.getOpt())); modified = true; } if (command.hasOption(OPT_NEW_NETID.getOpt())) { eperson.setNetid(command.getOptionValue(OPT_NEW_NETID.getOpt())); modified = true; } if (command.hasOption(OPT_GIVENNAME.getOpt())) { eperson.setFirstName(command.getOptionValue(OPT_GIVENNAME.getOpt())); modified = true; } if (command.hasOption(OPT_SURNAME.getOpt())) { eperson.setLastName(command.getOptionValue(OPT_SURNAME.getOpt())); modified = true; } if (command.hasOption(OPT_PHONE.getOpt())) { eperson.setMetadata("phone", command.getOptionValue(OPT_PHONE.getOpt())); modified = true; } if (command.hasOption(OPT_LANGUAGE.getOpt())) { eperson.setLanguage(command.getOptionValue(OPT_LANGUAGE.getOpt())); modified = true; } if (command.hasOption(OPT_REQUIRE_CERTIFICATE.getOpt())) { eperson.setRequireCertificate( Boolean.valueOf(command.getOptionValue(OPT_REQUIRE_CERTIFICATE.getOpt()))); modified = true; } if (command.hasOption(OPT_CAN_LOGIN.getOpt())) { eperson.setCanLogIn(Boolean.valueOf(command.getOptionValue(OPT_CAN_LOGIN.getOpt()))); modified = true; } if (modified) { try { eperson.update(); context.commit(); System.out.printf("Modified EPerson %d\n", eperson.getID()); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } } else { System.out.println("No changes."); } } return 0; }
From source file:org.dspace.eperson.EPersonCLITool.java
/** * Tool for manipulating user accounts./* ww w . j av a 2 s. c o m*/ * * @param argv the command line arguments given * @throws ParseException * Base for Exceptions thrown during parsing of a command-line. * @throws SQLException * An exception that provides information on a database access error or other errors. * @throws AuthorizeException * Exception indicating the current user of the context does not have permission * to perform a particular action. */ public static void main(String argv[]) throws ParseException, SQLException, AuthorizeException { final OptionGroup VERBS = new OptionGroup(); VERBS.addOption(VERB_ADD); VERBS.addOption(VERB_DELETE); VERBS.addOption(VERB_LIST); VERBS.addOption(VERB_MODIFY); final Options globalOptions = new Options(); globalOptions.addOptionGroup(VERBS); globalOptions.addOption("h", "help", false, "explain options"); GnuParser parser = new GnuParser(); CommandLine command = parser.parse(globalOptions, argv, true); Context context = new Context(); // Disable authorization since this only runs from the local commandline. context.turnOffAuthorisationSystem(); int status = 0; if (command.hasOption(VERB_ADD.getOpt())) { status = cmdAdd(context, argv); } else if (command.hasOption(VERB_DELETE.getOpt())) { status = cmdDelete(context, argv); } else if (command.hasOption(VERB_MODIFY.getOpt())) { status = cmdModify(context, argv); } else if (command.hasOption(VERB_LIST.getOpt())) { status = cmdList(context, argv); } else if (command.hasOption('h')) { new HelpFormatter().printHelp("user [options]", globalOptions); } else { System.err.println("Unknown operation."); new HelpFormatter().printHelp("user [options]", globalOptions); context.abort(); status = 1; throw new IllegalArgumentException(); } if (context.isValid()) { try { context.complete(); } catch (SQLException ex) { System.err.println(ex.getMessage()); } } }
From source file:org.dspace.eperson.EPersonCLITool.java
/** Command to create an EPerson. */ private static int cmdAdd(Context context, String[] argv) throws AuthorizeException, SQLException { Options options = new Options(); options.addOption(VERB_ADD);/*from w w w .j ava 2s. co m*/ final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption(OPT_GIVENNAME); options.addOption(OPT_SURNAME); options.addOption(OPT_PHONE); options.addOption(OPT_LANGUAGE); options.addOption(OPT_REQUIRE_CERTIFICATE); Option option = new Option("p", "password", true, "password to match the EPerson name"); options.addOption(option); options.addOption("h", "help", false, "explain --add options"); // Rescan the command for more details. GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --add [options]", options); return 0; } // Check that we got sufficient credentials to define a user. if ((!command.hasOption(OPT_EMAIL.getOpt())) && (!command.hasOption(OPT_NETID.getOpt()))) { System.err.println("You must provide an email address or a netid to identify the new user."); return 1; } if (!command.hasOption('p')) { System.err.println("You must provide a password for the new user."); return 1; } // Create! EPerson eperson = null; try { eperson = ePersonService.create(context); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } eperson.setCanLogIn(true); eperson.setSelfRegistered(false); eperson.setEmail(command.getOptionValue(OPT_EMAIL.getOpt())); eperson.setFirstName(context, command.getOptionValue(OPT_GIVENNAME.getOpt())); eperson.setLastName(context, command.getOptionValue(OPT_SURNAME.getOpt())); eperson.setLanguage(context, command.getOptionValue(OPT_LANGUAGE.getOpt(), Locale.getDefault().getLanguage())); ePersonService.setMetadata(context, eperson, "phone", command.getOptionValue(OPT_PHONE.getOpt())); eperson.setNetid(command.getOptionValue(OPT_NETID.getOpt())); ePersonService.setPassword(eperson, command.getOptionValue('p')); if (command.hasOption(OPT_REQUIRE_CERTIFICATE.getOpt())) { eperson.setRequireCertificate( Boolean.valueOf(command.getOptionValue(OPT_REQUIRE_CERTIFICATE.getOpt()))); } else { eperson.setRequireCertificate(false); } try { ePersonService.update(context, eperson); context.complete(); System.out.printf("Created EPerson %s\n", eperson.getID().toString()); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } return 0; }
From source file:org.dspace.eperson.EPersonCLITool.java
/** Command to delete an EPerson. */ private static int cmdDelete(Context context, String[] argv) { Options options = new Options(); options.addOption(VERB_DELETE);//from w w w . jav a2 s . com final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption("h", "help", false, "explain --delete options"); GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --delete [options]", options); return 0; } // Delete! EPerson eperson = null; try { if (command.hasOption(OPT_NETID.getOpt())) { eperson = ePersonService.findByNetid(context, command.getOptionValue(OPT_NETID.getOpt())); } else if (command.hasOption(OPT_EMAIL.getOpt())) { eperson = ePersonService.findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt())); } else { System.err.println("You must specify the user's email address or netid."); return 1; } } catch (SQLException e) { System.err.append(e.getMessage()); return 1; } if (null == eperson) { System.err.println("No such EPerson"); return 1; } try { ePersonService.delete(context, eperson); context.complete(); System.out.printf("Deleted EPerson %s\n", eperson.getID().toString()); } catch (SQLException ex) { System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { System.err.println(ex.getMessage()); return 1; } catch (IOException ex) { System.err.println(ex.getMessage()); return 1; } return 0; }
From source file:org.dspace.eperson.EPersonCLITool.java
/** Command to modify an EPerson. */ private static int cmdModify(Context context, String[] argv) throws AuthorizeException, SQLException { Options options = new Options(); options.addOption(VERB_MODIFY);/*from w ww . j a va 2 s. c o m*/ final OptionGroup identityOptions = new OptionGroup(); identityOptions.addOption(OPT_EMAIL); identityOptions.addOption(OPT_NETID); options.addOptionGroup(identityOptions); options.addOption(OPT_GIVENNAME); options.addOption(OPT_SURNAME); options.addOption(OPT_PHONE); options.addOption(OPT_LANGUAGE); options.addOption(OPT_REQUIRE_CERTIFICATE); options.addOption(OPT_CAN_LOGIN); options.addOption(OPT_NEW_EMAIL); options.addOption(OPT_NEW_NETID); options.addOption("h", "help", false, "explain --modify options"); GnuParser parser = new GnuParser(); CommandLine command; try { command = parser.parse(options, argv); } catch (ParseException e) { System.err.println(e.getMessage()); return 1; } if (command.hasOption('h')) { new HelpFormatter().printHelp("user --modify [options]", options); return 0; } // Modify! EPerson eperson = null; try { if (command.hasOption(OPT_NETID.getOpt())) { eperson = ePersonService.findByNetid(context, command.getOptionValue(OPT_NETID.getOpt())); } else if (command.hasOption(OPT_EMAIL.getOpt())) { eperson = ePersonService.findByEmail(context, command.getOptionValue(OPT_EMAIL.getOpt())); } else { System.err.println("No EPerson selected"); return 1; } } catch (SQLException e) { System.err.append(e.getMessage()); return 1; } boolean modified = false; if (null == eperson) { System.err.println("No such EPerson"); return 1; } else { if (command.hasOption(OPT_NEW_EMAIL.getOpt())) { eperson.setEmail(command.getOptionValue(OPT_NEW_EMAIL.getOpt())); modified = true; } if (command.hasOption(OPT_NEW_NETID.getOpt())) { eperson.setNetid(command.getOptionValue(OPT_NEW_NETID.getOpt())); modified = true; } if (command.hasOption(OPT_GIVENNAME.getOpt())) { eperson.setFirstName(context, command.getOptionValue(OPT_GIVENNAME.getOpt())); modified = true; } if (command.hasOption(OPT_SURNAME.getOpt())) { eperson.setLastName(context, command.getOptionValue(OPT_SURNAME.getOpt())); modified = true; } if (command.hasOption(OPT_PHONE.getOpt())) { ePersonService.setMetadata(context, eperson, "phone", command.getOptionValue(OPT_PHONE.getOpt())); modified = true; } if (command.hasOption(OPT_LANGUAGE.getOpt())) { eperson.setLanguage(context, command.getOptionValue(OPT_LANGUAGE.getOpt())); modified = true; } if (command.hasOption(OPT_REQUIRE_CERTIFICATE.getOpt())) { eperson.setRequireCertificate( Boolean.valueOf(command.getOptionValue(OPT_REQUIRE_CERTIFICATE.getOpt()))); modified = true; } if (command.hasOption(OPT_CAN_LOGIN.getOpt())) { eperson.setCanLogIn(Boolean.valueOf(command.getOptionValue(OPT_CAN_LOGIN.getOpt()))); modified = true; } if (modified) { try { ePersonService.update(context, eperson); context.complete(); System.out.printf("Modified EPerson %s\n", eperson.getID().toString()); } catch (SQLException ex) { context.abort(); System.err.println(ex.getMessage()); return 1; } catch (AuthorizeException ex) { /* XXX SNH */ } } else { System.out.println("No changes."); } } return 0; }
From source file:org.dspace.eperson.Groomer.java
/** * Command line tool for "grooming" the EPerson collection. *//*from w w w. j av a2s . c o m*/ static public void main(String[] argv) throws SQLException { final String USAGE = "EPerson -verb [option...]"; OptionGroup verbs = new OptionGroup(); verbs.setRequired(true); verbs.addOption(new Option("h", "help", false, "explain this tool")); verbs.addOption(new Option("u", "unsalted", false, "list accounts with unsalted password hashes")); Options options = new Options(); options.addOptionGroup(verbs); PosixParser parser = new PosixParser(); CommandLine command = null; try { command = parser.parse(options, argv); } catch (ParseException ex) { System.err.println(ex.getMessage()); if (!(ex instanceof MissingOptionException)) new HelpFormatter().printHelp(USAGE, options); System.exit(1); } // Help the user if (command.hasOption('h') || command.hasOption('?')) { new HelpFormatter().printHelp(USAGE, options); } // Scan for unsalted hashes else if (command.hasOption('u')) { Context myContext = new Context(); final TableRowIterator tri = DatabaseManager.query(myContext, "SELECT email FROM EPerson WHERE password IS NOT NULL AND digest_algorithm IS NULL"); for (TableRow row = tri.next(); tri.hasNext(); row = tri.next()) System.out.println(row.getStringColumn("email")); myContext.abort(); // No changes to commit } // Should not happen: verb option defined but no code! else System.err.println("Unimplemented verb: " + verbs.getSelected()); }
From source file:org.eclipse.jubula.app.autagent.AutAgentApplication.java
/** * method to create an options object, filled with all options * * @return the options/*from w ww . ja v a 2 s . c o m*/ */ private static Options createOptions() { Options options = new Options(); Option portOption = new Option(COMMANDLINE_OPTION_PORT, true, Messages.CommandlineOptionPort); portOption.setArgName(COMMANDLINE_PORT); options.addOption(portOption); options.addOption(COMMANDLINE_OPTION_LENIENT, false, Messages.CommandlineOptionLenient); options.addOption(COMMANDLINE_OPTION_HELP, false, Messages.CommandlineOptionHelp); OptionGroup verbosityOptions = new OptionGroup(); verbosityOptions.addOption(new Option(COMMANDLINE_OPTION_QUIET, false, Messages.CommandlineOptionQuiet)); verbosityOptions .addOption(new Option(COMMANDLINE_OPTION_VERBOSE, false, Messages.CommandlineOptionVerbose)); options.addOptionGroup(verbosityOptions); OptionGroup startStopOptions = new OptionGroup(); startStopOptions.addOption(new Option(COMMANDLINE_OPTION_START, false, Messages.CommandlineOptionStart)); OptionBuilder.hasOptionalArg(); Option stopOption = OptionBuilder.create(COMMANDLINE_OPTION_STOP); stopOption.setDescription(NLS.bind(Messages.OptionStopDescription, DEFAULT_HOSTNAME_LOCALHOST)); stopOption.setArgName(HOSTNAME); startStopOptions.addOption(stopOption); options.addOptionGroup(startStopOptions); return options; }