2012-01-20 39 views
1

假設我有一個簡單的程序,它採用自變量輸入以下列形式
DO1 inputLocation outputLocation
DO2 inputLocation outputLocation
DO3 [30或60或90] inputLocation outputLocation一個
DO4 [PD或C] inputLocation outputLocation
DO5 [GHI] inputLocation outputLocation如何處理命令行參數中的Java

我也有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 
} 

我做它似乎不是很可擴展的方式。如果一個函數的參數改變了,或者添加了新的函數,看起來這樣做會很痛苦。 什麼是「最好」的方式去這樣的事情

+0

可能重複(http://stackoverflow.com/問題/ 1200054/JAVA庫換解析的命令行參數) –

回答

10

在這一點上,我會用commons-clijargs。除非你試圖用參數做一些非常特別的事情,我會說專注於你的應用的真實業務,不要處理應用參數的混亂

3

使用一個commnd行解析庫。看看here

2

我以前使用過JOpt Simple,效果很好。它可以讓你抽象出命令行arg混亂,並保持一個非常乾淨的可更新參數列表。另外一個好處是它會生成標準命令行工具的幫助輸出。

繼承人一個簡單的例子:

private void runWithArgs (String[] args) { 
    OptionParser parser = getOptionParser(); 
    OptionSet options = null; 

    try { 
     options = parser.parse (args); 
    } 
    catch (OptionException e) { 
     log.error ("Sorry the command line option(s): " + e.options() + 
        " is/are not recognized. -h for help."); 
     return; 
    } 

    if (options.has ("h")) { 
     try { 
      log.info ("Help: "); 
      parser.printHelpOn (System.out); 
     } 
     catch (IOException e) { 
      log.error ("Trying to print the help screen." + e.toString()); 
     } 
     return; 
    } 

    if (options.has ("i")) { 
     defaultHost = (String) options.valueOf ("i"); 
    } 

    if (options.has ("p")) { 
     defaultPort = (Integer) options.valueOf ("p"); 
    } 

    if (options.has ("q")) { 
     String queryString = (String) options.valueOf ("q"); 

     log.info ("Performing Query: " + queryString); 
     performQuery (queryString, defaultHost, defaultPort); 
     return; 
    } 
} 
1

我用args4j結果成功之前也是如此。

只是另一種選擇。

1

您可以使用塞德里克·貝斯特的JCommander

因爲生命太短解析命令行參數

我甚至創造性地違反了圖書館的原意來解析NMEA 0183這樣的句子$ GPRTE如下:

import java.util.List; 

import com.beust.jcommander.Parameter; 
import com.beust.jcommander.internal.Lists; 

public class GPRTE { 
    @Parameter 
    public List<String> parameters = Lists.newArrayList(); 

    @Parameter(names = "-GPRTE", arity = 4, description = "GPRTE") 
    public List<String> gprte; 
} 

代碼片斷處理NMEA 0183句$ GPRTE$GPRTE,1,1,c,*37-GPRTE 1 1 c *37遵守JCommander解析語法:[?Java庫,用於解析命令行參數]的

/** 
* <b>RTE</b> - route message<p> 
* Processes each <b>RTE</b> message received from the serial port in following format:<p>$GPRTE,d1,d2,d3,d4<p>Example: $GPRTE,1,1,c,*37 
* @param sequences result of {@link #Utils.process(String)} method 
* @see <a href="http://www.gpsinformation.org/dale/nmea.htm#RTE">http://www.gpsinformation.org/dale/nmea.htm#RTE<a><p>*/ 
public static void processGPRTE(final String command){ 
    final String NMEA_SENTENCE = "GPRTE"; 
    final String PARAM = "\u0001"; 
    final String DOLLAR = "\u0004"; 
    final String COMMA = "\u0005"; 

    String parsedString = command; 

    if (parsedString.contains("$"+NMEA_SENTENCE)){ 

     parsedString = parsedString.replaceAll("\\$", DOLLAR+PARAM); 
     parsedString = parsedString.replaceAll(",", COMMA); 

     System.out.println("GPRTE: " + parsedString); 

     String[] splits = parsedString.split(DOLLAR); 
     for(String info: splits){ 

      if (info.contains(PARAM+NMEA_SENTENCE)) { 
       info = info.replaceFirst(PARAM, "-"); 

       System.out.println("GPRTE info: " + info); 


       String[] args = info.split(COMMA); 

       GPRTE cmd = new GPRTE(); 
       new JCommander(cmd, processEmptyString(args)); 
       List<String> message = cmd.gprte; 

       String data1 = SerialOutils.unescape(message.get(0)); 
       System.out.println("GPRTE: data1 = " + data1); 

       String data2 = SerialOutils.unescape(message.get(1)); 
       System.out.println("GPRTE: data2 = " + data2); 

       String data3 = SerialOutils.unescape(message.get(2)); 
       System.out.println("GPRTE: data3 = " + data3); 

       String data4 = SerialOutils.unescape(message.get(3)); 
       System.out.println("GPRTE: data4 = " + data4); 

       System.out.println(""); 

      } 
     } 
    } 
}