简体   繁体   中英

How can I pass arguments like these for example: runType=EOD threadCnt=10 cleanLogs=true in my commandline java program?

What is the best way to pass these arguments to a java commandline program. Currently, I have order dependent args that the user must remember. My program is called as below:

CommandLineRunner EOD 10 true

I want the user to be able to do this:

CommandLineRunner runType=EOD threadCnt=10 cleanLogs=true

Or change order of arguments.

CommandLineRunner threadCnt=10 runType=EOD cleanLogs=true

I would prefer not to use an external library for this but if there is a good one in java, please provide an example of how to use it to build my example. The = signs must be there since I need to make this program follow a standard for other python/ruby based cli applications we have in our organization.

Thank you in advance.

You can pass the parameters in the command line just after the main class:

java CommandLineRunner "runType=EOD" "threadCnt=10" "cleanLogs=true"

Then in the program:

import java.util.*;

public class CommandLineRunner {

    public static void main(String args[]){

        //Collect the input parameters...
        HashMap<String, String> argsMap = new HashMap<String, String>();
        for (int i = 0; i < args.length; i++){
            StringTokenizer st = new StringTokenizer(args[i], "=");
            while(st.hasMoreTokens()) {
                String key = st.nextToken();
                //Handle the case you don't define a key/value with '='
                try{
                    String val = st.nextToken();
                    argsMap.put(key, val);
                }catch(NoSuchElementException e){
                    argsMap.put(key, "");
                }
            } 
        }

        //Then use them when needed...
        for (Map.Entry<String,String> entry : argsMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println("arg: "+ key + "\tval:" + value);

            if(key.equals("runType")){
                //Add your business logic here
            }
            else if (key.equals("threadCnt")){
                //Add your business logic here
            }
            else if (key.equals("cleanLogs")){
                //Add your business logic here
            }else
                //Unknown argument
                throw new RuntimeException("Invalid argument!");
        }
    }
}

So you will have your arguments in your map, despite of their input order.

Note

From Java7 you can use the more elegant switch construct to handle String (thus your arguments) too:

    //Then use them when needed...
    for (Map.Entry<String,String> entry : argsMap.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println("arg: "+ key + "\tval:" + value);

        switch (key) {
            case "runType":
                //Add your business logic here
                break;
            case "threadCnt":
                //Add your business logic here
                break;
            case "cleanLogs":
                //Add your business logic here
                break;
            default:
                //Unknown argument
                throw new RuntimeException("Invalid argument!");
        }
    }

This will work as long as you don't use spaces in the values:

java CommandLineRunner threadCnt=10 runType=EOD cleanLogs=true

Then you'll have to parse each of the three arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM