lunlumo #7620(2008/09/10 14:46 GMT) [ Java ] Rating0/0=0.00
ApacheCommonsCLIを使って書いてみました。
see: Apache commons CLI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.MissingArgumentException; import org.apache.commons.cli.Options; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; class cmdopt { public static void main(String[] args) { CommandLineParser parser = new PosixParser(); Options options = new Options(); options.addOption(OptionBuilder.withArgName("o").withLongOpt("output").isRequired(true).withDescription("set output").create("o")); options.addOption("q","quote",false,"set quote"); options.addOption("d","debug",true,"set debug level"); try { CommandLine command = parser.parse(options,args); if ( (command.hasOption("d") && ( Integer.parseInt(command.getOptionValue("d")) < 0 || Integer.parseInt(command.getOptionValue("d")) > 2)) || command.getArgs().length == 0) throw new MissingArgumentException("invalid argument."); System.out.println("[オプション情報]"); System.out.println("o(output):\t"+(command.hasOption("o") ? "ON" : "OFF")); System.out.println("q(quote):\t"+(command.hasOption("q") ? "ON" : "OFF")); System.out.println("d(debug):\t"+(command.hasOption("d") ? command.getOptionValue("d") : "2")); System.out.println(); System.out.println("[パラメータ情報]"); System.out.printf("指定数:\t%d\n",command.getArgs().length); for (int i=0;i<command.getArgs().length;i++) System.out.printf("%d:\t%s\n",i+1,command.getArgs()[i]); } catch(ParseException ex) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("cmdopt",options); } catch(Exception ex) { ex.printStackTrace(); } } }
Rating0/0=0.00-0+
[ reply ]
lunlumo #7620() [ Java ] Rating0/0=0.00
ApacheCommonsCLIを使って書いてみました。
see: Apache commons CLI
Rating0/0=0.00-0+
[ reply ]