Commons CLI – Home Maven Repository: Command Line Parsers

Commons CLI – Home Maven Repository: Command Line Parsers

Ostermiller Java Utilities


Home
|
Download
|
Install
|
Components
|
Build
|
JavaDoc
|
Source
|
Bugs
|
License


OstermillerUtils
  • Download
  • Installation Instructions
  • Components
  • JavaDoc
  • Browse Source Code
  • Bugs and Features
  • Version History
  • Compile Modifications
  • Contributors
  • License
Documentation
  • Components

    • Array Helper
    • Base 64
    • Browser Opener
    • CGI Query String Parser
    • Circular Buffers
    • Command Line Parser
    • Concatenation Streams
    • CSV Format
    • Date/Time Parser
    • Exec Helper
    • File Helper
    • Iterators and Enumerations
    • Line Ends
    • MD5 Sums
    • No Close Streams
    • Parallelizer
    • Password Dialog
    • Random Password Generator
    • Significant Figures
    • Size Limit Input Stream
    • String Helper
    • String Tokenizer
    • Straight Stream Reader
    • Tabs
    • Uber Properties
  • JavaDoc
  • Browse Source Code

CmdLn: Java Command Line Parser

  • CmdLn.java Source Code
  • CmdLn Javadoc

A command line parser for array of strings passed to Java main methods.

There are three ways to use the command line parser, as detailed with three examples below. Each of the examples creates a command line application that accepts –help –file and –delay arguments with the following help message:

demo command line application -f --file <?> file to open --delay <?> delay in seconds (default 5 seconds)

Option Enum

import com.Ostermiller.util.*;
public class OptionEnum private enum EnumOptions HELP(new CmdLnOption("help",'h')), FILE(new CmdLnOption("file",'f').setRequiredArgument().setDescription("file to open")), DELAY(new CmdLnOption("delay").setOptionalArgument().setDescription("delay in seconds (default 5 seconds)")); private CmdLnOption option; private EnumOptions(CmdLnOption option) option.setUserObject(this); this.option = option; private CmdLnOption getCmdLineOption() return option; public static void main(String[] args) throws Exception CmdLn cmdLn = new CmdLn(args).setDescription("demo command line application"); for (EnumOptions option: EnumOptions.values()) cmdLn.addOption(option.getCmdLineOption()); String fileName = null; int delay = 0; for(CmdLnResult result: cmdLn.getResults()) switch((EnumOptions)result.getOption().getUserObject()) case HELP: cmdLn.printHelp(); System.exit(0); break; case FILE: fileName = result.getArgument(); break; case DELAY: delay = 5000; if (result.getArgumentCount() > 0) delay = Integer.parseInt(result.getArgument()) * 1000; break; if (delay > 0) Thread.sleep(delay); if (fileName != null) // open file and do work... for(String argument: cmd.getNonOptionArguments()) // other arguments 

Call Back to Listeners

import com.Ostermiller.util.*;
public class OptionCallback private String fileName = null; private int delay = 0; private void parseCommandLine(String[] args) final CmdLn cmdLn = new CmdLn(args).setDescription("demo command line application"); cmdLn.addOption( new CmdLnOption("help",'h').setListener( new CmdLnListener() public void found(CmdLnResult result) cmdLn.printHelp(); System.exit(0); ) ); cmdLn.addOption( new CmdLnOption("file",'f').setRequiredArgument().setDescription("file to open").setListener( new CmdLnListener() public void found(CmdLnResult result) fileName = result.getArgument(); ) ); cmdLn.addOption( new CmdLnOption("delay").setOptionalArgument().setDescription("delay in seconds (default 5 seconds)").setListener( new CmdLnListener() public void found(CmdLnResult result) delay = 5000; if (result.getArgumentCount() > 0) delay = Integer.parseInt(result.getArgument()) * 1000; ) ); cmdLn.parse(); public static void main(String[] args) throws Exception OptionCallback optionCallback = new OptionCallback(); optionCallback.parseCommandLine(args); if (optionCallback.delay > 0) Thread.sleep(optionCallback.delay); if (fileName != null) // open file and do work... for(String argument: cmd.getNonOptionArguments()) // other arguments 

Options by Name

import com.Ostermiller.util.*;
public class OptionCallback public static void main(String[] args) throws Exception CmdLn cmdLn = new CmdLn(args).setDescription("demo command line application"); cmdLn.addOptions(new CmdLnOption[] new CmdLnOption("help",'h'), new CmdLnOption("file",'f').setRequiredArgument().setDescription("file to open"), new CmdLnOption("delay").setOptionalArgument().setDescription("delay in seconds (default 5 seconds)"). ); if(cmdLn.getResult('h').present()) cmdLn.printHelp(); System.exit(0); if(cmdLn.getResult("delay").present()) delay = 5000; if (cmdLn.getResult("delay").getArgumentCount() > 0) delay = Integer.parseInt(cmdLn.getResult("delay").getArgument()) * 1000; Thread.sleep(delay); if(cmdLn.getResult('f').present()) // open file and do work... for(String argument: cmd.getNonOptionArguments()) // other arguments 


©
2001-2012


Found

The document has moved here .

About the Author: admin