Monday 30 January 2017

Java: Passing Command Line Options

You might want your users to pass command line options and make them human friendly, like the one below,

$java -jar CommandLineOptions -d -e 'myemail@email.com' -h 'hostname' -p 'port' -n 'Sudhakar'

An expanded usage help for the customer will be
$java -jar CommandLineOptions
usage: CommandLineOptions
Note: Pass the arguments correctly
 -d                 Customer has details
 -e,--email <arg>   provide email address
 -h,--host <arg>    provide host address
 -n,--name <arg>    provide name
 -p,--port <arg>    provide port number

Please get in touch with Sudhakar Betha for more options

To Achieve this, you will need to include apache commons=-cli to your project build path - https://mvnrepository.com/artifact/commons-cli/commons-cli/1.2

You might want to go through usage & api for reference prior to this

Below is the Main Class for this implementation,
https://github.com/sudhakarbetha/CoreJava/blob/master/CoreJava/src/first/CommandLineOptions.java


package first;
import org.apache.commons.cli.*;
/** * Created by Sudhakar on 30-01-2017. */public class CommandLineOptions {


    public void printName(String name) {
        System.out.println("Name=" + name);    }

    public void printEmail(String email) {
        System.out.println("Email=" + email);    }

    public void printHost(String host) {
        System.out.println("Host=" + host);    }

    public void printPort(int port) {
        System.out.println("Port=" + port);    }

    public static void main(String args[]) {

        CommandLineOptions object = new CommandLineOptions();
        Options options = new Options();
        Option d = new Option("d", false, "Customer has details");        Option email = new Option("e", "email", true, "provide email address");        Option host = new Option("h", "host", true, "provide host address");        Option port = new Option("p", "port", true, "provide port number");        Option name = new Option("n", "name", true, "provide name");
        options.addOption(d);        options.addOption(email);        options.addOption(host);        options.addOption(port);        options.addOption(name);
        CommandLineParser parser = new BasicParser();

        CommandLine commandLine;        try {
            commandLine = parser.parse(options, args);

            if (commandLine.hasOption("d")
                    && commandLine.hasOption("e")
                    && commandLine.hasOption("n")
                    && commandLine.hasOption("h")
                    && commandLine.hasOption("p")) {

                object.printEmail(commandLine.getOptionValue("e"));                object.printHost(commandLine.getOptionValue("h"));                object.printName(commandLine.getOptionValue("n"));                object.printPort(Integer.parseInt(commandLine.getOptionValue("p")));
            } else {
                HelpFormatter formatter = new HelpFormatter();                String header = "Note: Pass the arguments correctly";                String footer = "Please get in touch with Sudhakar Betha for more options";                formatter.printHelp("CommandLineOptions", header, options, footer);
            }
        } catch (ParseException e) {
            System.out.println("ParseException:" + e);        }


    }


}



Build your project jar with the dependencies (commons-cli)
$ java -jar CommandLineOptions.jar  -d -e myemail@gmail.com -h host-mypc -p 8080 -n 'Sudhakar Betha'

Email=myemail@gmail.com
Host=host-mypc
Name=Sudhakar Betha
Port=8080

No comments:

Post a Comment

Linux : Create a new user for the machine

Creating a new user in linux is sometimes needed, when you want to share the user access with anyone. SSH to the machine over the newly cre...