improve parsing argv
This commit is contained in:
@@ -49,20 +49,16 @@ bool ConnectToMPD()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ParseArgv(int argc, char **argv)
|
void ParseArgv(int argc, char **argv)
|
||||||
{
|
{
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
|
|
||||||
vector<string> v;
|
bool quit = 0;
|
||||||
v.reserve(argc-1);
|
|
||||||
for (int i = 1; i < argc; i++)
|
|
||||||
v.push_back(argv[i]);
|
|
||||||
|
|
||||||
bool exit = 0;
|
for (int i = 1; i < argc; i++)
|
||||||
for (vector<string>::iterator it = v.begin(); it != v.end() && !exit; it++)
|
|
||||||
{
|
{
|
||||||
if (*it == "-v" || *it == "--version")
|
if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
|
||||||
{
|
{
|
||||||
cout << "ncmpcpp version: " << VERSION << endl
|
cout << "ncmpcpp version: " << VERSION << endl
|
||||||
<< "built with support for:"
|
<< "built with support for:"
|
||||||
@@ -76,9 +72,9 @@ bool ParseArgv(int argc, char **argv)
|
|||||||
<< " unicode"
|
<< " unicode"
|
||||||
# endif
|
# endif
|
||||||
<< endl;
|
<< endl;
|
||||||
return 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
else if (*it == "-?" || *it == "--help")
|
else if (strcmp(argv[i], "-?") == 0 || strcmp(argv[i], "--help") == 0)
|
||||||
{
|
{
|
||||||
cout
|
cout
|
||||||
<< "Usage: ncmpcpp [OPTION]...\n"
|
<< "Usage: ncmpcpp [OPTION]...\n"
|
||||||
@@ -92,67 +88,68 @@ bool ParseArgv(int argc, char **argv)
|
|||||||
<< " prev play the previous song\n"
|
<< " prev play the previous song\n"
|
||||||
<< " volume [+-]<num> adjusts volume by [+-]<num>\n"
|
<< " volume [+-]<num> adjusts volume by [+-]<num>\n"
|
||||||
;
|
;
|
||||||
return 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ConnectToMPD())
|
if (!ConnectToMPD())
|
||||||
return 1;
|
exit(0);
|
||||||
|
|
||||||
if (*it == "play")
|
if (strcmp(argv[i], "play") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Play();
|
Mpd->Play();
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "pause")
|
else if (strcmp(argv[i], "pause") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Execute("pause \"1\"\n");
|
Mpd->Execute("pause \"1\"\n");
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "toggle")
|
else if (strcmp(argv[i], "toggle") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Execute("pause\n");
|
Mpd->Execute("pause\n");
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "stop")
|
else if (strcmp(argv[i], "stop") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Stop();
|
Mpd->Stop();
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "next")
|
else if (strcmp(argv[i], "next") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Next();
|
Mpd->Next();
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "prev")
|
else if (strcmp(argv[i], "prev") == 0)
|
||||||
{
|
{
|
||||||
Mpd->Prev();
|
Mpd->Prev();
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else if (*it == "volume")
|
else if (strcmp(argv[i], "volume") == 0)
|
||||||
{
|
{
|
||||||
it++;
|
i++;
|
||||||
Mpd->UpdateStatus();
|
Mpd->UpdateStatus();
|
||||||
if (!Mpd->GetErrorMessage().empty())
|
if (!Mpd->GetErrorMessage().empty())
|
||||||
{
|
{
|
||||||
cout << "Error: " << Mpd->GetErrorMessage() << endl;
|
cout << "Error: " << Mpd->GetErrorMessage() << endl;
|
||||||
return 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
if (it != v.end())
|
if (i != argc)
|
||||||
Mpd->SetVolume(Mpd->GetVolume()+StrToInt(*it));
|
Mpd->SetVolume(Mpd->GetVolume()+atoi(argv[i]));
|
||||||
exit = 1;
|
quit = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "ncmpcpp: invalid option " << *it << endl;
|
cout << "ncmpcpp: invalid option: " << argv[i] << endl;
|
||||||
return 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
if (!Mpd->GetErrorMessage().empty())
|
if (!Mpd->GetErrorMessage().empty())
|
||||||
{
|
{
|
||||||
cout << "Error: " << Mpd->GetErrorMessage() << endl;
|
cout << "Error: " << Mpd->GetErrorMessage() << endl;
|
||||||
return 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return exit;
|
if (quit)
|
||||||
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CaseInsensitiveSorting::operator()(string a, string b)
|
bool CaseInsensitiveSorting::operator()(string a, string b)
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
|
||||||
bool ConnectToMPD();
|
bool ConnectToMPD();
|
||||||
bool ParseArgv(int, char **);
|
void ParseArgv(int, char **);
|
||||||
|
|
||||||
class CaseInsensitiveSorting
|
class CaseInsensitiveSorting
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -186,11 +186,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (argc > 1)
|
if (argc > 1)
|
||||||
{
|
{
|
||||||
if (ParseArgv(argc, argv))
|
ParseArgv(argc, argv);
|
||||||
{
|
|
||||||
Mpd->Disconnect();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ConnectToMPD())
|
if (!ConnectToMPD())
|
||||||
|
|||||||
Reference in New Issue
Block a user