Skip to content

CommandLineOption

Jordan Samhi edited this page Oct 25, 2026 · 3 revisions

CommandLineOption

com.jordansamhi.androspecter.commandlineoptions.CommandLineOption is a mutable option declaration used by CommandLineOptions. It stores aliases, help text, whether the option takes a value, and whether it is required. Configure the descriptor before registering it.

This page describes the 2.0.0 API.

Constructor

new CommandLineOption(longOpt, shortOpt, description, hasArg, isRequired)
Argument Meaning
longOpt Long alias such as apk, without --; may be null when a short alias is used
shortOpt Short alias such as a, without -; may be null when a long alias is used
description Help text; may be null
hasArg true for an option taking one value; false for a flag
isRequired Whether the option must be present unless help is requested

Construction and setters do not validate a declaration. Registration with CommandLineOptions.addOption(...) performs validation and rejects conflicting aliases. h and help are reserved by the parser's built-in help flag.

Configure before registration

This complete utility class creates an independent parser. Call its returned parser's parse(args) method and handle help and validation as shown on CommandLineOptions.

import com.jordansamhi.androspecter.commandlineoptions.CommandLineOption;
import com.jordansamhi.androspecter.commandlineoptions.CommandLineOptions;

public final class OptionExample {
    /**
     * Creates a parser with a required APK path and an optional verbose flag.
     * @return a new configured parser, not yet used to parse arguments
     */
    public static CommandLineOptions createOptions() {
        CommandLineOption apk = new CommandLineOption(
            "apk", "a", "Input file", true, true);
        apk.setDescription("Readable Android APK file");

        CommandLineOption verbose = new CommandLineOption(
            "verbose", "v", "Enable application verbosity", false, false);

        CommandLineOptions options = new CommandLineOptions("my-analysis-tool");
        options.addOption(apk);
        options.addOption(verbose);
        return options;
    }
}

addOption(...) snapshots the declaration. Calling apk.setRequired(false) after registration would change the descriptor but would not make the registered option optional. Configure before registration or create a new parser with the desired declarations.

Accessors and legacy values

Property Read Write
Long alias getLongOpt() setLongOpt(...)
Short alias getShortOpt() setShortOpt(...)
Help text getDescription() setDescription(...)
Takes one value hasArg() setHasArg(...)
Mandatory option isRequired() setRequired(...)
Legacy parsed value getValue() setValue(...)

Descriptors are mutable and not thread-safe. Their value property supports legacy code: the registering parser clears it before each parse attempt and updates it after a successful parse. Manually setting this property does not supply a command-line argument or change the parser's immutable results.

For new code, read ParseResult.getOptionValue(...) and ParseResult.hasOption(...). A present flag has no value, so its descriptor value may be null; use option presence rather than value to test a flag.

Clone this wiki locally