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 @@
<source>commandLineOptionConfigArg</source>
<translation>file</translation>
</message>
<message>
<source>commandLineOptionCmd</source>
<translation>run a command line</translation>
</message>
<message>
<source>commandLineOptionCmdArg</source>
<translation>command line</translation>
</message>
<message>
<source>commandLineOptionHelp</source>
<translation>show this help</translation>
......@@ -76,6 +68,14 @@
<source>commandLineOptionVersion</source>
<translation>show app version</translation>
</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>
<name>AssistantAbstractView</name>
......@@ -413,25 +413,61 @@ Server url not configured.</translation>
</context>
<context>
<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>
<source>showFunctionDescription</source>
<translation type="unfinished"></translation>
<translation>Shows the main window of the application.</translation>
</message>
<message>
<source>showFunctionCall</source>
<translation type="unfinished"></translation>
<source>showCliDescription</source>
<translation>&quot;show&quot;</translation>
</message>
<message>
<source>joinConferenceFunctionDescription</source>
<translation type="unfinished"></translation>
<source>callFunctionDescription</source>
<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>
<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>
<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>
</context>
<context>
......
......@@ -60,14 +60,6 @@
<source>commandLineOptionConfigArg</source>
<translation>file</translation>
</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>
<source>commandLineOptionHelp</source>
<translation>affiche cette aide</translation>
......@@ -76,6 +68,14 @@
<source>commandLineOptionVersion</source>
<translation>affiche la version de l&apos;application</translation>
</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>
<name>AssistantAbstractView</name>
......@@ -413,25 +413,61 @@ Url du serveur non configurée.</translation>
</context>
<context>
<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>
<source>showFunctionDescription</source>
<translation type="unfinished"></translation>
<translation>Affiche la fenêtre principale de l&apos;application.</translation>
</message>
<message>
<source>showFunctionCall</source>
<translation type="unfinished"></translation>
<source>showCliDescription</source>
<translation>&quot;show&quot;</translation>
</message>
<message>
<source>joinConferenceFunctionDescription</source>
<translation type="unfinished"></translation>
<source>callFunctionDescription</source>
<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>
<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>
<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>
</context>
<context>
......
......@@ -100,6 +100,12 @@ App::App (int &argc, char *argv[]) : SingleApplication(argc, argv, true, Mode::U
mParser->showHelp();
}
if(mParser->isSet("cli-help")){
mCli = new Cli(this);
mCli->showHelp();
::exit(EXIT_SUCCESS);
}
if (mParser->isSet("version"))
mParser->showVersion();
......@@ -327,11 +333,12 @@ void App::createParser () {
mParser = new QCommandLineParser();
mParser->setApplicationDescription(tr("applicationDescription"));
mParser->addPositionalArgument("command",tr("commandLineDescription"),"[command]");
mParser->addOptions({
{ { "h", "help" }, tr("commandLineOptionHelp") },
{ "cli-help", tr("commandLineCliHelp") },
{ { "v", "version" }, tr("commandLineOptionVersion") },
{ "config", tr("commandLineOptionConfig"), tr("commandLineOptionConfigArg") },
{ { "c", "cmd" }, tr("commandLineOptionCmd"), tr("commandLineOptionCmdArg") },
#ifndef Q_OS_MACOS
{ "iconified", tr("commandLineOptionIconified") },
#endif // ifndef Q_OS_MACOS
......
......@@ -137,12 +137,14 @@ static void cliInitiateConference (QHash<QString, QString> &args) {
Cli::Command::Command (
const QString &functionName,
const QString &description,
const QString &functionDescription,
const QString &cliDescription,
Cli::Function function,
const QHash<QString, Cli::Argument> &argsScheme
) :
mFunctionName(functionName),
mDescription(description),
mFunctionDescription(functionDescription),
mCliDescription(cliDescription),
mFunction(function),
mArgsScheme(argsScheme) {}
......@@ -200,17 +202,17 @@ QRegExp Cli::mRegExpArgs("(?:(?:([\\w-]+)\\s*)=\\s*(?:\"([^\"\\\\]*(?:\\\\.[^\"\
QRegExp Cli::mRegExpFunctionName("^\\s*([a-z-]+)\\s*");
Cli::Cli (QObject *parent) : QObject(parent) {
addCommand("show", tr("showFunctionDescription"), ::cliShow);
addCommand("call", tr("showFunctionCall"), ::cliCall, {
addCommand("show", tr("showFunctionDescription"), tr("showCliDescription"), ::cliShow);
addCommand("call", tr("callFunctionDescription"), tr("callCliDescription"), ::cliCall, {
{ "sip-address", {} }
});
addCommand("initiate-conference", tr("initiateConferenceFunctionDescription"), ::cliInitiateConference, {
addCommand("initiate-conference", tr("initiateConferenceFunctionDescription"), tr("initiateConferenceCliDescription"), ::cliInitiateConference, {
{ "sip-address", {} }, { "conference-id", {} }
});
addCommand("join-conference", tr("joinConferenceFunctionDescription"), ::cliJoinConference, {
addCommand("join-conference", tr("joinConferenceFunctionDescription"), tr("joinConferenceCliDescription"), ::cliJoinConference, {
{ "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", {} }
});
}
......@@ -219,14 +221,22 @@ Cli::Cli (QObject *parent) : QObject(parent) {
void Cli::addCommand (
const QString &functionName,
const QString &description,
const QString &functionDescription,
const QString &cliDescription,
Function function,
const QHash<QString, Argument> &argsScheme
) {
if (mCommands.contains(functionName))
if (mCommands.contains(functionName)) {
qWarning() << QStringLiteral("Command already exists: `%1`.").arg(functionName);
else
mCommands[functionName] = Cli::Command(functionName, description, function, argsScheme);
return;
}
mCommands[functionName] = Cli::Command(
functionName,
functionDescription,
cliDescription,
function,
argsScheme
);
}
// -----------------------------------------------------------------------------
......@@ -274,6 +284,104 @@ void Cli::executeCommand (const QString &command, CommandFormat *format) const {
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 {
......
......@@ -58,7 +58,8 @@ class Cli : public QObject {
Command () = default;
Command (
const QString &functionName,
const QString &description,
const QString &functionDescription,
const QString &cliDescription,
Function function,
const QHash<QString, Argument> &argsScheme
);
......@@ -66,9 +67,19 @@ class Cli : public QObject {
void execute (QHash<QString, QString> &args) const;
void executeUri (const std::shared_ptr<linphone::Address> &address) const;
QString getFunctionDescription() {
return mFunctionDescription;
}
QString getCliDescription() {
return mCliDescription;
}
private:
QString mFunctionDescription;
QString mCliDescription;
QString mFunctionName;
QString mDescription;
Function mFunction = nullptr;
QHash<QString, Argument> mArgsScheme;
};
......@@ -85,10 +96,13 @@ public:
void executeCommand (const QString &command, CommandFormat *format = nullptr) const;
void showHelp();
private:
void addCommand (
const QString &functionName,
const QString &description,
const QString &functionDescription,
const QString &cliDescription,
Function function,
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