Danijel
Danijel

Reputation: 8606

Proper design of command line parameters?

I have a Java class API that looks like this:

boolean categoryAdd( String name, String description, String user );

and I need to create a command line equivalent for it.

How would the command line parameters look like?

Like this

-categoryadd name description user

or like this

-categoryadd -categoryname name -categorydescription description -categoryuser user

or like this

-categoryadd -categoryname=name -categorydescription=description -categoryuser=user

or something else?

Upvotes: 0

Views: 131

Answers (2)

user177800
user177800

Reputation:

Common usage dictates that single character (short) options are in the form of -v and multi-character (long) options are in the form of --verbose as examples.

http://en.wikipedia.org/wiki/Command-line_interface

Seriously consider using a CLI Parsing library that will handle all this for you and more.

My preference is JSAP, it is feature complete, stable and highly useful. Newer libraries use @Annotations and other things but this gets it done for me.

Upvotes: 1

Alex Adas
Alex Adas

Reputation: 210

In java command line string separated by spaces so when you call you method from CLI like:

java MyClass categoryadd name description user

, in you main class you will recieve args with 4 values:

  • categoryadd
  • name
  • description
  • user

This way it will be easy for to manipulate (validate) parameters, because you don't need to do additional manipulations to extract passed variables.

From other hand from user side without additional information it pretty confusing what kind of parameters to pass and in which order. In your 2nd and 3rd propositions you "helping" user to determine which parameters he passes. I prefer the 3rd proposition with equals, because it will be easy to parse the: you will receive parameters like:

  • -categoryadd
  • -categoryname=name
  • -categorydescription=description
  • -categoryuser=user

Now you delay on first and can parse key-value with string.split(=) way. Your 2nd option also can be done but I think it is more tricky

Upvotes: 0

Related Questions