instrumentation/hrktorrent: draft implementation of getopt_long-based argument parsing
authorP2P-Next <p2p-next@cs.pub.ro>
Mon, 9 Aug 2010 18:43:14 +0000 (21:43 +0300)
committerP2P-Next <p2p-next@cs.pub.ro>
Mon, 9 Aug 2010 18:43:14 +0000 (21:43 +0300)
instrumentation/hrktorrent/main.cpp

index a6ab88b..4fcf246 100644 (file)
@@ -1,27 +1,37 @@
 #include "hrktorrent.h"
 #include <getopt.h>
+
 void
-printusage()
+printversion()
 {
        std::cout << APPNAME << " " << VERSION << " by hrkfdn (hrkfdn A T gmail D O T com) and other contributors\n";
+}
+
+void
+printusage(const char *argv0)
+{
+       printversion();
        std::cout << "usage: " << APPNAME << " [options] torrent-file\n";
        std::cout << "NOTE: options provided by the commandline override configuration file entries!\n\n";
 
-       std::cout << "\t--minport\t\t- start port range (default: 6881)\n";
-       std::cout << "\t--maxport\t\t- end port range (default: 6999)\n";
+       std::cout << "\t--minport <port>\t- start port range (default: 6881)\n";
+       std::cout << "\t--maxport <port>\t- end port range (default: 6999)\n";
 
-       std::cout << "\t--maxdown\t\t- download speed limit (in kb/s) (default: unlimited)\n";
-       std::cout << "\t--maxup\t\t\t- upload speed limit (in kb/s) (default: unlimited)\n";
+       std::cout << "\t--maxdown <speed>\t- download speed limit (in kb/s) (default: unlimited)\n";
+       std::cout << "\t--maxup <speed>\t\t- upload speed limit (in kb/s) (default: unlimited)\n";
 
+       std::cout << "\t--limitlocal\t\t-limit local network (default: off)\n";
        std::cout << "\t--nodht\t\t\t- disable dht (default: on)\n";
        std::cout << "\t--noupnp\t\t\t- disable upnp (default: on)\n";
        std::cout << "\t--noseed\t\t- disable seeding (default: on)\n";
        std::cout << "\t--forcereannounce\t- reannounce every X minutes (default: 2)\n";
-       std::cout << "\t--downloaddir\t- set the target directory (default: .)\n";
-       std::cout << "\t--ipfilter\t\t- enable ip filtering (default: off)\n\n";
-       std::cout << "\t--verbose\t\t- print verbose messages (default: off)\n\n";
+       std::cout << "\t--outputdir\t\t- set the target directory (default: .)\n";
+       std::cout << "\t--ipfilter\t\t- enable ip filtering (default: off)\n";
+       std::cout << "\t--verbose\t\t- print verbose messages (default: off)\n";
+       std::cout << "\t--version\t\t- print version\n";
+       std::cout << "\t--help\t\t\t- print this help screen\n\n";
 
-       std::cout << "example: hrktorrent --minport6500 --maxport6600 --nodht file.torrent\n";
+       std::cout << "example: " << argv0 << " --minport 6500 --maxport 6600 --maxup 50 --maxdown 60 --limitlocal --nodht file.torrent\n";
 
        std::cout << std::endl;
 }
@@ -47,117 +57,69 @@ parseargs(int argc, char** argv)
                {"maxup", 1, NULL, 'u'},
                {"forcereannounce", 1, NULL, 'f'},
                {"outputdir", 1, NULL, 'o'},
-               {0,0,0,0}
+               {NULL, 0, NULL, 0}
        };
 
        while (1) {
-               opt = getopt_long(argc, argv, "hnsiv:p:P:M:u:f:d:",long_options, &option_index);
+               opt = getopt_long(argc, argv, "hiltnsvVp:P:d:u:f:o:",
+                               long_options, &option_index);
                if (opt == -1)
                        break;
 
-               switch(opt)
-               {
-                       case 1:
-                               /* If this option set a flag, do nothing else now. */
-                               if (long_options[option_index].flag != 0)
-                                       break;
-                               //long_opt = long_options[option_index].name;
-                               printf ("option %s", long_options[option_index].name);
-                               if (optarg)
-                                       printf (" with arg %s", optarg);
-                               printf ("\n");
-                               break;
-                       case 'p':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               break;
-                       case 'P':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               break;
-                       case 'M':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               break;
-                       case 'u':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               break;
-                       case 'h':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               Settings->Set("nodht", 1);
-                               break;
-                       case 'n':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               Settings->Set("noupnp", 1);
-                               break;
-                       case 's':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               Settings->Set("noseed", 1);
-                               break;
-                       case 'd':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               break;
-                       case 'i':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               Settings->Set("ipfilter", 1);
-                               break;
-                       case 'v':
-                               std::cout << opt << " "<< optarg<<std::endl;
-                               Settings->Set("verbose", 1);
-                               break;
-                       default:
-                               std::cout <<"wtf"<<" "<<opt<<std::endl;
-
+               switch(opt) {
+               case 'h':
+                       printusage(argv[0]);
+                       exit(EXIT_FAILURE);
+                       break;
+               case 'i':
+                       Settings->Set("ipfilter", 1);
+                       break;
+               case 'l':
+                       Settings->Set("limitlocal", 1);
+                       break;
+               case 't':
+                       Settings->Set("nodht", 1);
+                       break;
+               case 'n':
+                       Settings->Set("noupnp", 1);
+                       break;
+               case 's':
+                       Settings->Set("noseed", 1);
+                       break;
+               case 'v':
+                       Settings->Set("verbose", 1);
+                       break;
+               case 'V':
+                       printversion();
+                       exit(EXIT_SUCCESS);
+                       break;
+               case 'p':
+                       Settings->Set("minport", atoi(optarg));
+                       break;
+               case 'P':
+                       Settings->Set("maxport", atoi(optarg));
+                       break;
+               case 'd':
+                       Settings->Set("maxdown", atoi(optarg));
+                       break;
+               case 'u':
+                       Settings->Set("maxup", atoi(optarg));
+                       break;
+               case 'f':
+                       Settings->Set("forcereannounce", atoi(optarg));
+                       break;
+               case 'o':
+                       Settings->Set("outputdir", optarg);
+                       break;
+               default:
+                       printusage(argv[0]);
+                       exit(EXIT_FAILURE);
                }
 
        }
 
-}      
-/*
-void
-parseargs(int argc, char** argv)
-{
-       for(unsigned int i = 1; i < argc; i++) {
-               char* parg = argv[i];
-               if(strstr(parg, "--noseed")) {
-                       Settings->Set("seed", 0);
-                       continue;
-               }
-               else if(strstr(parg, "--nodht")) {
-                       Settings->Set("dht", 0);
-                       continue;
-               }
-               else if(strstr(parg, "--noupnp")) {
-                       Settings->Set("upnp", 0);
-                       continue;
-               }
-               else if(strstr(parg, "--verbose")) {
-                       Settings->Set("verbose", 1);
-                       continue;
-               }
-               else if(strstr(parg, "--ipfilter")) {
-                       Settings->Set("ipfilter", 1);
-                       continue;
-               }
-       
-
-               parg += 2;
-
-               std::map<std::string, setting_t>::iterator it;
-               for(it = Settings->getSettingMap()->begin(); it != Settings->getSettingMap()->end(); it++) {
-                       if(strstr(parg, it->first.c_str())) {
-                               char* val = parg + strlen(it->first.c_str());
-                               switch(it->second.type) {
-                                       case TYPE_INT:
-                                               it->second.ival = atoi(val);
-                                               break;
-                                       case TYPE_STRING:
-                                               it->second.sval = val;
-                                               break;
-                               }
-                               std::cout << it->first.c_str()<< "=(" << it->second.ival << ", "<<it->second.sval <<")" << std::endl;
-                       }
-               }
-       }
 }
-*/
+
 #define CLEAN(x) if(x) delete x
 void
 close()
@@ -178,11 +140,11 @@ main(int argc, char* argv[])
        int runstate = EXIT_FAILURE;
 
        if(argc < 2) {
-               printusage();
+               printusage(argv[0]);
                return EXIT_FAILURE;
        }
 
-//     Core = new CCore(argc, argv);
+       Core = new CCore(argc, argv);
        parseargs(argc, argv);
 //     runstate = Core->Run();