Jammy
Jammy

Reputation: 423

Handling options in a command-line program

This is more of a (newbie) general programming question than a java-specific challenge. It's my target language if it makes a difference.

How do you sanely handle a program with options that may have multiple combinations during operation?

For example, let's say I have a music player command line app that can run as follows: muzak -s -v -a -i my_music_dir

*-s: shuffle *-r: replay once *-v: reverse playlist *-a: replay all (if -a and -r is active at the same time, -a overrides) *-i: ignore previously played (same opportunity for file to be replayed)

When I'm writing my methods, how do I keep track of all of the possible options? E.g. muzak -s -v my_music_dir results in a different playlist than muzak -v -s my_music_dir for a list with the same starting order.

Upvotes: 2

Views: 726

Answers (3)

Beri
Beri

Reputation: 11610

I would advise you to use switch, as this approach is clean and scales nicely (new options)

public static void main(String[] args) {

Options options= new Options();

for(String arg: args){ // iterate over all options
  if(arg == null) continue; // switch does not like nulls

  switch(arg){ // since JDK7 you can switch over Strings
   case "-r":
     options.setReplay(true); break; 
   case "-a" : 
     options.setReplayAll(true); break; 
   default:
     throw new ParseException("Unknown option: "+ arg)
  }
}

.... // rest of your code

}

As for rest of the code I would advice you to create a class Options:

class Options{

  boolean replay = false;
  boolean replayAll = false;

  // getters and setters

  // other methods holding flag combinations, like:
  public boolean replayNone(){
   return !replay && ! replayAll;
  }
}

Upvotes: 0

u_w
u_w

Reputation: 23

You can do something fairly simple like this. I'm taking the array of command line parameters, checking if it contains each flag and setting a boolean value accordingly.

Afterwards I can handle each special scenario using if conditions using my booleans.

public static void main(String[] args) {

    boolean replay = false;
    boolean replayAll = false;

    if (Arrays.asList(args).contains("-r")) {
        replay = true;
    }
    if (Arrays.asList(args).contains("-a")) {
        replayAll = true;
    }

    //handle special scenarios at the end
    if (replay && replayAll) {
        replay = false;
        //keep only replayAll true
    }

    System.out.println(replay); 
    System.out.println(replayAll);

}

So if you do java Music -r -a, the result will be:

repeat: false

repeatAll: true

Upvotes: 0

Sergey Gornostaev
Sergey Gornostaev

Reputation: 7787

If you don't want to reinvent the wheel, use Apache Commons CLI

import org.apache.commons.cli.*;

public class Main {
    public static void main(String args[]) {
        Options options = new Options();
        options.addOption("t", false, "display current time");
        CommandLineParser parser = new DefaultParser();
        try {
            CommandLine cmd = parser.parse(options, args);
            if(cmd.hasOption("t")) {
                System.out.println("-t is set");
            }
        }
        catch(ParseException exp) {}
    }
}

Upvotes: 2

Related Questions