Java 具有Apache Commons CLI的两个独占OptionGroup
我正在构建一个命令行Java应用程序,在使用Apache Commons CLI解析命令行参数时遇到问题 我试图介绍我的场景,其中我需要有两个独占的命令行参数组,它们具有长(Java 具有Apache Commons CLI的两个独占OptionGroup,java,command-line-interface,command-line-arguments,apache-commons-cli,picocli,Java,Command Line Interface,Command Line Arguments,Apache Commons Cli,Picocli,我正在构建一个命令行Java应用程序,在使用Apache Commons CLI解析命令行参数时遇到问题 我试图介绍我的场景,其中我需要有两个独占的命令行参数组,它们具有长(-abc)和短(-a)参数 用例1 短参数:-d oracle-jdbc:oracle:thin:@//host:port/databa 相同但具有长参数:-方言oracle-jdbcUrl jdbc:oracle:thin:@//host:port/databa 用例2: import picocli.CommandL
-abc
)和短(-a
)参数
用例1
- 短参数:
-d oracle-jdbc:oracle:thin:@//host:port/databa
- 相同但具有长参数:
-方言oracle-jdbcUrl jdbc:oracle:thin:@//host:port/databa
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "SqlRunner",
sortOptions = false,
usageHelpWidth = 100,
description = "SQL command line tool. It executes the given SQL and show the result on the standard output.\n",
parameterListHeading = "General options:\n",
footerHeading = "\nPlease report issues at arnold.somogyi@gmail.com.",
footer = "\nDocumentation, source code: https://github.com/zappee/sql-runner.git")
public class SqlRunner implements Runnable {
/**
* Definition of the general command line options.
*/
@Option(names = {"-?", "--help"}, usageHelp = true, description = "Display this help and exit.")
private boolean help;
@Option(names = {"-d", "--dialect"}, defaultValue = "oracle", showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "Supported SQL dialects: oracle.")
private static String dialect;
@ArgGroup(exclusive = true, multiplicity = "1", heading = "\nProvide a JDBC URL:\n")
MainArgGroup mainArgGroup;
/**
* Two exclusive parameter groups:
* (1) JDBC URL parameter
* (2) Custom connection parameters
*/
static class MainArgGroup {
/**
* JDBC URL option (only one parameter).
*/
@Option(names = {"-j", "--jdbcUrl"}, arity = "1", description = "JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.")
private static String jdbcUrl;
/**
* Custom connection parameter group.
*/
@ArgGroup(exclusive = false, multiplicity = "1", heading = "\nCustom configuration:\n")
CustomConfigurationGroup customConfigurationGroup;
}
/**
* Definition of the SQL which will be executed.
*/
@Parameters(index = "0", arity = "1", description = "SQL to be executed. Example: 'select 1 from dual'")
String sql;
/**
* Custom connection parameters.
*/
static class CustomConfigurationGroup {
@Option(names = {"-h", "--host"}, required = true, description = "Name of the database server.")
private static String host;
@Option(names = {"-p", "--port"}, required = true, description = "Number of the port where the server listens for requests.")
private static String port;
@Option(names = {"-s", "--sid"}, required = true, description = "Name of the particular database on the server. Also known as the SID in Oracle terminology.")
private static String sid;
@Option(names = {"-U", "--user"}, required = true, description = "Name for the login.")
private static String user;
@Option(names = {"-P", "--password"}, required = true, description = "Password for the connecting user.")
private static String password;
}
/**
* The entry point of the executable JAR.
*
* @param args command line parameters
*/
public static void main(String[] args) {
CommandLine cmd = new CommandLine(new SqlRunner());
int exitCode = cmd.execute(args);
System.exit(exitCode);
}
/**
* It is used to create a thread.
*/
@Override
public void run() {
int exitCode = 0; //executeMyStaff();
System.exit(exitCode);
}
}
$ java -jar target/sql-runner-1.0-shaded.jar --help
Usage: SqlRunner [-?] [-d=<dialect>] (-j=<jdbcUrl> | (-h=<host> -p=<port> -s=<sid> -U=<user>
-P=<password>)) <sql>
SQL command line tool. It executes the given SQL and show the result on the standard output.
General settings:
<sql> SQL to be executed. Example: 'select 1 from dual'
-?, --help Display this help and exit.
-d, --dialect=<dialect> Supported SQL dialects: oracle.
Default: oracle
Custom configuration:
-h, --host=<host> Name of the database server.
-p, --port=<port> Number of the port where the server listens for requests.
-s, --sid=<sid> Name of the particular database on the server. Also known as the SID in
Oracle terminology.
-U, --user=<user> Name for the login.
-P, --password=<password> Password for the connecting user.
Provide a JDBC URL:
-j, --jdbcUrl=<jdbcUrl> JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.
Please report issues at arnold.somogyi@gmail.com.
Documentation, source code: https://github.com/zappee/sql-runner.git
- 短参数:
-d oracle-h host-p 1521-s database-U user-p pwd
- 相同但具有长参数:
-方言oracle-主机主机-端口1521-sid数据库-用户-密码pwd
选项创建了两个选项组
:
OptionGroup jdbcUrlGroup = new OptionGroup();
jdbcUrlGroup.setRequired(true);
jdbcUrlGroup.addOption(jdbcUrl);
第二组:
OptionGroup customConfigurationGroup = new OptionGroup();
customConfigurationGroup.setRequired(true);
customConfigurationGroup.addOption(host);
customConfigurationGroup.addOption(port);
customConfigurationGroup.addOption(sid);
customConfigurationGroup.addOption(user);
customConfigurationGroup.addOption(password);
然后我用以下方式构建选项
对象:
Options options = new Options();
options.addOptionGroup(jdbcUrlGroup);
options.addOptionGroup(customConfigurationGroup);
options.addOption(dialect);
但这不起作用,因为它希望定义这两个组
这就是方言选项
的定义方式:
Option dialect = Option
.builder("d")
.longOpt("dialect")
.required(false)
.hasArg()
.argName("DIALECT")
.desc("supported SQL dialects: oracle. Default value: oracle")
.build();
除此之外,其他必需的选项
定义看起来类似:
.required(true)
结果:
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "SqlRunner",
sortOptions = false,
usageHelpWidth = 100,
description = "SQL command line tool. It executes the given SQL and show the result on the standard output.\n",
parameterListHeading = "General options:\n",
footerHeading = "\nPlease report issues at arnold.somogyi@gmail.com.",
footer = "\nDocumentation, source code: https://github.com/zappee/sql-runner.git")
public class SqlRunner implements Runnable {
/**
* Definition of the general command line options.
*/
@Option(names = {"-?", "--help"}, usageHelp = true, description = "Display this help and exit.")
private boolean help;
@Option(names = {"-d", "--dialect"}, defaultValue = "oracle", showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "Supported SQL dialects: oracle.")
private static String dialect;
@ArgGroup(exclusive = true, multiplicity = "1", heading = "\nProvide a JDBC URL:\n")
MainArgGroup mainArgGroup;
/**
* Two exclusive parameter groups:
* (1) JDBC URL parameter
* (2) Custom connection parameters
*/
static class MainArgGroup {
/**
* JDBC URL option (only one parameter).
*/
@Option(names = {"-j", "--jdbcUrl"}, arity = "1", description = "JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.")
private static String jdbcUrl;
/**
* Custom connection parameter group.
*/
@ArgGroup(exclusive = false, multiplicity = "1", heading = "\nCustom configuration:\n")
CustomConfigurationGroup customConfigurationGroup;
}
/**
* Definition of the SQL which will be executed.
*/
@Parameters(index = "0", arity = "1", description = "SQL to be executed. Example: 'select 1 from dual'")
String sql;
/**
* Custom connection parameters.
*/
static class CustomConfigurationGroup {
@Option(names = {"-h", "--host"}, required = true, description = "Name of the database server.")
private static String host;
@Option(names = {"-p", "--port"}, required = true, description = "Number of the port where the server listens for requests.")
private static String port;
@Option(names = {"-s", "--sid"}, required = true, description = "Name of the particular database on the server. Also known as the SID in Oracle terminology.")
private static String sid;
@Option(names = {"-U", "--user"}, required = true, description = "Name for the login.")
private static String user;
@Option(names = {"-P", "--password"}, required = true, description = "Password for the connecting user.")
private static String password;
}
/**
* The entry point of the executable JAR.
*
* @param args command line parameters
*/
public static void main(String[] args) {
CommandLine cmd = new CommandLine(new SqlRunner());
int exitCode = cmd.execute(args);
System.exit(exitCode);
}
/**
* It is used to create a thread.
*/
@Override
public void run() {
int exitCode = 0; //executeMyStaff();
System.exit(exitCode);
}
}
$ java -jar target/sql-runner-1.0-shaded.jar --help
Usage: SqlRunner [-?] [-d=<dialect>] (-j=<jdbcUrl> | (-h=<host> -p=<port> -s=<sid> -U=<user>
-P=<password>)) <sql>
SQL command line tool. It executes the given SQL and show the result on the standard output.
General settings:
<sql> SQL to be executed. Example: 'select 1 from dual'
-?, --help Display this help and exit.
-d, --dialect=<dialect> Supported SQL dialects: oracle.
Default: oracle
Custom configuration:
-h, --host=<host> Name of the database server.
-p, --port=<port> Number of the port where the server listens for requests.
-s, --sid=<sid> Name of the particular database on the server. Also known as the SID in
Oracle terminology.
-U, --user=<user> Name for the login.
-P, --password=<password> Password for the connecting user.
Provide a JDBC URL:
-j, --jdbcUrl=<jdbcUrl> JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.
Please report issues at arnold.somogyi@gmail.com.
Documentation, source code: https://github.com/zappee/sql-runner.git
-d oracle
:缺少必需的选项:[-j…],[-h…,-p…,-s…,-U…,-p…]
-d oracle-jdbcUrl xxx
:缺少必需的选项:[-h…,-p…,-s…,-U…,-p…]
-d oracle-h yyy
:缺少必需的选项:[-j…]
但是我想要的是:如果提供了JDBC URL,那么就不需要主机、端口等参数,或者相反。我认为是时候忘记它,并将其标记为不推荐使用的库了。好的,如果您只有几个命令行参数,那么您可以使用它,否则最好不要使用。事实上,这个Apache项目最近(2019年2月17日)进行了更新,但仍然缺少许多功能,使用Apache Commons CLI库有点痛苦
该项目似乎是解析命令行参数的更好候选。这是一个非常直观的库,易于使用,并且有一个很好的和全面的。我认为一个拥有完美文档的中等级别工具比一个没有任何文档的闪亮项目要好
无论如何,picocli
是一个非常好的库,有完美的文档,所以我给它加了两个加号:)
这就是我如何用picocli
:
import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
@Command(name = "SqlRunner",
sortOptions = false,
usageHelpWidth = 100,
description = "SQL command line tool. It executes the given SQL and show the result on the standard output.\n",
parameterListHeading = "General options:\n",
footerHeading = "\nPlease report issues at arnold.somogyi@gmail.com.",
footer = "\nDocumentation, source code: https://github.com/zappee/sql-runner.git")
public class SqlRunner implements Runnable {
/**
* Definition of the general command line options.
*/
@Option(names = {"-?", "--help"}, usageHelp = true, description = "Display this help and exit.")
private boolean help;
@Option(names = {"-d", "--dialect"}, defaultValue = "oracle", showDefaultValue = CommandLine.Help.Visibility.ALWAYS, description = "Supported SQL dialects: oracle.")
private static String dialect;
@ArgGroup(exclusive = true, multiplicity = "1", heading = "\nProvide a JDBC URL:\n")
MainArgGroup mainArgGroup;
/**
* Two exclusive parameter groups:
* (1) JDBC URL parameter
* (2) Custom connection parameters
*/
static class MainArgGroup {
/**
* JDBC URL option (only one parameter).
*/
@Option(names = {"-j", "--jdbcUrl"}, arity = "1", description = "JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.")
private static String jdbcUrl;
/**
* Custom connection parameter group.
*/
@ArgGroup(exclusive = false, multiplicity = "1", heading = "\nCustom configuration:\n")
CustomConfigurationGroup customConfigurationGroup;
}
/**
* Definition of the SQL which will be executed.
*/
@Parameters(index = "0", arity = "1", description = "SQL to be executed. Example: 'select 1 from dual'")
String sql;
/**
* Custom connection parameters.
*/
static class CustomConfigurationGroup {
@Option(names = {"-h", "--host"}, required = true, description = "Name of the database server.")
private static String host;
@Option(names = {"-p", "--port"}, required = true, description = "Number of the port where the server listens for requests.")
private static String port;
@Option(names = {"-s", "--sid"}, required = true, description = "Name of the particular database on the server. Also known as the SID in Oracle terminology.")
private static String sid;
@Option(names = {"-U", "--user"}, required = true, description = "Name for the login.")
private static String user;
@Option(names = {"-P", "--password"}, required = true, description = "Password for the connecting user.")
private static String password;
}
/**
* The entry point of the executable JAR.
*
* @param args command line parameters
*/
public static void main(String[] args) {
CommandLine cmd = new CommandLine(new SqlRunner());
int exitCode = cmd.execute(args);
System.exit(exitCode);
}
/**
* It is used to create a thread.
*/
@Override
public void run() {
int exitCode = 0; //executeMyStaff();
System.exit(exitCode);
}
}
$ java -jar target/sql-runner-1.0-shaded.jar --help
Usage: SqlRunner [-?] [-d=<dialect>] (-j=<jdbcUrl> | (-h=<host> -p=<port> -s=<sid> -U=<user>
-P=<password>)) <sql>
SQL command line tool. It executes the given SQL and show the result on the standard output.
General settings:
<sql> SQL to be executed. Example: 'select 1 from dual'
-?, --help Display this help and exit.
-d, --dialect=<dialect> Supported SQL dialects: oracle.
Default: oracle
Custom configuration:
-h, --host=<host> Name of the database server.
-p, --port=<port> Number of the port where the server listens for requests.
-s, --sid=<sid> Name of the particular database on the server. Also known as the SID in
Oracle terminology.
-U, --user=<user> Name for the login.
-P, --password=<password> Password for the connecting user.
Provide a JDBC URL:
-j, --jdbcUrl=<jdbcUrl> JDBC URL, example: jdbc:oracle:<drivertype>:@//<host>:<port>/<database>.
Please report issues at arnold.somogyi@gmail.com.
Documentation, source code: https://github.com/zappee/sql-runner.git
导入picocli.CommandLine;
导入picocli.CommandLine.ArgGroup;
导入picocli.CommandLine.Command;
导入picocli.CommandLine.Option;
导入picocli.CommandLine.Parameters;
@命令(name=“SqlRunner”,
sortOptions=false,
usageHelpWidth=100,
description=“SQL命令行工具。它执行给定的SQL并在标准输出上显示结果。\n”,
parameterListHeading=“常规选项:\n”,
footerHeading=“\n请在arnold报告问题。somogyi@gmail.com.",
footer=“\n文档,源代码:https://github.com/zappee/sql-runner.git")
公共类SqlRunner实现了Runnable{
/**
*常规命令行选项的定义。
*/
@选项(名称={“-?”,“--help”},usageHelp=true,description=“显示此帮助并退出”。)
私人布尔帮助;
@选项(名称={“-d”,“--dialogue”},defaultValue=“oracle”,showDefaultValue=CommandLine.Help.Visibility.ALWAYS,description=“受支持的SQL方言:oracle”)
私有静态字符串方言;
@ArgGroup(exclusive=true,multiplicity=“1”,heading=“\n提供JDBC URL:\n”)
MainArgGroup MainArgGroup;
/**
*两个独占参数组:
*(1)JDBC URL参数
*(2)自定义连接参数
*/
静态类MainArgGroup{
/**
*JDBCURL选项(仅一个参数)。
*/
@选项(名称={“-j”,“--jdbcUrl”},arity=“1”,description=“JDBC URL,示例:JDBC:oracle::@/:/”)
私有静态字符串jdbcUrl;
/**
*自定义连接参数组。
*/
@ArgGroup(exclusive=false,multiplicity=“1”,heading=“\n自定义配置:\n”)
CustomConfigurationGroup CustomConfigurationGroup;
}
/**
*将要执行的SQL的定义。
*/
@参数(index=“0”、arity=“1”、description=“要执行的SQL。示例:“从双精度中选择1”)
字符串sql;
/**
*自定义连接参数。
*/
静态类CustomConfigurationGroup{
@选项(names={“-h”,“--host”},required=true,description=“数据库服务器的名称”)
私有静态字符串主机;
@选项(name={“-p”,“--port”},required=true,description=“服务器侦听请求的端口号”。)
专用静态字符串端口;
@选项(Name={“-s”,“--sid”},required=true,description=“服务器上特定数据库的名称。在Oracle术语中也称为sid。”)
私有静态字符串sid;
@选项(名称={“-U”,“--user”},必需=true,description=“登录名”)
私有静态字符串用户;
@选项(names={“-P”,“--password”},required=true,description=“连接用户的密码”。)
私有静态字符串密码;
}
/**
*可执行JAR的入口点。
*
*@param args命令行参数
*/
公共静态void main(字符串[]args){
CommandLine cmd=newcommandline(newsqlrunner());
int exitCode=cmd.execute(args);
系统退出(退出代码);
}
/**
*它用于创建线程。
*/
@凌驾
公开募捐{
int exitCode=0;//executeMyStaff();
系统退出(退出代码);
}
}