Skip to content

CommandLineOptions

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

CommandLineOptions 🎛

The CommandLineOptions class is a singleton class that handles command-line options parsing and validation. It encapsulates the Apache Commons CLI library for this purpose.

Table of Contents

Overview

This class serves as a central point for managing command-line options within the application, allowing for easy addition, retrieval, and parsing of options.

Attributes

  • options: Apache Commons CLI Options object for regular options.
  • firstOptions: Apache Commons CLI Options object for first options like help.
  • parser: Apache Commons CLI Parser object to parse command-line options.
  • cmdLine: Apache Commons CLI CommandLine object holding parsed options.
  • appName: The application's name.
  • optionList: List of CommandLineOption objects.

Methods

  • Singleton Pattern: v() method to get the singleton instance.
  • Parsing: parseArgs(String[] args) for parsing command-line arguments.
  • Option Management: addOption(CommandLineOption option) to add an option, and getOptionValue(String longOpt) to retrieve an option's value.
  • Application Name Management: getAppName() and setAppName(String appName) to get and set the application name, respectively.

Examples

Example 1: Singleton Instance Retrieval

CommandLineOptions clo = CommandLineOptions.v();

Example 2: Adding and Parsing Options

CommandLineOptions clo = CommandLineOptions.v();
clo.setAppName("MyApp");

// Adding options
CommandLineOption helpOption = new CommandLineOption("help", "h", "Displays help information.", false, false);
clo.addOption(helpOption);

// Parsing arguments
String[] args = {"-h"};
clo.parseArgs(args);

Example 3: Retrieving Option Value

CommandLineOptions clo = CommandLineOptions.v();
CommandLineOption fileOption = new CommandLineOption("file", "f", "Specify the file path.", true, true);
clo.addOption(fileOption);

String[] args = {"-f", "/path/to/file.txt"};
clo.parseArgs(args);

String filePath = clo.getOptionValue("file"); // Returns "/path/to/file.txt"

Certainly! Below is a typical example demonstrating the use of the CommandLineOptions class for Android analysis with the AndroSpecter library. This example could be part of a setup for an experiment involving FlowDroid analysis with AndroLibZoo, including connectivity to a Redis server.

Example 4: AndroLibZoo FlowDroid Experiment Configuration

// Create and configure the CommandLineOptions instance
CommandLineOptions options = CommandLineOptions.v();
options.setAppName("AndroLibZoo FlowDroid Experiment");

// Set various necessary options
options.addOption(new CommandLineOption("apikey", "a", "AndroZoo API key", true, true));
options.addOption(new CommandLineOption("platforms", "p", "Platform file", true, true));
options.addOption(new CommandLineOption("redis-srv", "s", "Sets the redis server address", true, true));
options.addOption(new CommandLineOption("redis-port", "n", "Sets the redis port to connect to", true, true));
options.addOption(new CommandLineOption("redis-pwd", "w", "Sets the redis password", true, true));
options.addOption(new CommandLineOption("redis-root", "o", "Sets the redis root list/set", true, true));
options.addOption(new CommandLineOption("tool", "t", "The tool to use", true, true));

// Parse the command-line arguments
options.parseArgs(args);

This code snippet demonstrates how the AndroSpecter library can be leveraged to configure various aspects of an Android analysis experiment. It includes setting up the API key, platform file, Redis server configurations, and specifying the analysis tool. The parseArgs method is then used to parse the command-line arguments, ensuring that all required parameters are properly configured for the experiment.

CommandLineOptions class simplifies the task of handling command-line options within a Java application, providing an efficient way to add, parse, and retrieve options. It is designed to be used in conjunction with the CommandLineOption class, enabling an integrated and clear approach to command-line parsing.