cli-parser-cleanup (#15)
All checks were successful
Build ptprnt / build (push) Successful in 3m50s
All checks were successful
Build ptprnt / build (push) Successful in 3m50s
Reviewed-on: moritz/ptouch-prnt#15
This commit was merged in pull request #15.
This commit is contained in:
94
src/cli/CliParser.cpp
Normal file
94
src/cli/CliParser.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#include "CliParser.hpp"
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
CliParser::CliParser(std::string appDescription, std::string versionString)
|
||||
: mApp(std::move(appDescription)), mVersionString(std::move(versionString)) {
|
||||
setupParser();
|
||||
}
|
||||
|
||||
int CliParser::parse(int argc, char** argv) {
|
||||
try {
|
||||
mApp.parse(argc, argv);
|
||||
} catch (const CLI::CallForHelp& e) {
|
||||
// User requested help - display it and signal clean exit
|
||||
mApp.exit(e);
|
||||
return 1; // Signal: exit cleanly
|
||||
} catch (const CLI::CallForVersion&) {
|
||||
// User requested version - already displayed by callback
|
||||
return 1; // Signal: exit cleanly
|
||||
} catch (const CLI::ParseError& e) {
|
||||
// Parse error - display error message
|
||||
mApp.exit(e);
|
||||
return -1; // Signal: error
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void CliParser::setupParser() {
|
||||
// Version callback
|
||||
auto printVersion = [this](std::size_t) {
|
||||
fmt::print("ptprnt version: {}\n", mVersionString);
|
||||
throw CLI::CallForVersion();
|
||||
};
|
||||
|
||||
// General options
|
||||
mApp.add_flag("-v,--verbose", mOptions.verbose, "Enable verbose output");
|
||||
mApp.add_flag("--trace", mOptions.trace, "Enable trace output (shows USB communication)");
|
||||
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
|
||||
|
||||
// Printer selection
|
||||
mApp.add_option("-p,--printer", mOptions.printerSelection,
|
||||
"Select printer driver (default: auto). Use --list-all-drivers to see available options")
|
||||
->default_val("auto");
|
||||
|
||||
mApp.add_flag("--list-all-drivers", mOptions.listDrivers, "List all available printer drivers and exit");
|
||||
|
||||
// Text printing options
|
||||
// Note: CLI11 options are processed in order when using ->each() with callbacks
|
||||
mApp.add_option(
|
||||
"-t,--text",
|
||||
"Text to print (can be used multiple times, use formatting options before to influence text layout)")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& text) { mOptions.commands.emplace_back(CommandType::Text, text); });
|
||||
|
||||
// Text formatting options
|
||||
mApp.add_option("-f,--font", "Font used for the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& font) { mOptions.commands.emplace_back(CommandType::Font, font); });
|
||||
|
||||
mApp.add_option("-s,--fontsize", "Font size of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& size) { mOptions.commands.emplace_back(CommandType::FontSize, size); });
|
||||
|
||||
mApp.add_option("--valign", "Vertical alignment of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& align) { mOptions.commands.emplace_back(CommandType::VAlign, align); });
|
||||
|
||||
mApp.add_option("--halign", "Horizontal alignment of the following text occurrences")
|
||||
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
|
||||
->each([this](const std::string& align) { mOptions.commands.emplace_back(CommandType::HAlign, align); });
|
||||
}
|
||||
|
||||
} // namespace ptprnt::cli
|
||||
75
src/cli/CliParser.hpp
Normal file
75
src/cli/CliParser.hpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <CLI/CLI.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "interface/ICliParser.hpp"
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
/**
|
||||
* @brief CLI argument parser for ptprnt
|
||||
*
|
||||
* Concrete implementation of ICliParser using CLI11.
|
||||
* Handles all command-line argument parsing.
|
||||
* Separates CLI concerns from core library functionality.
|
||||
*/
|
||||
class CliParser : public ICliParser {
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a CLI parser
|
||||
* @param appDescription Application description for help text
|
||||
* @param versionString Version string to display
|
||||
*/
|
||||
CliParser(std::string appDescription, std::string versionString);
|
||||
|
||||
~CliParser() override = default;
|
||||
|
||||
CliParser(const CliParser&) = delete;
|
||||
CliParser& operator=(const CliParser&) = delete;
|
||||
CliParser(CliParser&&) = delete;
|
||||
CliParser& operator=(CliParser&&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Parse command line arguments
|
||||
* @param argc Argument count
|
||||
* @param argv Argument values
|
||||
* @return 0 on success, positive value if should exit immediately (help/version), negative on error
|
||||
*/
|
||||
int parse(int argc, char** argv) override;
|
||||
|
||||
/**
|
||||
* @brief Get the parsed options
|
||||
* @return Reference to parsed options
|
||||
*/
|
||||
[[nodiscard]] const CliOptions& getOptions() const override { return mOptions; }
|
||||
|
||||
private:
|
||||
void setupParser();
|
||||
|
||||
CLI::App mApp;
|
||||
std::string mVersionString;
|
||||
CliOptions mOptions;
|
||||
};
|
||||
|
||||
} // namespace ptprnt::cli
|
||||
73
src/cli/interface/ICliParser.hpp
Normal file
73
src/cli/interface/ICliParser.hpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
ptrnt - print labels on linux
|
||||
Copyright (C) 2025 Moritz Martinius
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ptprnt::cli {
|
||||
|
||||
/**
|
||||
* @brief Types of CLI commands that can be issued
|
||||
*/
|
||||
enum class CommandType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 };
|
||||
|
||||
/**
|
||||
* @brief A command with its type and value
|
||||
*/
|
||||
using Command = std::pair<CommandType, std::string>;
|
||||
|
||||
/**
|
||||
* @brief Parsed CLI options and commands
|
||||
*/
|
||||
struct CliOptions {
|
||||
bool verbose{false};
|
||||
bool trace{false};
|
||||
bool listDrivers{false};
|
||||
std::string printerSelection{"auto"};
|
||||
std::vector<Command> commands{};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Interface for CLI argument parsing
|
||||
*
|
||||
* This interface allows for mocking CLI parsing in unit tests
|
||||
* and provides a clear contract for CLI parser implementations.
|
||||
*/
|
||||
class ICliParser {
|
||||
public:
|
||||
virtual ~ICliParser() = default;
|
||||
|
||||
/**
|
||||
* @brief Parse command line arguments
|
||||
* @param argc Argument count
|
||||
* @param argv Argument values
|
||||
* @return 0 on success, positive value if should exit immediately (help/version), negative on error
|
||||
*/
|
||||
virtual int parse(int argc, char** argv) = 0;
|
||||
|
||||
/**
|
||||
* @brief Get the parsed options
|
||||
* @return Reference to parsed options
|
||||
*/
|
||||
[[nodiscard]] virtual const CliOptions& getOptions() const = 0;
|
||||
};
|
||||
|
||||
} // namespace ptprnt::cli
|
||||
Reference in New Issue
Block a user