Commit e43fb27a authored by nicolas's avatar nicolas

feat(Cli.cpp) : add the --cli-help option to display the help of the cli

parent d7baf780
...@@ -60,14 +60,6 @@ ...@@ -60,14 +60,6 @@
<source>commandLineOptionConfigArg</source> <source>commandLineOptionConfigArg</source>
<translation>file</translation> <translation>file</translation>
</message> </message>
<message>
<source>commandLineOptionCmd</source>
<translation>run a command line</translation>
</message>
<message>
<source>commandLineOptionCmdArg</source>
<translation>command line</translation>
</message>
<message> <message>
<source>commandLineOptionHelp</source> <source>commandLineOptionHelp</source>
<translation>show this help</translation> <translation>show this help</translation>
...@@ -76,6 +68,14 @@ ...@@ -76,6 +68,14 @@
<source>commandLineOptionVersion</source> <source>commandLineOptionVersion</source>
<translation>show app version</translation> <translation>show app version</translation>
</message> </message>
<message>
<source>commandLineCliHelp</source>
<translation>displays the help menu to use Linphone with the CLI.</translation>
</message>
<message>
<source>commandLineDescription</source>
<translation>send aan order to the application towards a command line.</translation>
</message>
</context> </context>
<context> <context>
<name>AssistantAbstractView</name> <name>AssistantAbstractView</name>
...@@ -413,25 +413,61 @@ Server url not configured.</translation> ...@@ -413,25 +413,61 @@ Server url not configured.</translation>
</context> </context>
<context> <context>
<name>Cli</name> <name>Cli</name>
<message>
<source>linphoneCliDescription</source>
<translation>Way to control the Linphone application through command lines.</translation>
</message>
<message>
<source>uriCommandLineSyntax</source>
<translation>linphone sip:&lt;sip-address&gt;?method=&lt;method&gt;([&amp;&lt;argument&gt;=&lt;base64-encoded-value&gt;]*)</translation>
</message>
<message>
<source>cliCommandLineSyntax</source>
<translation>linphone &quot;&lt;method&gt; ([&lt;argument&gt;=&lt;value&gt;]*)&quot;</translation>
</message>
<message>
<source>commandsName</source>
<translation>commands list :</translation>
</message>
<message> <message>
<source>showFunctionDescription</source> <source>showFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Shows the main window of the application.</translation>
</message> </message>
<message> <message>
<source>showFunctionCall</source> <source>showCliDescription</source>
<translation type="unfinished"></translation> <translation>&quot;show&quot;</translation>
</message> </message>
<message> <message>
<source>joinConferenceFunctionDescription</source> <source>callFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Initiates a call towards &lt;sip-address&gt;.</translation>
</message>
<message>
<source>callCliDescription</source>
<translation>&quot;call sip-address=&lt;sip-address&gt;&quot;</translation>
</message> </message>
<message> <message>
<source>initiateConferenceFunctionDescription</source> <source>initiateConferenceFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Initiates a conference of id &lt;id&gt;.</translation>
</message>
<message>
<source>initiateConferenceCliDescription</source>
<translation>&quot;initiate-conference sip-address=&lt;my sip-address&gt; conference-id=&lt;id&gt;&quot;</translation>
</message>
<message>
<source>joinConferenceFunctionDescription</source>
<translation>Joins the n°&lt;id&gt; conference hosted by the &lt;host sip-address&gt; as &lt;name&gt;. If you are connected to a proxy config, see join-conference-as.</translation>
</message>
<message>
<source>joinConferenceCliDescription</source>
<translation>&quot;join-conference sip-address=&lt;host sip-address&gt; conference-id=&lt;id&gt; display-name=&lt;name&gt;&quot;</translation>
</message> </message>
<message> <message>
<source>joinConferenceAsFunctionDescription</source> <source>joinConferenceAsFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Joins the n°&lt;id&gt; conference hosted by the &lt;host sip-address&gt; as&lt;my sip-address&gt;. If you are not connected to a proxy-config, see join-conference.</translation>
</message>
<message>
<source>joinConferenceAsCliDescription</source>
<translation>&quot;join-conference-as sip-address=&lt;host sip-address&gt; conference-id=&lt;id&gt; guest-sip-address=&lt;my sip-address&gt;&quot;</translation>
</message> </message>
</context> </context>
<context> <context>
......
...@@ -60,14 +60,6 @@ ...@@ -60,14 +60,6 @@
<source>commandLineOptionConfigArg</source> <source>commandLineOptionConfigArg</source>
<translation>file</translation> <translation>file</translation>
</message> </message>
<message>
<source>commandLineOptionCmd</source>
<translation>éxécuter une ligne de commande</translation>
</message>
<message>
<source>commandLineOptionCmdArg</source>
<translation>ligne de commande</translation>
</message>
<message> <message>
<source>commandLineOptionHelp</source> <source>commandLineOptionHelp</source>
<translation>affiche cette aide</translation> <translation>affiche cette aide</translation>
...@@ -76,6 +68,14 @@ ...@@ -76,6 +68,14 @@
<source>commandLineOptionVersion</source> <source>commandLineOptionVersion</source>
<translation>affiche la version de l&apos;application</translation> <translation>affiche la version de l&apos;application</translation>
</message> </message>
<message>
<source>commandLineCliHelp</source>
<translation>affiche le menu d&apos;aide pour l&apos;utilisation de Linphone en CLI.</translation>
</message>
<message>
<source>commandLineDescription</source>
<translation>envoie un ordre à l&apos;application Linphone, voir --cli-help pour plus de détails.</translation>
</message>
</context> </context>
<context> <context>
<name>AssistantAbstractView</name> <name>AssistantAbstractView</name>
...@@ -413,25 +413,61 @@ Url du serveur non configurée.</translation> ...@@ -413,25 +413,61 @@ Url du serveur non configurée.</translation>
</context> </context>
<context> <context>
<name>Cli</name> <name>Cli</name>
<message>
<source>linphoneCliDescription</source>
<translation>Moyen d&apos;effectuer des actions sur l&apos;application Linphone en ligne de commande.</translation>
</message>
<message>
<source>uriCommandLineSyntax</source>
<translation>linphone sip:&lt;sip-address&gt;?method=&lt;method&gt;([&amp;&lt;argument&gt;=&lt;valeur-en-base64&gt;]*)</translation>
</message>
<message>
<source>cliCommandLineSyntax</source>
<translation>linphone &quot;&lt;method&gt; ([&lt;argument&gt;=&lt;valeur&gt;]*)&quot;</translation>
</message>
<message>
<source>commandsName</source>
<translation>liste des commandes :</translation>
</message>
<message> <message>
<source>showFunctionDescription</source> <source>showFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Affiche la fenêtre principale de l&apos;application.</translation>
</message> </message>
<message> <message>
<source>showFunctionCall</source> <source>showCliDescription</source>
<translation type="unfinished"></translation> <translation>&quot;show&quot;</translation>
</message> </message>
<message> <message>
<source>joinConferenceFunctionDescription</source> <source>callFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Initie un appel vers &lt;sip-address&gt;.</translation>
</message>
<message>
<source>callCliDescription</source>
<translation>&quot;call sip-address=&lt;sip-address&gt;&quot;</translation>
</message> </message>
<message> <message>
<source>initiateConferenceFunctionDescription</source> <source>initiateConferenceFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Initie une conférence d&apos;id &lt;id&gt;.</translation>
</message>
<message>
<source>initiateConferenceCliDescription</source>
<translation>&quot;initiate-conference sip-address=&lt;ma sip-address&gt; conference-id=&lt;id&gt;&quot;</translation>
</message>
<message>
<source>joinConferenceFunctionDescription</source>
<translation>Rejoint la conférence n°&lt;id&gt; hébergée par la &lt;sip-address de l&apos;host&gt; en tant que &lt;nom&gt;. Si vous êtes connecté à une proxy config, voir join-conference-as.</translation>
</message>
<message>
<source>joinConferenceCliDescription</source>
<translation>&quot;join-conference sip-address=&lt;sip-address de l&apos;host&gt; conference-id=&lt;id&gt; display-name=&lt;nom&gt;&quot;</translation>
</message> </message>
<message> <message>
<source>joinConferenceAsFunctionDescription</source> <source>joinConferenceAsFunctionDescription</source>
<translation type="unfinished"></translation> <translation>Rejoint la conférence n°&lt;id&gt; hébergée par la &lt;sip-address de l&apos;host&gt; en tant que &lt;ma sip-address&gt;. Si vous n&apos;êtes pas connecté à une proxy-config, voir join-conference.</translation>
</message>
<message>
<source>joinConferenceAsCliDescription</source>
<translation>&quot;join-conference-as sip-address=&lt;sip-address de l&apos;host&gt; conference-id=&lt;id&gt; guest-sip-address=&lt;ma sip-address&gt;&quot;</translation>
</message> </message>
</context> </context>
<context> <context>
......
...@@ -100,6 +100,12 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U ...@@ -100,6 +100,12 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U
mParser->showHelp(); mParser->showHelp();
} }
if(mParser->isSet("cli-help")){
mCli = new Cli(this);
mCli->showHelp();
::exit(EXIT_SUCCESS);
}
if (mParser->isSet("version")) if (mParser->isSet("version"))
mParser->showVersion(); mParser->showVersion();
...@@ -327,11 +333,12 @@ void App::createParser () { ...@@ -327,11 +333,12 @@ void App::createParser () {
mParser = new QCommandLineParser(); mParser = new QCommandLineParser();
mParser->setApplicationDescription(tr("applicationDescription")); mParser->setApplicationDescription(tr("applicationDescription"));
mParser->addPositionalArgument("command",tr("commandLineDescription"),"[command]");
mParser->addOptions({ mParser->addOptions({
{ { "h", "help" }, tr("commandLineOptionHelp") }, { { "h", "help" }, tr("commandLineOptionHelp") },
{ "cli-help", tr("commandLineCliHelp") },
{ { "v", "version" }, tr("commandLineOptionVersion") }, { { "v", "version" }, tr("commandLineOptionVersion") },
{ "config", tr("commandLineOptionConfig"), tr("commandLineOptionConfigArg") }, { "config", tr("commandLineOptionConfig"), tr("commandLineOptionConfigArg") },
{ { "c", "cmd" }, tr("commandLineOptionCmd"), tr("commandLineOptionCmdArg") },
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
{ "iconified", tr("commandLineOptionIconified") }, { "iconified", tr("commandLineOptionIconified") },
#endif // ifndef Q_OS_MACOS #endif // ifndef Q_OS_MACOS
......
...@@ -137,12 +137,14 @@ static void cliInitiateConference (QHash<QString, QString> &args) { ...@@ -137,12 +137,14 @@ static void cliInitiateConference (QHash<QString, QString> &args) {
Cli::Command::Command ( Cli::Command::Command (
const QString &functionName, const QString &functionName,
const QString &description, const QString &functionDescription,
const QString &cliDescription,
Cli::Function function, Cli::Function function,
const QHash<QString, Cli::Argument> &argsScheme const QHash<QString, Cli::Argument> &argsScheme
) : ) :
mFunctionName(functionName), mFunctionName(functionName),
mDescription(description), mFunctionDescription(functionDescription),
mCliDescription(cliDescription),
mFunction(function), mFunction(function),
mArgsScheme(argsScheme) {} mArgsScheme(argsScheme) {}
...@@ -200,17 +202,17 @@ QRegExp Cli::mRegExpArgs("(?:(?:([\\w-]+)\\s*)=\\s*(?:\"([^\"\\\\]*(?:\\\\.[^\"\ ...@@ -200,17 +202,17 @@ QRegExp Cli::mRegExpArgs("(?:(?:([\\w-]+)\\s*)=\\s*(?:\"([^\"\\\\]*(?:\\\\.[^\"\
QRegExp Cli::mRegExpFunctionName("^\\s*([a-z-]+)\\s*"); QRegExp Cli::mRegExpFunctionName("^\\s*([a-z-]+)\\s*");
Cli::Cli (QObject *parent) : QObject(parent) { Cli::Cli (QObject *parent) : QObject(parent) {
addCommand("show", tr("showFunctionDescription"), ::cliShow); addCommand("show", tr("showFunctionDescription"), tr("showCliDescription"), ::cliShow);
addCommand("call", tr("showFunctionCall"), ::cliCall, { addCommand("call", tr("callFunctionDescription"), tr("callCliDescription"), ::cliCall, {
{ "sip-address", {} } { "sip-address", {} }
}); });
addCommand("initiate-conference", tr("initiateConferenceFunctionDescription"), ::cliInitiateConference, { addCommand("initiate-conference", tr("initiateConferenceFunctionDescription"), tr("initiateConferenceCliDescription"), ::cliInitiateConference, {
{ "sip-address", {} }, { "conference-id", {} } { "sip-address", {} }, { "conference-id", {} }
}); });
addCommand("join-conference", tr("joinConferenceFunctionDescription"), ::cliJoinConference, { addCommand("join-conference", tr("joinConferenceFunctionDescription"), tr("joinConferenceCliDescription"), ::cliJoinConference, {
{ "sip-address", {} }, { "conference-id", {} }, { "display-name", {} } { "sip-address", {} }, { "conference-id", {} }, { "display-name", {} }
}); });
addCommand("join-conference-as", tr("joinConferenceAsFunctionDescription"), ::cliJoinConferenceAs, { addCommand("join-conference-as", tr("joinConferenceAsFunctionDescription"), tr("joinConferenceAsCliDescription"), ::cliJoinConferenceAs, {
{ "sip-address", {} }, { "conference-id", {} }, { "guest-sip-address", {} } { "sip-address", {} }, { "conference-id", {} }, { "guest-sip-address", {} }
}); });
} }
...@@ -219,14 +221,22 @@ Cli::Cli (QObject *parent) : QObject(parent) { ...@@ -219,14 +221,22 @@ Cli::Cli (QObject *parent) : QObject(parent) {
void Cli::addCommand ( void Cli::addCommand (
const QString &functionName, const QString &functionName,
const QString &description, const QString &functionDescription,
const QString &cliDescription,
Function function, Function function,
const QHash<QString, Argument> &argsScheme const QHash<QString, Argument> &argsScheme
) { ) {
if (mCommands.contains(functionName)) if (mCommands.contains(functionName)) {
qWarning() << QStringLiteral("Command already exists: `%1`.").arg(functionName); qWarning() << QStringLiteral("Command already exists: `%1`.").arg(functionName);
else return;
mCommands[functionName] = Cli::Command(functionName, description, function, argsScheme); }
mCommands[functionName] = Cli::Command(
functionName,
functionDescription,
cliDescription,
function,
argsScheme
);
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
...@@ -274,6 +284,104 @@ void Cli::executeCommand (const QString &command, CommandFormat *format) const { ...@@ -274,6 +284,104 @@ void Cli::executeCommand (const QString &command, CommandFormat *format) const {
mCommands[functionName].executeUri(address); mCommands[functionName].executeUri(address);
} }
QString splitWord(QString word, int &curPos, const int lineLength, const QString &padding) {
QString out;
out += word.mid(0,lineLength - curPos) + "\n" + padding;
curPos = padding.length();
word = word.mid(lineLength - curPos);
while(word.length() > lineLength - curPos) {
out += word.mid(0,lineLength - curPos);
word = word.mid(lineLength - curPos);
out += "\n" + padding;
}
out += word;
curPos = word.length() + padding.length();
return out;
}
QString indentedWord(QString word, int &curPos, const int lineLength, const QString &padding){
QString out;
if (curPos + word.length() > lineLength ) {
if (padding.length() + word.length() > lineLength) {
out += splitWord(word, curPos, lineLength, padding);
} else {
out += QStringLiteral("\n");
out += padding + word;
curPos = padding.length();
}
} else {
out += word;
curPos += word.length();
}
return out;
}
static string multilineIndent (const QString &str, int indentationNumber = 0) {
static const int lineLength(80);
static const QRegExp spaceRegexp("(\\s)");
const QString padding(indentationNumber * 2, ' ');
QString out = padding;
int indentedTextCurPos = padding.length();
int spacePos = 0;
int wordPos = spacePos;
QString word;
while ((spacePos = spaceRegexp.indexIn(str, spacePos)) != -1) {
word = str.mid(wordPos,spacePos-wordPos);
out += indentedWord(word, indentedTextCurPos, lineLength, padding);
switch (str[spacePos].unicode()) {
case '\n':
out += "\n" + padding;
indentedTextCurPos = padding.length();
break;
case '\t': //tabulated as a space
case ' ':
if (indentedTextCurPos == lineLength) {
out += "\n" + padding;
indentedTextCurPos = padding.length();
} else {
out += " ";
indentedTextCurPos += 1;
}
break;
case '\r':
case '\f':
break;
default :
break;
}
spacePos += 1;
wordPos = spacePos;
}
word = str.mid(wordPos);
out += indentedWord(word, indentedTextCurPos,lineLength, padding);
out += "\n";
return ::Utils::appStringToCoreString(out);
}
void Cli::showHelp() {
cout << multilineIndent(tr("linphoneCliDescription"),0);
cout << "\n" << "Usage : " << "\n";
cout << multilineIndent(tr("uriCommandLineSyntax"),0);
cout <<multilineIndent(tr("cliCommandLineSyntax"),0);
cout << "\n";
cout << multilineIndent(tr("commandsName"));
cout << "\n";
for (const auto &method : mCommands.keys()) {
cout << multilineIndent(mCommands[method].getCliDescription(),1);
cout << multilineIndent(mCommands[method].getFunctionDescription(),2);
cout << "\n";
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
QString Cli::parseFunctionName (const QString &command) const { QString Cli::parseFunctionName (const QString &command) const {
......
...@@ -58,7 +58,8 @@ class Cli : public QObject { ...@@ -58,7 +58,8 @@ class Cli : public QObject {
Command () = default; Command () = default;
Command ( Command (
const QString &functionName, const QString &functionName,
const QString &description, const QString &functionDescription,
const QString &cliDescription,
Function function, Function function,
const QHash<QString, Argument> &argsScheme const QHash<QString, Argument> &argsScheme
); );
...@@ -66,9 +67,19 @@ class Cli : public QObject { ...@@ -66,9 +67,19 @@ class Cli : public QObject {
void execute (QHash<QString, QString> &args) const; void execute (QHash<QString, QString> &args) const;
void executeUri (const std::shared_ptr<linphone::Address> &address) const; void executeUri (const std::shared_ptr<linphone::Address> &address) const;
QString getFunctionDescription() {
return mFunctionDescription;
}
QString getCliDescription() {
return mCliDescription;
}
private: private:
QString mFunctionDescription;
QString mCliDescription;
QString mFunctionName; QString mFunctionName;
QString mDescription;
Function mFunction = nullptr; Function mFunction = nullptr;
QHash<QString, Argument> mArgsScheme; QHash<QString, Argument> mArgsScheme;
}; };
...@@ -85,10 +96,13 @@ public: ...@@ -85,10 +96,13 @@ public:
void executeCommand (const QString &command, CommandFormat *format = nullptr) const; void executeCommand (const QString &command, CommandFormat *format = nullptr) const;
void showHelp();
private: private:
void addCommand ( void addCommand (
const QString &functionName, const QString &functionName,
const QString &description, const QString &functionDescription,
const QString &cliDescription,
Function function, Function function,
const QHash<QString, Argument> &argsScheme = QHash<QString, Argument>() const QHash<QString, Argument> &argsScheme = QHash<QString, Argument>()
); );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment