Java中解析命令行参数的代码结构 [英] Code Structure for Parsing Command line Arguments in Java

查看:58
本文介绍了Java中解析命令行参数的代码结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对代码的结构有疑问.

I have a question regarding structuring of code.

我已经说了三种类型的软件包A,B和C.

I have let us say three types of packages A,B and C.

现在,包A中的类包含包含main()函数的类.这些班需要一些命令行参数才能运行.

Now, classes in package A contains classes which contain the main() function. These classes need some command line arguments to run.

在程序包B中,有一些类,其中包含一些需要在不同时间配置的公共变量.例如,在调用函数A之前,应设置或重置该变量,根据该变量的输出会有所不同.

In package B, there are classes which contains some public variables, which need to be configured, at different times. For example before calling function A, the variable should be set or reset, the output differs according to this variable.

在程序包C中,使用程序包B中的类执行某些任务.他们确实如前所述配置变量.不仅在创建对象时,而且还在中间阶段.

In package C, uses the classes in package B to perform some tasks. They do configure their variables as said before. Not only when the object is created, but also at intermediate stage.

程序包A还具有一些类,这些类依次使用程序包B和程序包C中的类.为了配置B和C类中的变量,程序包A中的类包含main()函数,读取命令行参数并传递各个类别的正确值.

Package A also has classes which in turn use classes from package B and package C. In order to configure the variables in classes of B and C, class in package A containing the main() function, reads command line arguments and passes the correct values to respective class.

现在,在这种情况下,我想使用 Apache Commons CLI 解析器.

Now, given this scenario, I want to use Apache Commons CLI parser.

我无法理解我应该如何精确地编写代码,以一种优雅的方式进行结构化.在这种情况下,什么是好的设计实践.

I am unable to understand how exactly I should write my code to be structured in an elegant way. What is a good design practice for such scenario.

最初,我编写了一个没有Apache的类来解析命令行参数.

Initially I wrote a class without Apache to parse the command line arguments.

由于我想对设计问题提出建议,因此我将摘录代码而不是完整的代码.

Since I want a suggestion on design issue, I will give an excerpt of code rather than complete code.

public class ProcessArgs 
{
     private String optionA= "default";
     private String optionB= "default";
     private String optionC= "default";

     public void printHelp ()
     {
         System.out.println ("FLAG : DESCRIPTION : DEFAULT VALUE");
         System.out.println ("-A <Option A> : Enable Option A : " + optionA);
         System.out.println ("-B <Option B> : Enable Option B : " + optionB);
         System.out.println ("-C <Option C> : Enable Option C : " + optionC); 
     }

     public void printConfig()
     {
         System.out.println ("Option A " + optionA);
         System.out.println ("Option B " + optionB);
         System.out.println ("Option C " + optionC);
     }

     public void parseArgs (String[] args)
     {
        for (int i=0;i<args.length;i++)
        {
        if (args[i].equalsIgnoreCase ("-A"))
           optionA = args[++i];
        else if (args[i].equalsIgnoreCase ("-B"))
           optionB = args[++i];
        else if (args[i].equalsIgnoreCase ("-C"))
           optionC = args[++i];
        else
           throw new RuntimeException ("Wrong Argument : " + args[i] + " :: -h for Help.");
    }
     }
}

注意事项-

  • 我已经有50多个命令行选项,它们都集中在一个地方.
  • 每个类仅使用一组命令行选项.

我试图以某种方式编写一个接口,但未成功.我不确定这是否是一个好方法.我需要一些设计准则.

I tried to write an interface, somehow but I am unsuccessful. I am not sure if this is a good way to do it or not. I need some design guidelines.

这是我编写的代码-

public interface ClassOptions 
{
    Options getClassOptions();
    void setClassOptions(Options options);
}

public class Aclass implements ClassOptions
{
    private String optionA="defaultA";
    private String optionB="defaultB";

    public Options getClassOptions()
    {
        Options options = new Options();    

        options.addOption("A", true, "Enable Option A");
        options.addOption("B", true, "Enable Option B");        

        return options;
    }

    public void setClassOptions(Options options, String args[])
    {
        CommandLineParser parser = new BasicParser();
        CommandLine cmd=null;
        try 
        {
            cmd = parser.parse( options, args);
        } catch (ParseException e) 
        {
            // TODO Auto-generated catch block
            // e.printStackTrace();
            System.out.println("ignored option");
        }

        if(cmd.hasOption("A"))
            optionA = "enabled";

        if(cmd.hasOption("B"))
            optionB = "enabled";    
    }
}

我认为这样编写代码的问题是-

I think the problems in such writing of code are -

  • 有不同类型的参数,例如int,double,string,boolean.如何处理所有这些问题.
  • 例如,
  • getClassOption()和setClassOption()都包含参数"A","B".这段代码很容易在编写代码时出错,我想消除这种错误.
  • 我认为代码在这里变得重复,可以以某种方式封装在另一个类中.
  • 并非所有参数都是必需的,但可以忽略.

谢谢!

推荐答案

我会向您推荐JCommander.

I would recommend to you JCommander.

我认为这是一个非常好的Java参数解析器.

I think it's a really good Argument Parser for Java.

您可以在批注中定义所有Argument内容,然后调用JCommander对其进行解析.最重要的是,它(基于您的注释)还可以打印出相应的帮助页面.您不必担心任何事情.

You define all the Argument stuff within annotations and just call JCommander to parse it. On top of that it also (based on your annotations) can print out the corresponding help page. You don't have to take care about anything.

我相信您会爱上它!:)

I believe you will love it! :)

看看: http://jcommander.org/有很多这样的例子!

Take a look at it: http://jcommander.org/ There are a lot of examples and such!

祝你好运!:)

这篇关于Java中解析命令行参数的代码结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆