Skip to content

CommandLineOption

Jordan Samhi edited this page Aug 22, 2023 · 1 revision

CommandLineOption 🛠

The CommandLineOption class represents a command line option. It encapsulates properties like the long and short forms, description, whether it requires an argument, whether it's mandatory, and its value.

Table of Contents

Overview

The class serves as a representation of a command line option and is structured to provide all essential information related to a command line argument.

Attributes

  • longOpt: Long form of the option, e.g., "help".
  • shortOpt: Short form of the option, e.g., "h".
  • description: Description of the option.
  • hasArg: Flag to indicate if the option requires an argument.
  • isRequired: Flag to indicate if the option is mandatory.
  • value: Value of the option if provided on the command line.

Methods

  • Constructors, getters, and setters are provided for all attributes.

Examples

Example 1: Creating a New CommandLineOption

CommandLineOption helpOption = new CommandLineOption("help", "h", "Displays help information.", false, false);

Example 2: Modifying an Existing CommandLineOption

// Create an option
CommandLineOption fileOption = new CommandLineOption("file", "f", "Specify the file path.", true, true);

// Modify the option
fileOption.setDescription("Specify the path to the input file.");
fileOption.setRequired(false);

Example 3: Querying a CommandLineOption

// Create an option
CommandLineOption verboseOption = new CommandLineOption("verbose", "v", "Enable verbose mode.", false, false);

// Query the option
String description = verboseOption.getDescription(); // Returns "Enable verbose mode."
boolean isRequired = verboseOption.isRequired(); // Returns false

These examples demonstrate the utility of the CommandLineOption class in handling command line options, making it easier to manage and utilize these within a program.