Java tutorial
/* * DatpassCLI - command line utility for generating passwords * Copyright (C) 2013 - 2014 Alexandru Geana (alegen) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package net.alegen.datpass.cli.input; import net.alegen.datpass.cli.Application; import net.alegen.datpass.cli.utils.ClipboardHandler; import net.alegen.datpass.cli.utils.Settings; import net.alegen.datpass.library.Generator; import net.alegen.datpass.library.configure.Configurator; import org.apache.commons.cli.*; public class PasswordCommand extends Command { private static final String OPTION_LENGTH = "l"; private static final String OPTION_LENGTH_LONG = "length"; private static final String OPTION_INPUT = "i"; private static final String OPTION_INPUT_LONG = "input"; protected PasswordCommand() { this.options = new Options(); Option length = OptionBuilder.hasArg().withArgName("length").withDescription("length of the password") .withLongOpt(OPTION_LENGTH_LONG).create(OPTION_LENGTH); Option input = OptionBuilder.hasArg().withArgName("input") .withDescription("input for the key generation function").withLongOpt(OPTION_INPUT_LONG) .create(OPTION_INPUT); this.options.addOption(length); this.options.addOption(input); } @Override public void execute() { try { CommandLineParser parser = new PosixParser(); String[] arrayArgs = new String[this.args.size()]; this.args.toArray(arrayArgs); CommandLine line = parser.parse(this.options, arrayArgs); String input = ""; int length = -1; if (line.hasOption(OPTION_INPUT)) input = line.getOptionValue(OPTION_INPUT); else { System.out.print("Input: "); input = String.valueOf(System.console().readPassword()); } if (line.hasOption(OPTION_LENGTH)) length = Integer.parseInt(line.getOptionValue(OPTION_LENGTH)); else length = 100; String password = Generator.getInstance().password(input, length); if (Settings.echo()) System.out.println(password); if (Settings.clipboard()) ClipboardHandler.getInstance().set(password); } catch (Generator.GeneratorException e) { } catch (ParseException e) { } } }