javaapachecommand-line-interfaceapache-commons-cli

Can I distinguish short and long options in org.apache.commons.cli


Suppose I have a tool which uses org.apache.commons.cli package to take command line options. I define an option

Options opts    = new Options();
opts.addOption("v", "version",  false, "print tool version information");

So the user can call my tool with -v or with --version command line options.

The question is, can I then tell in my code if the user typed -v or --version?

What I am thinking is to associate long versions of command line options with verbose output, and short versions with non-verbose. So, if a user passes -v, he/she will get version number of the tool, but if a user types --version, he/she will get version of the tool, versions of dependencies, some other versioning and vendor info, etc.

Two questions:

  1. Can it be done with org.apache.commons.cli effectively?
  2. Is it a good idea from the user perspective? Is this a common practice or am I inventing something esoteric here?

Experienced thought on both implementation and the philosophy of this approach are appreciated.

Thanks in advance


Solution

  • I don't think you will get that information this way, as the commandline is parsed into matching Options without access to the actualcommandline after parsing.

    I would define two separate Option-objects here, one for 'v' and one for 'version', then you can check, which of the two options was matched and act accordingly.