Command line argument parsing with getopt

This commit is contained in:
Ashik K 2023-03-26 09:58:15 +02:00
parent dc989c1892
commit 3835392444
1 changed files with 22 additions and 2 deletions

View File

@ -2,8 +2,11 @@
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>
@ -13,13 +16,30 @@
int main(int argc, char *argv[])
{
if (argc !=3 ) {
std::string exbid, apikey;
if (argc !=5 ) {
std::cout<<"Please run as "<<argv[0] <<" <exhibition id> <api key>"<<std::endl;
exit(0);
}
int c;
while ((c = getopt (argc, argv, "e:k:")) != -1)
switch(c)
{
case 'e':
exbid = optarg;
break;
case 'k':
apikey = optarg;
break;
default:
break;
}
char url[256], outfilename[64];
for (int i = 0; i<4; i++) {
snprintf(url, sizeof url, "https://api.dimu.org/api/solr/select?q=Kosta&wt=json&fq=(artifact.exhibitionUids:\"%s\")&start=%d&rows=100&api.key=%s", argv[1], i*100, argv[2]);
snprintf(url, sizeof url, "https://api.dimu.org/api/solr/select?q=Kosta&wt=json&fq=(artifact.exhibitionUids:\"%s\")&start=%d&rows=100&api.key=%s", exbid.c_str(), i*100, apikey.c_str());
std::cout<<url<<"\n";
snprintf(outfilename, sizeof outfilename, "data_%d.json", i);
/*if (DEBUG_LEVEL > 0) */std::cout<<"url to fetch is "<<std::endl<<url<<std::endl;