A robust, lightweight library for parsing command-line arguments with strict validation, automatic help generation, and intuitive API.
Designed with modern C++ best practices, input_parser.cpp provides a comprehensive solution for command-line interfaces
Intuitive API with modern C++ constructs. Define options using designated initializers for maximum readability.
Comprehensive error checking with detailed messages. Collects all errors during parsing for complete diagnostics.
Beautifully formatted help messages generated automatically based on your option definitions.
Supports all common CLI formats: short options, long options, equals syntax, and mixed formats.
Continues parsing after encountering errors to provide complete diagnostic information.
Optimized for minimal overhead with O(1) lookups and efficient memory usage.
Integrate input_parser.cpp into your project in minutes with these simple examples
#include "input_parser.hpp"
#include <iostream>
#include <string>
int main(int argc, char** argv) {
// Initialize parser with program metadata
InputParser parser("imgproc", "Advanced image processing utility");
// Define command-line options
parser.add_option({
.short_name = "-v",
.long_name = "--verbose",
.description = "Enable verbose output mode",
.requires_argument = false
});
parser.add_option({
.short_name = "-i",
.long_name = "--input",
.description = "Input image path",
.requires_argument = true,
.arg_placeholder = "PATH"
});
parser.add_option({
.short_name = "-o",
.long_name = "--output",
.description = "Output image path",
.requires_argument = true,
.arg_placeholder = "PATH"
});
parser.add_option({
.short_name = "-q",
.long_name = "--quality",
.description = "Output quality (1-100)",
.requires_argument = true,
.arg_placeholder = "VALUE"
});
// Parse command-line arguments
if (!parser.parse(argc, argv)) {
// Handle parsing errors
std::cerr << "Error parsing arguments:\n";
for (const auto& error : parser.get_errors()) {
std::cerr << " • " << error << "\n";
}
std::cerr << "\n" << parser.generate_help();
return EXIT_FAILURE;
}
// Check for verbose flag
if (parser.has_option("--verbose")) {
std::cout << "Verbose mode enabled\n";
}
// Retrieve input file (required)
auto input_file = parser.get_argument("--input");
if (!input_file) {
std::cerr << "Error: Input file is required\n";
return EXIT_FAILURE;
}
// Retrieve output file (required)
auto output_file = parser.get_argument("--output");
if (!output_file) {
std::cerr << "Error: Output file is required\n";
return EXIT_FAILURE;
}
// Retrieve optional quality setting
int quality = 90;
if (auto q = parser.get_argument("--quality")) {
try {
quality = std::stoi(*q);
if (quality < 1 || quality > 100) {
throw std::out_of_range("Invalid quality value");
}
} catch (...) {
std::cerr << "Error: Invalid quality value: " << *q << "\n";
return EXIT_FAILURE;
}
}
std::cout << "Processing image:\n";
std::cout << " Input: " << *input_file << "\n";
std::cout << " Output: " << *output_file << "\n";
std::cout << " Quality: " << quality << "\n";
// Access positional arguments
auto positional = parser.get_positional_args();
if (!positional.empty()) {
std::cout << "\nPositional arguments:\n";
for (const auto& arg : positional) {
std::cout << " • " << arg << "\n";
}
}
return EXIT_SUCCESS;
}
# Valid command with all options
./imgproc -v -i input.jpg -o output.png --quality=95
# Mixed short and long options
./imgproc --verbose -i input.jpg -o output.png
# With positional arguments
./imgproc input.jpg -o output.png -- additional_file.txt
# Invalid command (missing required argument)
./imgproc -v -i input.jpg
# Output: Error: Output file is required
Comprehensive documentation of all classes, methods, and configuration options
The core class for defining and parsing command-line arguments.
InputParser(std::string program_name,
std::string description)
Parameters:
program_name: Application name for help outputdescription: Brief description of your applicationRegisters a new command-line option with the parser.
void add_option(const Option& opt)
Throws: std::invalid_argument if option names are duplicated or invalid.
Example:
parser.add_option({
.short_name = "-h",
.long_name = "--help",
.description = "Show help message",
.requires_argument = false
});
Checks if an option was provided in the command line.
bool has_option(const std::string& name) const
Accepts both short and long option names. Returns true if the option was found.
Example:
if (parser.has_option("--verbose")) {
enable_logging();
}
Retrieves the argument value for an option if it exists.
std::optional<std::string>
get_argument(const std::string& name) const
Returns the argument value if present, or std::nullopt if the option wasn't found or has no argument.
Example:
if (auto file = parser.get_argument("--input")) {
process_file(*file);
}
Returns a list of all positional arguments.
const std::vector<std::string>&
get_positional_args() const
Positional arguments are all arguments that are not options and not arguments to options.
Example:
for (const auto& file : parser.get_positional_args()) {
process_file(file);
}
Returns a list of parsing errors encountered during the last parse operation.
const std::vector<std::string>&
get_errors() const
Each error is a human-readable string describing a specific parsing issue.
Example:
for (const auto& error : parser.get_errors()) {
std::cerr << "Error: " << error << "\n";
}
Understanding the internal process of command-line argument parsing
Create InputParser instance with program name and description. Register all options with add_option().
Split command-line arguments into tokens. Identify option prefixes (-, --) and positional arguments.
For each token, determine if it's a known option (short or long form). Handle equals-syntax options (--file=path.txt).
Verify that required arguments are present. Check that options expecting no arguments don't have them.
Store parsed values in internal data structures. Track positional arguments separately.
Compile all parsing errors. Return true if no errors, false otherwise.
Comparison with other command-line parsing solutions for C++