DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Parsing Simple Command Line Arguments In Java, Using The The Commons CLI Library.

Haikal Saadb user avatar by
Haikal Saadb
·
Feb. 13, 07 · · Code Snippet
Like (0)
Save
Tweet
2.77K Views

Join the DZone community and get the full member experience.

Join For Free
// Skeleton to read in two command line arguments, one mandatory, one optional


package snippets;

import org.apache.commons.cli.*;

/* 
 * Stub program that reads command line arguments
 */
public class CommandLineProgram {

	private static Options options = null; // Command line options
	
	private static final String PROPERTIES_LOCATION_OPTION = "f";
	private static final String OUTPUT_FILE_OPTION = "o";
	private static final String DEFAULT_OUTPUT_FILE = "out.feed";
	
	private CommandLine cmd = null; // Command Line arguments
	
	private String outputFile = DEFAULT_OUTPUT_FILE;
	
	static{
		options = new Options();
		options.addOption(PROPERTIES_LOCATION_OPTION, true, 
				"Data file location");
		options.addOption(OUTPUT_FILE_OPTION, false, "Output file. " + DEFAULT_OUTPUT_FILE + " by default ");
		
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		CommandLineProgram cliProg = new CommandLineProgram();
		cliProg.loadArgs(args);
	}
	
	/**
	 * Validate and set command line arguments.
	 * Exit after printing usage if anything is astray
	 * @param args String[] args as featured in public static void main()
	 */
	private void loadArgs(String[] args){
		CommandLineParser parser = new PosixParser();
		try {
			cmd = parser.parse(options, args);
		} catch (ParseException e) {
			System.err.println("Error parsing arguments");
			e.printStackTrace();
			System.exit(1);
		}
		
		// Check for mandatory args
		
		if (! cmd.hasOption(PROPERTIES_LOCATION_OPTION)){
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp("java -jar this_jar.jar", options);
			System.exit(1);
		}
		
		// Look for optional args.
		
		if (cmd.hasOption(OUTPUT_FILE_OPTION)){
			outputFile = cmd.getOptionValue(OUTPUT_FILE_OPTION);
			
		}
	}
}

Command-line interface Java (programming language) Library Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Most Popular Kubernetes Alternatives and Competitors
  • 6 Things Startups Can Do to Avoid Tech Debt
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • How to Submit a Post to DZone

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo