Dokit
Internal development documentation
Loading...
Searching...
No Matches
abstractcommand.h
1// SPDX-FileCopyrightText: 2022-2024 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#ifndef DOKIT_ABSTRACTCOMMAND_H
5#define DOKIT_ABSTRACTCOMMAND_H
6
7#include <QBluetoothDeviceInfo>
8#include <QCommandLineParser>
9#include <QLoggingCategory>
10#include <QObject>
11
13
15{
17
18public:
19 /// Supported output formats.
20 enum class OutputFormat {
21 Csv, ///< RFC 4180 compliant CSV text.
22 Json, ///< RFC 8259 compliant JSON text.
23 Text, ///< Plain unstructured text.
24 };
25
26 explicit AbstractCommand(QObject * const parent = nullptr);
27
28 virtual QStringList requiredOptions(const QCommandLineParser &parser) const;
29 virtual QStringList supportedOptions(const QCommandLineParser &parser) const;
30
31 static QString escapeCsvField(const QString &field);
32
33 template<typename R>
34 static quint32 parseNumber(const QString &value, const QString &unit, const quint32 sensibleMinimum = 0);
35
36public slots:
37 virtual QStringList processOptions(const QCommandLineParser &parser);
38 virtual bool start() = 0;
39
40protected:
41 QString deviceToScanFor; ///< Device (if any) that were passed to processOptions().
42 PokitDiscoveryAgent * discoveryAgent; ///< Agent for Pokit device descovery.
43 OutputFormat format { OutputFormat::Text }; ///< Selected output format.
44 static Q_LOGGING_CATEGORY(lc, "dokit.cli.command", QtInfoMsg); ///< Logging category for UI commands.
45
46protected slots:
47 virtual void deviceDiscovered(const QBluetoothDeviceInfo &info) = 0;
48 virtual void deviceDiscoveryFinished() = 0;
49
50 friend class TestAbstractCommand;
51};
52
53#endif // DOKIT_ABSTRACTCOMMAND_H
The AbstractCommand class provides a consistent base for the classes that implement CLI commands.
QString deviceToScanFor
Device (if any) that were passed to processOptions().
AbstractCommand(QObject *const parent=nullptr)
Constructs a new command with parent.
virtual QStringList supportedOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names supported by this command.
static quint32 parseNumber(const QString &value, const QString &unit, const quint32 sensibleMinimum=0)
Returns value as an integer multiple of the ratio R.
virtual void deviceDiscovered(const QBluetoothDeviceInfo &info)=0
Handles PokitDiscoveryAgent::pokitDeviceDiscovered signal.
PokitDiscoveryAgent * discoveryAgent
Agent for Pokit device descovery.
OutputFormat format
Selected output format.
OutputFormat
Supported output formats.
@ Text
Plain unstructured text.
@ Csv
RFC 4180 compliant CSV text.
@ Json
RFC 8259 compliant JSON text.
static Q_LOGGING_CATEGORY(lc, "dokit.cli.command", QtInfoMsg)
Logging category for UI commands.
virtual bool start()=0
Begins the functionality of this command, and returns true if begun successfully, false otherwise.
virtual QStringList processOptions(const QCommandLineParser &parser)
Processes the relevant options from the command line parser.
virtual void deviceDiscoveryFinished()=0
Handles PokitDiscoveryAgent::deviceDiscoveryFinished signal.
static QString escapeCsvField(const QString &field)
Returns an RFC 4180 compliant version of field.
virtual QStringList requiredOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names required by this command.
The PokitDiscoveryAgent class discovers nearby Pokit devices.
Q_OBJECTQ_OBJECT
QObject * parent() const const