Java命令行解析库 Argparse4j

fmms 12年前
     argparse4j 是 Python argparse 命令行解析器的 Java 语言移植版。    <p>它的主要特性:</p>    <ul>     <li>Supported positional arguments and optional arguments.</li>     <li>Variable number of arguments.</li>     <li>Generates well formatted line-wrapped help message.</li>     <li>Takes into account East Asian Width ambiguous characters when line-wrap.</li>     <li>Sub-commands like, <tt>git add</tt>.</li>     <li>Customizable option prefix characters, e.g. <tt>'+f'</tt> and <tt>'/h'</tt>.</li>     <li>Print default values in help message.</li>     <li>Choice from given collection of values.</li>     <li>Type conversion from option strings.</li>     <li>Can directly assign values into user defined classes using annotation.</li>     <li>Group arguments so that it will be printed in help message in more readable way.</li>    </ul>    <p>示例代码:</p>    <pre class="brush:java; toolbar: true; auto-links: false;">import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException;  import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; import net.sourceforge.argparse4j.inf.ArgumentParserException; import net.sourceforge.argparse4j.inf.Namespace;  public class Checksum {      public static void main(String[] args) {         ArgumentParser parser = ArgumentParsers.newArgumentParser("Checksum")                 .defaultHelp(true)                 .description("Calculate checksum of given files.");         parser.addArgument("-t", "--type")                 .choices("SHA-256", "SHA-512", "SHA1").setDefault("SHA-256")                 .help("Specify hash function to use");         parser.addArgument("file").nargs("*")                 .help("File to calculate checksum");         Namespace ns = null;         try {             ns = parser.parseArgs(args);         } catch (ArgumentParserException e) {             parser.handleError(e);             System.exit(1);         }         MessageDigest digest = null;         try {             digest = MessageDigest.getInstance(ns.getString("type"));         } catch (NoSuchAlgorithmException e) {             System.err.printf("Could not get instance of algorithm %s: %s",                     ns.getString("type"), e.getMessage());             System.exit(1);         }         for (String name : ns.                    <string>                          getList("file")) {             Path path = Paths.get(name);             try (ByteChannel channel = Files.newByteChannel(path,                     StandardOpenOption.READ);) {                 ByteBuffer buffer = ByteBuffer.allocate(4096);                 while (channel.read(buffer) > 0) {                     buffer.flip();                     digest.update(buffer);                     buffer.clear();                 }             } catch (IOException e) {                 System.err                         .printf("%s: failed to read data: %s", e.getMessage());                 continue;             }             byte md[] = digest.digest();             StringBuffer sb = new StringBuffer();             for (int i = 0, len = md.length; i < len; ++i) {                 String x = Integer.toHexString(0xff & md[i]);                 if (x.length() == 1) {                     sb.append("0");                 }                 sb.append(x);             }             System.out.printf("%s  %s\n", sb.toString(), name);         }     }  }                    </string></pre>    <p><strong>项目主页:</strong><a href="http://www.open-open.com/lib/view/home/1322613438421" target="_blank">http://www.open-open.com/lib/view/home/1322613438421</a></p>    <p></p>