Use getopts for argument parsing

This commit is contained in:
Jaidyn Ann 2020-07-03 19:13:41 -05:00
parent 4d4e6bad78
commit 52908aef68
8 changed files with 144 additions and 10 deletions

View File

@ -32,6 +32,7 @@ SRCS = \
src/Channel.cpp, \
src/Item.cpp, \
src/ProtocolListener.cpp, \
src/Config.cpp, \
src/webfetch.cpp, \
src/parsing.cpp, \
src/Rifen.cpp

View File

@ -2,6 +2,7 @@
#include <raptor2/raptor2.h>
#include "Channel.h"
#include "Item.h"
#include "Config.h"
#include "parsing.h"
Channel::Channel ( BString path, BString outputPath )
@ -17,7 +18,7 @@ Channel::Channel ( BString path, BString outputPath )
}
void
Channel::Parse ( )
Channel::Parse ( Config* cfg )
{
int itemCount = countItemParser( filePath.String() );
items = BList(itemCount);

View File

@ -5,6 +5,7 @@
#include <String.h>
#include <List.h>
#include <Url.h>
#include "Config.h"
class Channel {
public:
@ -25,7 +26,7 @@ public:
Channel ( BString, BString );
// Channel ( BEntry );
// Channel ( BUrl );
void Parse ( void );
void Parse ( Config* );
};
#endif

9
src/Config.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <String.h>
#include "Config.h"
Config::Config () {
verbose = false;
daemon = true;
mimetype = BString("text/xml");
outDir = BString("/boot/home/feeds/");
}

17
src/Config.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef CONFIG_H
#define CONFIG_H
#include <StorageKit.h>
class Config {
public:
bool verbose;
bool daemon;
BString mimetype;
BString outDir;
BString targetFeed; // file or url
Config ( );
};
#endif

View File

@ -1,26 +1,93 @@
#include <raptor2/raptor2.h>
#include <StorageKit.h>
#include <String.h>
#include <getopt.h>
#include "Channel.h"
#include "Item.h"
#include "parsing.h" //
#include "parsing.h"
#include "Config.h"
#include "Rifen.h"
int
usage ()
{
fprintf(stderr, "%s", usageMsg.String());
return 2;
}
bool
create_item ( void* item )
{
printf("hi");
Item* itemPtr = (Item*)item;
itemPtr->Filetize( false );
return false;
}
int
invocation ( int argc, char** argv, Config** cfgPtr )
{
Config* cfg = *(cfgPtr);
bool suicide = false;
static struct option sLongOptions[] = {
{ "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' },
{ "output", required_argument, 0, 'O' },
{ "mimetype", required_argument, 0, 'm' },
{ "foreground", no_argument, 0, 'D' },
{ 0, 0, 0, 0 }
};
while (true) {
opterr = 0;
int c = getopt_long(argc, argv, "+hvDm:O:", sLongOptions, NULL);
switch (c) {
case -1:
if ( optind < argc )
cfg->targetFeed = BString(argv[optind]);
return 0;
case 'h':
return usage();
case 'm':
cfg->mimetype = BString(optarg);
break;
case 'O':
cfg->outDir = BString(optarg);
break;
case 'v':
cfg->verbose = true;
break;
case 'D':
cfg->daemon = false;
break;
case '?':
if ( optopt == 'O' || optopt == 'm' )
fprintf( stderr, "Option `-%c` requires an argument.\n\n", optopt );
else
fprintf( stderr, "Unknown option `-%c`.\n\n", optopt );
return 2;
}
}
return 0;
}
int
main ( int argc, char** argv )
{
BString outputDir("/boot/home/feeds/");
Channel* chan = (Channel*)malloc( sizeof(Channel) );
//
//
Config* cfg = new Config;
usageMsg.ReplaceAll("%app%", "Rifen");
chan = new Channel(argv[1], outputDir);
chan->Parse();
invocation( argc, argv, &cfg );
Channel* chan = (Channel*)malloc( sizeof(Channel) );
chan = new Channel(cfg->targetFeed, cfg->outDir);
chan->Parse(cfg);
BList items = chan->items;
items.DoForEach(&create_item);

41
src/Rifen.h Normal file
View File

@ -0,0 +1,41 @@
#include <StorageKit.h>
int main ( int, char** );
int invocation ( int, char**, Config** );
int usage ( );
bool create_item ( void* );
// ----------------------------------------------------------------------------
BString usageMsg =
"Usage: %app% [-hvD] [-m mimetype] [-tT datetime] [-O directory] \n"
" %app% [-hv] [-mtTO] ( <text/xml file> | <META:url file> | <url> )\n"
"\n"
"%app%, a RSS and Atom feed parser/daemon.\n"
"\n"
"Options:\n"
" -h, --help - Print this usage info.\n"
" -v, --verbose - Print verbose (debug) info.\n"
" -m, --mimetype - Mimetype of new item files. (Default: text/xml)\n"
" -O, --output - Output dir for item files. (Default: ~/feeds/)\n"
" -t, --before - Only return items published before this datetime.\n"
" -T, --after - Only return items published after this datetime.\n"
" -D, --foreground - Run in the foreground, do not daemonize.\n"
" Only applies when running without file/url arg.\n"
"\n"
"When invoked without a file or URL, will search for any new feed items\n"
"published since last check by by any 'feed file' placed in the config\n"
"directory (default: ~/config/settings/Rifen/feeds/) and create their\n"
"corresponding files.\n"
"\n"
"When invoked with a file or URL, will create files from items contained\n"
"within the given file or URL.\n"
"\n"
"'Feed files' are files with a 'META:url' attribute containing the\n"
"corresponding URL to the feed's XML.\n"
"\n"
"Both -t and -T use the ISO 8601 format for specifying datetimes:\n"
" YYYY-MM-DDTHH:MM:SS - 2020-01-01T07:07:07\n"
"You can leave out seconds, minutes, or hours, but YMD are required.\n"
;

View File

@ -5,9 +5,6 @@
#include "Item.h"
#include "parsing.h"
/* predicate == sweet http version of tag
subject == parent
object == data */
// ============================================================================
// PARSERS