Java命令行选项解析之Commons-CLI & Args4J & JCommander

http://rensanning.iteye.com/blog/2161201

JCommander star1000+

This is an annotation based parameter parsing framework for Java 8.

Here is a quick example:

public class JCommanderTest {
@Parameter
public List<String> parameters = Lists.newArrayList(); @Parameter(names = { "-log", "-verbose" }, description = "Level of verbosity")
public Integer verbose = 1; @Parameter(names = "-groups", description = "Comma-separated list of group names to be run")
public String groups; @Parameter(names = "-debug", description = "Debug mode")
public boolean debug = false; @DynamicParameter(names = "-D", description = "Dynamic parameters go here")
public Map<String, String> dynamicParams = new HashMap<String, String>(); }

and how you use it:

JCommanderTest jct = new JCommanderTest();
String[] argv = { "-log", "2", "-groups", "unit1,unit2,unit3",
"-debug", "-Doption=value", "a", "b", "c" };
JCommander.newBuilder()
.addObject(jct)
.build()
.parse(argv); Assert.assertEquals(2, jct.verbose.intValue());
Assert.assertEquals("unit1,unit2,unit3", jct.groups);
Assert.assertEquals(true, jct.debug);
Assert.assertEquals("value", jct.dynamicParams.get("option"));
Assert.assertEquals(Arrays.asList("a", "b", "c"), jct.parameters);

The full doc is available at http://jcommander.org.

Java port of Python's famous argparse command-line argument parser. https://argparse4j.github.io/

Argparse4j is a command line argument parser library for Java based on Python's argparse module.

Argparse4j is available in Maven central repository:

<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
<version>0.8.0</version>
</dependency>
 https://github.com/argparse4j/argparse4j

https://github.com/jankroken/commandline

The Apache Commons CLI library provides an API for parsing command line options passed to programs. It's also able to print help messages detailing the options available for a command line tool.

Commons CLI supports different types of options:

http://commons.apache.org/proper/commons-cli/

https://stackoverflow.com/questions/11704338/java-cli-commandlineparser

https://github.com/jatoben/CommandLine

Mirror of Apache Commons CLI

https://github.com/apache/commons-cli

CLI Parser is a tiny (10k jar), super easy to use library for parsing various kinds of command line arguments or property lists. Using annotations on your fields or JavaBean properties you can specify what configuration is available. Here is an example:

public class Loader {

  @Argument
private static Boolean hdfs = false; @Argument(alias = "r", description = "Regular expression to parse lines", required = true)
private static String regex; @Argument(alias = "k", description = "Key column", required = true)
private static String key; @Argument(alias = "p", description = "Key prefix")
private static String prefix; @Argument(alias = "c", description = "Column groups", delimiter = ",")
private static String[] columns; @Argument(alias = "n", description = "Column names", delimiter = ",")
private static String[] names; @Argument(alias = "h", description = "Redis host")
private static String host = "localhost"; @Argument(alias = "p", description = "Redis port")
private static Integer port = 6379; public static void main(String[] args) throws IOException {
// unparsed will contain all unparsed arguments to the command line
List<String> unparsed = Args.parseOrExit(Loader.class, args);
// Loader's fields will be populated after this line or the program will exit with usage
}
}

In this case we are configuring static fields, but you can also use the same system with instances. If you pass in a wrong command line argument you will get the usage message:

Usage: com.sampullara.cli.Example
-hdfs [flag]
-regex (-r) [String] Regular expression to parse lines
-key (-k) [String] Key column
-prefix (-p) [String] Key prefix
-columns (-c) [String[,]] Column groups
-names (-n) [String[,]] Column names
-host (-h) [String] Redis host (localhost)
-port (-p) [Integer] Redis port (6379)

That message will print out the names and aliases of the arguments, type, description and a default value for the parameter if there is one. You can add it to your code with:

<dependency>
<groupId>com.github.spullara.cli-parser</groupId>
<artifactId>cli-parser</artifactId>
<version>1.1</version>
</dependency>

https://github.com/spullara/cli-parser

KeyStore Explorer is a free GUI replacement for the Java command-line utilities keytool and jarsigner.

Official website: http://keystore-explorer.org/

Features:

  • Create, load, save and convert between various KeyStore types: JKS, JCEKS, PKCS#12, BKS (V1 and V2) and UBER
  • Change KeyStore and KeyStore entry passwords
  • Delete or rename KeyStore entries
  • Cut/copy/paste KeyStore entries
  • Append certificates to key pair certificate chains
  • Generate RSA, ECC and DSA key pairs with self-signed X.509 certificates
  • Apply X.509 certificate extensions to generated key pairs and Certificate Signing Requests (CSRs)
  • View X.509 Certificate, CRL and CRL entry X.509 V3 extensions
  • Import and export keys and certificates in many formats: PKCS#12, PKCS#8, PKCS#7, DER/PEM X.509 certificate files, Microsoft PVK, SPC, PKI Path, OpenSSL
  • Generate, view and sign CSRs in PKCS #10 and SPKAC formats
  • Sign JAR files
  • Configure a CA Certs KeyStore for use with KeyStore operations

https://github.com/kaikramer/keystore-explorer

Library Usage:

  1. Drop the jar into your lib folder and add to build path.
  2. Choose the converter of your choice, they are named DocToPDFConverter, DocxToPDFConverter, PptToPDFConverter, PptxToPDFConverter and OdtToPDFConverter.
  3. Instantiate with 4 parameters
    • InputStream inStream: Document source stream to be converted
    • OutputStream outStream: Document output stream
    • boolean showMessages: Whether to show intermediate processing messages to Standard Out (stdout)
    • boolean closeStreamsWhenComplete: Whether to close input and output streams when complete
  4. Call the "convert()" method and wait.

Caveats and technical details:

This tool relies on Apache POI, xdocreport, docx4j and odfdom libraries. They are not 100% reliable and the output format may not always be what you desire.

DOC:

Generally ok but takes some time to convert.. I notice that after conversion, the paragraph spacing tends to increase affecting your page layout. Conversion is done using docx4j to convert DOC to DOCX then to PDF.(Cannot use xdocreport once the DOCX data is obtained as the intermediate data structure is docx4j specific.)

DOCX:

Very good results. Fast conversion too. Conversion is done using xdocreport library as it seems faster and more accurate than docx4j.

PPT and PPTX:

Resulting file is a PDF comprising of a PNG embedded in each page. Should be good enough for printing. This is the limitation of the Apache POI and docx4j libraries.

ODT:

Quality and speed as good as DOCX. Conversion is done using odfdom of the Apache ODF Toolkit.

Main Libraries

Apache POI: https://poi.apache.org/
xdocreport: http://code.google.com/p/xdocreport/
docx4j: http://www.docx4java.org/
odfdom: https://incubator.apache.org/odftoolkit/odfdom/

https://github.com/yeokm1/docs-to-pdf-converter

Pdf2Dom is a PDF parser that converts the documents to a HTML DOM representation. The obtained DOM tree may be then serialized to a HTML file or further processed. A command-line utility for converting the PDF documents to HTML is included in the distribution package. Pdf2Dom may be also used as an independent Java library with a standard DOM i…http://cssbox.sourceforge.net/pdf2dom/

Pdf2Dom is based on the Apache PDFBox™ library.

https://github.com/radkovo/Pdf2Dom

About

Generate scaffold with spring boot.

Generate CRUD basic with spring boot.

Scaffold for java web, a clean generate with simple classes.

https://github.com/NetoDevel/cli-spring-boot-scaffold

An annotation based command line parser的更多相关文章

  1. logoff remote desktop sessions via command line tools

    This trick I learned from my one of ex-college.  In Windows servers, only two remote desktop session ...

  2. 15 Examples To Master Linux Command Line History

    When you are using Linux command line frequently, using the history effectively can be a major produ ...

  3. atprogram.exe : Atmel Studio Command Line Interface

    C:\Program Files\Atmel\Atmel Studio 6.1\atbackend\atprogram.exe No command specified.Atmel Studio Co ...

  4. Building QT projects from the command line

    /************************************************************************ * Building QT projects fro ...

  5. [笔记]The Linux command line

    Notes on The Linux Command Line (by W. E. Shotts Jr.) edited by Gopher 感觉博客园是不是搞了什么CSS在里头--在博客园显示效果挺 ...

  6. Linux Command Line learning

    https://www.codecademy.com/en/courses/learn-the-command-line Background The command line is a text i ...

  7. MAC OS 如何安装命令行工具:Command Line Tools

    打开终端输入:xcode-select --install 回车 安装好了测试结果:gcc -v 显示如下: xcode-select: note: install requested for com ...

  8. Xcode 8.X Command Line Tools

    Summary Step 1. Upgrade Your System to macOS Sierra Step 2. Open the Terminal Application Step 3. Is ...

  9. Creating Node.js Command Line Utilities to Improve Your Workflow

    转自:https://developer.telerik.com/featured/creating-node-js-command-line-utilities-improve-workflow/ ...

随机推荐

  1. VS2005工程的Device右边内容为空问题

    VS2005工程的Device右边内容为空问题 可能是刚刚在删除C盘一些文件或是这些文件因为某些原因丢失了,在打开WINCE6.0系统工程的时候,发现无法编译,才注意到VS2005工程的Device右 ...

  2. UITabbar的一些常规用法(总结)

    往往系统自带的UITabbar 不能满足我们的样式或者颜色设计,所以需要调整UITabbar. 1.自定义UITabbar,也是我学到的第一种方式(简单暴力). 先记录一下思路: 首先,隐藏系统自带的 ...

  3. obj-c属性的新的特性

    在以前的objc中我们必须在接口中定义属性对应的实例方法,然后在实现文件中"同步"该属性,如下代码: @interface Foo:NSObject{ NSString *name ...

  4. ELF 文件 动态连接 - 延迟绑定(PLT)

    PLT 全称:Procedure Linkage Table ,直译:过程连接表 由于在动态连接中,程序的模块之间包含了大量的函数引用,所以在程序开始执行前,动态链接会耗费较多的时间用于模块之间函数引 ...

  5. solr研磨之游标分页

    普通分页 当需要深度分页的时候,比如查询第10000页数据,每页显示10条,意味着需要提取前10000 x 10 页的数据,并将这100000条数据缓存在内存中,然后在内存中进行排序.最后返回最后10 ...

  6. Java内存模型_重排序

    重排序:是指编译器和处理器为了优化程序性能而对指令序列进行重新排序的一种手段 1..编译器优化的重排序.编译器在不改变单线程程序语义的前提下,可以重新安排语句的执行顺序. 2..指令级并行的重排序.现 ...

  7. <mate name="viewport">移动端设置详解

    <meta name="viewport" content="width=device-width,height=device-height,initial-sca ...

  8. mongodb3.6 副本集(三)mongodb 如何做数据备灾

    前言 个人理解,副本集一个主要作用就是当Master库出现故障,其中的一个salve从库会被选举出来成为新的Master.框架图如下: 其中,选举者是不参与数据存储的,它的作用只是为了选举出新的Mas ...

  9. 一个简单的例子搞懂ES6之Promise

    ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...

  10. DDD学习笔记1——分层架构

    新旧架构对比图: DDD中的基础设施层包括数据持久化(ORM数据访问),IoC容器实现,AOP实现(安全,日志记录,缓存等) Repository的接口通常放在领域层,具体实现在基础设施层 旧架构的业 ...