Foren-Ăśbersicht Letzte Topics Registrieren Anmelden

Java :: OpenSource :: CommandLineParser

Es kann jedes nĂĽtzliche Java-Listing gepostet werden. Vorraussetzung : Es muss funktionieren.

Moderator: Moderatoren

CommandLineParser

Beitragvon Hroudtwolf » 25.03.2012, 15:18

Hi,

Die folgende Klasse bietet einen simplen Command-Line Parser.
Alle Argumente die mit einem Minus anfangen werden als relevante Argumente erfasst und können nachfolgende Argumente welche kein voranführendes Minus haben als Wert nutzen.

Beispiele:
Code: [Alles auswählen]  [Auf-/Zuklappen]
-a -b -c foo             Speichert {-a:true; -b:true, -c: foo}
-a foo                   Speichert {-a: foo}
a                        Speichert nichts


Der Code:
Code: [Alles auswählen]  [Auf-/Zuklappen]
/*
* 2012(c) By Marc Sven Kleinboehl aka Hroudtwolf
* All rights reserved.
*/

package utilities;

import java.util.HashMap;

public class CommandLineParser {

   private HashMap<String, String> commandLineArgument;

   /*
    * CTor.
    * @param   String[]   saArguments      An array of  command line arguments.
    */
   public CommandLineParser (String[] saArguments) {
      
      int nI = 0;
      
      this.commandLineArgument = new HashMap<String, String> ();
      
      while (saArguments.length > nI) {
         
         if (saArguments[nI].startsWith("-") || saArguments[nI].startsWith("/")) {
            if (saArguments.length > nI + 1 && ! (saArguments[nI + 1].startsWith("-") || saArguments[nI + 1].startsWith("/"))) {
               this.commandLineArgument.put (saArguments[nI], saArguments[nI + 1]);
            }else{
               this.commandLineArgument.put (saArguments[nI], "");
            }
         }
         
         nI++;
      }
      
      return;
   }
   
   /*
    * Checks whether a specific argument exists.
    * @param    String sArgument   The key of the argument.
    * @return  boolean            TRUE if the argument exists.
    */
   public boolean hasArgument (String sArgument) {
      
      return this.commandLineArgument.containsKey(sArgument);
   }
   
   /*
    * Retrieves the value of a specific argument.
    * @param    String sArgument   The key of the argument.
    * @return  String            The value of the argument on success. Otherwise an empty string.
    */
   public String getArgumentValue (String sArgument) {
      
      if (! this.hasArgument (sArgument)) {
         return "";
      }

      return this.commandLineArgument.get(sArgument);
   }
   
   /*
    * Retrieves the value of a specific argument.
    * @param    String sArgument   The key of the argument.
    * @param    String sDefault   The optional default value. It will returned if the argument doesn't exists.
    * @return  String            The value of the argument on success. Otherwise an empty string.
    */
   public String getArgumentValue (String sArgument, String sDefault) {
      
      if (! this.hasArgument (sArgument)) {
         return sDefault;
      }

      return this.commandLineArgument.get(sArgument);
   }
}


LG
Wolf
Benutzeravatar
Hroudtwolf
Administrator
 
Beiträge: 9625
Registriert: 02.02.2005, 02:39
  • Website
  • ICQ
  • Position des Users auf der Mitgliederkarte

Re: CommandLineParser

Beitragvon bembulak » 25.03.2012, 16:44

Ahhhh!
Sehr nützlich, endlich mal ein einfacher und verständlicher CL-Parser!

1000 Dank!
MFG
bembu
Benutzeravatar
bembulak
Ehrenmitglied
Ehrenmitglied
 
Beiträge: 3130
Registriert: 18.07.2005, 11:44


ZurĂĽck zu Java :: OpenSource

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 1 Gast

cron