Java open command line

Process the command line with CLI in Java

Writing code to parse command line arguments is still necessary sometimes. When you need to examine command line arguments, see how and why you should use your open source Java toolkit and use Command Line Interface.

Writing code to parse command line arguments
isn’t the most exciting job, but sometimes it’s still necessary.
The next time you need to examine command line arguments and things
get a little complex, whip out your trusty open source Java
toolkit, and use Command Line Interface (CLI).

Jakarta Commons hosts the CLI project. While
it’s overkill if you only have one or two arguments, it’s essential
if your application takes most of its settings from the command
line.

To use CLI, you need to create an instance of
the Options class:

With this instance of Options, you define the
command line arguments that your application will accept. One way
to do this is by using the addOption() method of the Options class.
Call this method once for each option that your application can
accept.

Читайте также:  Java читает все форматы

opt.addOption(“h”, false, “Print help for this
application”);
opt.addOption(“u”, true, “The username to use”);
opt.addOption(“dsn”, true, “The data source to use”);

Once you define your classes’ arguments, create
a CommandLineParser, and parse the String array that was passed to
your main method.

BasicParser parser = new BasicParser();
CommandLine cl = parser.parse(opt, args);

Now that all of the arguments are parsed, you
can examine the CommandLine instance returned by the parser to
determine what arguments and values were supplied by the user.

if ( cl.hasOption(‘h’) ) HelpFormatter f = new
HelpFormatter();
f.printHelp(“OptionsTip”, opt);
>
else

As you can see from above, you can use the
HelpFormatter class to automatically generate usage information for
your program.

// OptionsTip.java
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.ParseException;

public class OptionsTip public static void main(String args[])
try Options
opt = new Options();

opt.addOption(“h”,
false, “Print help for this application”);
opt.addOption(“u”,
true, “The username to use”);

opt.addOption(“dsn”, true, “The data source to use”);

BasicParser
parser = new BasicParser();
CommandLine
cl = parser.parse(opt, args);

if
( cl.hasOption(‘h’) ) HelpFormatter
f = new HelpFormatter();

>
catch
(ParseException e) e.printStackTrace();

CLI takes a tedious chore off your hands and
makes parsing command line arguments a simple task. For more
information, check
out the documentation.

Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically sign up today!

Person using a laptop computer.

Subscribe to the Daily Tech Insider Newsletter

Stay up to date on the latest in technology with Daily Tech Insider. We bring you news on industry-leading companies, products, and people, as well as highlighted articles, downloads, and top resources. You’ll receive primers on hot tech topics that will help you stay ahead of the game.

Account Information

Share with Your Friends

Account Information

Contact davidpetersheim

Editor’s Picks

TechRepublic Premium Editorial Calendar: IT Policies, Checklists, Hiring Kits and Research for Download

TechRepublic Premium content helps you solve your toughest IT issues and jump-start your career or next project.

Published: July 3, 2023, 4:30 AM EDT Modified: July 3, 2023, 11:34 AM EDT Read More See more TechRepublic Premium

Artificial intelligence and modern computer technology image concept.

Microsoft’s First Generative AI Certificate Is Available for Free

Published: June 30, 2023, 5:16 PM EDT Modified: July 5, 2023, 4:00 PM EDT Read More See more Artificial Intelligence

An image representing cloud servers.

How Generative AI is a Game Changer for Cloud Security

Generative AI will be a game changer in cloud security, especially in common pain points like preventing threats, reducing toil from repetitive tasks, and bridging the cybersecurity talent gap.

Published: June 29, 2023, 12:34 PM EDT Modified: June 30, 2023, 3:40 PM EDT Read More See more Cloud Security

A computer running payroll software.

The 8 Best International Payroll Services for 2023

Does your business need a payroll provider that offers international payroll services? Use our buyer’s guide to review the best solutions, from ADP to Oyster.

Published: July 10, 2023, 7:28 AM EDT Modified: July 14, 2023, 6:44 PM EDT Read More See more Payroll

someone using ChatGPT on a laptop

ChatGPT Cheat Sheet: Complete Guide for 2023

Get up and running with ChatGPT with this comprehensive cheat sheet. Learn everything from how to sign up for free to enterprise use cases, and start using ChatGPT quickly and effectively.

Published: July 21, 2023, 11:50 AM EDT Modified: July 21, 2023, 1:41 PM EDT Read More See more Artificial Intelligence

6 Best monday.com Competitors and Alternatives for 2023

Looking for an alternative to monday.com? Our comprehensive list covers the best monday alternatives, their key features, pricing, pros, cons and more.

Published: July 6, 2023, 1:00 AM EDT Modified: July 6, 2023, 4:01 PM EDT Read More See more Project Management

Person using a laptop computer.

Subscribe to the Daily Tech Insider Newsletter

Stay up to date on the latest in technology with Daily Tech Insider. We bring you news on industry-leading companies, products, and people, as well as highlighted articles, downloads, and top resources. You’ll receive primers on hot tech topics that will help you stay ahead of the game. Delivered Weekdays

TechRepublic Premium

Conflict of Interest Disclosure Policy

This Conflict of Interest Disclosure Policy from TechRepublic Premium establishes the ground rules that will allow a hired consultant to work on another project for another party. Company permission for such a situation is discretionary by the company and is dependent on meeting the requirements of this policy. From the policy: To use the Company’s . Provided By TechRepublic Premium Published: Jul 2023 Modified: Jul 2023 Read More

Intrusion Detection Policy

All modern enterprises must accept the fact that at some point their systems or networks will very likely experience an unauthorized intrusion of some kind. This is the state of the world’s current security environment and for the most part enterprises, especially well-managed ones, have come to terms with this fate while also doing their . Provided By TechRepublic Premium Published: Jul 2023 Modified: Jul 2023 Read More

Feature Comparison: CRM Software and Services

Choosing the right customer relationship management software or service for your business requires strategy, thoughtful consideration and more than a little research. These guidelines and the accompanying tool, from TechRepublic Premium, will give you a customizable framework to find the best CRM solution for your needs. From the guide: Before starting to evaluate CRM systems . Provided By TechRepublic Premium Published: Jul 2023 Modified: Jul 2023 Read More

eLearning and Continuing Education Policy

Regardless of its industry or business, an enterprise can only reach the level of success that is achievable by the people that exist within that enterprise. Strategic plans, marketing campaigns, new product designs, technological innovations, etc., are all dependent on skilled people. If your enterprise views labor as a cost to be controlled rather than . Provided By TechRepublic Premium Published: Jul 2023 Modified: Jul 2023 Read More

Источник

Оцените статью