java - 如何在 Java 中处理命令行参数

标签 java design-patterns

假设我有一个简单的程序,它接受以下形式之一的参数输入
do1 inputLocation outputLocation
do2 inputLocation outputLocation
do3 [30 or 60 or 90] inputLocation outputLocation
do4 [P D or C] inputLocation outputLocation
do5 [G H I] inputLocation outputLocation

我在程序中还有5个同名函数需要调用。到目前为止,我想到了这样做(在“半伪代码”中)

static void main(String[] args)
{
    if (args.length == 3)
        processTriple(args);
    if (args.length == 4)
        processQuadruple(args);
    throw new UnsupportedOperationException("dasdhklasdha");
}

流程函数看起来像这样

processDouble(String args[])
{
    String operation = "args[0]";
    Location input = getInput(args[1]);
    Location output = getInput(args[2]);
    if (operation.equals("do1"))
        do1(input,output);
    if (operation.equals("do2"))
        do2(input,output);
    ... etc
}

我这样做的方式似乎不太可扩展。如果一个函数的参数发生变化,或者添加了新函数,那么维护起来似乎会很痛苦。 做这样的事情的“最佳”方法是什么

最佳答案

此时我会使用 commons-clijargs .除非你想用参数做一些非常特别的事情,否则我会说专注于你应用程序的实际业务,不要处理应用程序参数的困惑

关于java - 如何在 Java 中处理命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944199/

相关文章:

java - 获取导致 com.microsoft.sqlserver.jdbc.SQLServerException : String or binary data would be truncated 的列的任何方法

Java 函数需要获取特定文本并将其更改为 url

java - 来自 Java 的多个 Clojure 实例?

java - 搜索历史和过滤器的面向对象设计

java - 单例设计模式 java idioma

c# - 在 c# 中执行此操作的最佳 OOP 模式是什么

delphi - MVP : other constructor's parameters than view and model?

java - 为 giraph 运行 hive 作业以进行图形分析时出现问题

java - Hibernate Criteria 查询 Collections 包含某些元素

java - 单例模式 : Using Enum Version