summaryrefslogtreecommitdiff
path: root/src/argparser.cpp
blob: 7828242f0fbdc3919c9965100d0ae5e8546809b0 (plain)
    1 ////////////////////////////////////////////////////////////////////////
    2 // FILE:        argparser.cpp
    3 // AUTHOR:      Johannes Winkelmann, jw@tks6.net
    4 // COPYRIGHT:   (c) 2002 by Johannes Winkelmann
    5 // ---------------------------------------------------------------------
    6 //  This program is free software; you can redistribute it and/or modify
    7 //  it under the terms of the GNU General Public License as published by
    8 //  the Free Software Foundation; either version 2 of the License, or
    9 //  (at your option) any later version.
   10 ////////////////////////////////////////////////////////////////////////
   11 
   12 using namespace std;
   13 
   14 #include "argparser.h"
   15 
   16 /*!
   17   Construct a ArgParser object
   18   \param argc argument count
   19   \param argv argument vector
   20 */
   21 ArgParser::ArgParser( int argc, char** argv )
   22     : m_isCommandGiven( false ),
   23       m_isForced( false ),
   24       m_isTest( false ),
   25       m_isAlternateConfigGiven( false ),
   26       m_useCache( false ),
   27       m_calledAsPrtCache( false ),
   28       m_hasFilter( false ),
   29       m_noStdConfig( false ),
   30       m_writeLog( false ),
   31       m_nodeps( false ),
   32       m_all( false ),
   33       m_printPath( false ),
   34       m_execPreInstall( false ),
   35       m_execPostInstall( false ),
   36       m_preferHigher( false ),
   37       m_strictDiff( false ),
   38       m_useRegex(false),
   39       m_fullPath(false),
   40       m_recursive(false),
   41       m_printTree(false),
   42       m_depSort(false),
   43       m_alternateConfigFile( "" ),
   44       m_pkgmkArgs( "" ),
   45       m_pkgaddArgs( "" ),
   46       m_pkgrmArgs( "" ),
   47       m_sortArgs( "" ),
   48       m_filter( "" ),
   49       m_commandName( "" ),
   50       m_unknownOption( "" ),
   51       m_installRoot( "" ),
   52       m_ignore( "" ),
   53       m_commandType(HELP),
   54       m_argc( argc ),
   55       m_argv( argv ),
   56       m_verbose( 0 )
   57 {
   58 }
   59 
   60 
   61 /*!
   62   \return true if an alternate configuration file is given
   63 */
   64 bool ArgParser::isAlternateConfigGiven() const
   65 {
   66     return m_isAlternateConfigGiven;
   67 }
   68 
   69 
   70 /*!
   71   \return true if a command is given
   72 */
   73 bool ArgParser::isCommandGiven() const
   74 {
   75     return m_isCommandGiven;
   76 }
   77 
   78 
   79 /*!
   80   \return a list of arguments not processed by ArgParser
   81 */
   82 const list<char*>& ArgParser::otherArgs() const
   83 {
   84     return m_otherArgs;
   85 }
   86 
   87 
   88 /*!
   89   \return what command was given
   90 */
   91 ArgParser::Type ArgParser::commandType() const
   92 {
   93     return m_commandType;
   94 }
   95 
   96 
   97 /*!
   98   \return addtional arguments to pkgmk
   99 */
  100 const string& ArgParser::pkgmkArgs() const
  101 {
  102     return m_pkgmkArgs;
  103 }
  104 
  105 
  106 /*!
  107   \return addtional arguments to pkgadd
  108 */
  109 const string& ArgParser::pkgaddArgs() const
  110 {
  111     return m_pkgaddArgs;
  112 }
  113 
  114 
  115 /*!
  116   \return the name of the alternative configuration file
  117 */
  118 const string& ArgParser::alternateConfigFile() const
  119 {
  120     return m_alternateConfigFile;
  121 }
  122 
  123 
  124 /*!
  125   parse the arguments
  126   \return true on success
  127 */
  128 bool ArgParser::parse()
  129 {
  130     const int commandCount = 35;
  131     string commands[commandCount] = { "list", "search", "dsearch",
  132                                       "info",
  133                                       "depends", "install", "depinst",
  134                                       "help", "isinst", "dup", "update",
  135                                       "quickdep", "diff", "quickdiff",
  136                                       "grpinst", "version", "cache",
  137                                       "path", "listinst", "printf", "readme",
  138                                       "dependent", "sysup", "current",
  139                                       "fsearch", "lock", "unlock",
  140                                       "listlocked", "cat", "ls", "edit",
  141                                       "remove", "deptree", "dumpconfig",
  142                                       "listorphans" };
  143 
  144     Type commandID[commandCount] = { LIST, SEARCH, DSEARCH, INFO,
  145                                      DEPENDS, INSTALL, DEPINST,
  146                                      HELP, ISINST, DUP, UPDATE,
  147                                      QUICKDEP, DIFF, QUICKDIFF,
  148                                      GRPINST, SHOW_VERSION, CREATE_CACHE,
  149                                      PATH, LISTINST, PRINTF, README,
  150                                      DEPENDENT, SYSUP, CURRENT,
  151                                      FSEARCH, LOCK, UNLOCK, LISTLOCKED,
  152                                      CAT, LS, EDIT, REMOVE, DEPTREE,
  153                                      DUMPCONFIG, LISTORPHANS };
  154     if ( m_argc < 2 ) {
  155         return false;
  156     }
  157 
  158     // if called from a symlink ending on prt-cache, use cached
  159     // access
  160     string app = m_argv[0];
  161     string::size_type pos = app.rfind( "/" );
  162     if ( pos != string::npos ) {
  163         app = app.substr( pos );
  164     }
  165     if ( app.find( "prt-cache" ) != string::npos ) {
  166         m_useCache = true;
  167         m_calledAsPrtCache = true;
  168     }
  169 
  170     for ( int i = 1; i < m_argc; ++i ) {
  171         if ( m_argv[i][0] == '-' ) {
  172             string s = m_argv[i];
  173             if ( s == "-v" ) {
  174                 m_verbose += 1;
  175             } else if ( s == "-vv" ) {
  176                 m_verbose += 2;
  177             } else if ( s == "--force" ) {
  178                 m_isForced = true;
  179             } else if ( s == "--test" ) {
  180                 m_isTest = true;
  181             } else if ( s == "--cache" ) {
  182                 m_useCache = true;
  183             } else if ( s == "--nodeps" ) {
  184                 m_nodeps = true;
  185             } else if ( s == "--all" ) {
  186                 m_all = true;
  187             } else if ( s == "--path" ) {
  188                 m_printPath = true;
  189             } else if ( s == "--log" ) {
  190                 m_writeLog = true;
  191             } else if ( s == "--pre-install" ) {
  192                 m_execPreInstall = true;
  193             } else if ( s == "--post-install" ) {
  194                 m_execPostInstall = true;
  195             } else if ( s == "--install-scripts" ) {
  196                 m_execPreInstall = true;
  197                 m_execPostInstall = true;
  198             } else if ( s == "--no-std-config" ) {
  199                 m_noStdConfig = true;
  200             } else if ( s == "--prefer-higher" || s == "-ph" ) {
  201                 m_preferHigher = true;
  202             } else if ( s == "--strict-diff" || s == "-sd" ) {
  203                 m_strictDiff = true;
  204             } else if ( s == "--regex" ) {
  205                 m_useRegex = true;
  206             } else if ( s == "--full" ) {
  207                 m_fullPath = true;
  208             } else if ( s == "--recursive" ) {
  209                 m_recursive = true;
  210             } else if ( s == "--tree" ) {
  211                 m_printTree = true;
  212             } else if ( s == "--depsort" ) {
  213                 m_depSort = true;
  214 
  215             } else if ( s == "-f" ) {
  216                 m_pkgaddArgs += " " + s;
  217             } else if ( s == "-fr" ) {
  218                 m_pkgmkArgs += " -f";
  219             } else if ( s == "-if" ) {
  220                 m_pkgmkArgs += " " + s;
  221             } else if ( s == "-uf" ) {
  222                 m_pkgmkArgs += " " + s;
  223             } else if ( s == "-im" ) {
  224                 m_pkgmkArgs += " " + s;
  225             } else if ( s == "-um" ) {
  226                 m_pkgmkArgs += " " + s;
  227             } else if ( s == "-is" ) {
  228                 m_pkgmkArgs += " " + s;
  229             } else if ( s == "-us" ) {
  230                 m_pkgmkArgs += " " + s;
  231 	    } else if ( s == "-kw" ) {
  232                 m_pkgmkArgs += " " + s;
  233             } else if ( s == "-ns" ) {
  234                 m_pkgmkArgs += " " + s;
  235             } else if ( s == "-fi" ) {
  236                 m_pkgaddArgs += " -f";
  237             }
  238 
  239             // substrings
  240             else if ( s.substr( 0, 8 )  == "--margs=" ) {
  241                 m_pkgmkArgs += " " + s.substr( 8 );
  242             } else if ( s.substr( 0, 8 ) == "--aargs=" ) {
  243                 m_pkgaddArgs += " " + s.substr( 8 );
  244             } else if ( s.substr( 0, 8 ) == "--rargs=" ) {
  245                 m_pkgrmArgs = s.substr( 8 );
  246             } else if ( s.substr( 0, 7 ) == "--sort=" ) {
  247                 m_sortArgs = s.substr( 7 );
  248             } else if ( s.substr( 0, 9 ) == "--filter=" ) {
  249                 m_filter = s.substr( 9 );
  250                 m_hasFilter = true;
  251             } else if ( s.substr( 0, 9 ) == "--config=" ) {
  252                 m_alternateConfigFile = s.substr( 9 );
  253                 m_isAlternateConfigGiven = true;
  254             } else if ( s.substr( 0, 16 ) == "--config-append=" ) {
  255                 m_configData.push_back(make_pair(m_argv[i]+16,
  256                                                  CONFIG_APPEND ) );
  257             } else if ( s.substr( 0, 17 ) == "--config-prepend=" ) {
  258                 m_configData.push_back(make_pair(m_argv[i]+17,
  259                                                  CONFIG_PREPEND ) );
  260             } else if ( s.substr( 0, 13 ) == "--config-set=" ) {
  261                 m_configData.push_back(make_pair(m_argv[i]+13, CONFIG_SET ) );
  262             } else if ( s.substr( 0, 15 ) == "--install-root=" ) {
  263                 m_installRoot = s.substr(15);
  264             } else if ( s.substr( 0, 9 ) == "--ignore=" ) {
  265                 m_ignore = s.substr(9);
  266             } else {
  267                 m_unknownOption = s;
  268                 return false;
  269             }
  270         } else {
  271             if (!m_isCommandGiven) {
  272                 string s = m_argv[i];
  273                 m_commandName = s;
  274                 for ( int i = 0; i < commandCount; ++i ) {
  275                     if ( s == commands[i] ) {
  276                         m_isCommandGiven = true;
  277                         m_commandType = commandID[i];
  278                         break;
  279                     }
  280                 }
  281                 // first argument must be command
  282                 if ( !m_isCommandGiven ) {
  283                     return false;
  284                 }
  285             } else {
  286                 m_otherArgs.push_back( m_argv[i] );
  287             }
  288         }
  289     }
  290 
  291 
  292 
  293     return m_isCommandGiven;
  294 }
  295 
  296 
  297 /*!
  298   \return true whether --force has been specified
  299 */
  300 bool ArgParser::isForced() const
  301 {
  302     return m_isForced;
  303 }
  304 
  305 
  306 /*!
  307   \return true whether --test has been specified
  308 */
  309 bool ArgParser::isTest() const
  310 {
  311     return m_isTest;
  312 }
  313 
  314 
  315 /*!
  316   \return the level of verbose: -v -> 1, -vv -> 2
  317 */
  318 int ArgParser::verbose() const
  319 {
  320     return m_verbose;
  321 }
  322 
  323 
  324 /*!
  325   \return whether --cache has been specified
  326 */
  327 bool ArgParser::useCache() const
  328 {
  329     return m_useCache;
  330 }
  331 
  332 
  333 /*!
  334   \return whether prt-get was called as 'prt-cache' or not
  335 */
  336 bool ArgParser::wasCalledAsPrtCached() const
  337 {
  338     return m_calledAsPrtCache;
  339 }
  340 
  341 /*!
  342   \return whether prt-get should write to a log file or not
  343 */
  344 bool ArgParser::writeLog() const
  345 {
  346     return m_writeLog;
  347 }
  348 
  349 /*!
  350   \return the --sort="..." string
  351 */
  352 const string& ArgParser::sortArgs() const
  353 {
  354     return m_sortArgs;
  355 }
  356 
  357 /*!
  358   \return whether there was a --filter argument
  359 */
  360 bool ArgParser::hasFilter() const
  361 {
  362     return m_hasFilter;
  363 }
  364 
  365 
  366 /*!
  367   \return whether there was a --no-std-config argument
  368  */
  369 bool ArgParser::noStdConfig() const
  370 {
  371     return m_noStdConfig;
  372 }
  373 
  374 
  375 /*!
  376   \return the --filter="..." string
  377 */
  378 const string& ArgParser::filter() const
  379 {
  380     return m_filter;
  381 }
  382 
  383 /*!
  384   \return whether there was a --nodeps argument
  385 */
  386 bool ArgParser::nodeps() const
  387 {
  388     return m_nodeps;
  389 }
  390 
  391 /*!
  392   \return whether there was a --all argument
  393 */
  394 bool ArgParser::all() const
  395 {
  396     return m_all;
  397 }
  398 
  399 bool ArgParser::printPath() const
  400 {
  401     return m_printPath;
  402 }
  403 
  404 bool ArgParser::recursive() const
  405 {
  406     return m_recursive;
  407 }
  408 
  409 bool ArgParser::printTree() const
  410 {
  411     return m_printTree;
  412 }
  413 
  414 bool ArgParser::depSort() const
  415 {
  416     return m_depSort;
  417 }
  418 
  419 const string& ArgParser::commandName() const
  420 {
  421     return m_commandName;
  422 }
  423 
  424 const string& ArgParser::unknownOption() const
  425 {
  426     return m_unknownOption;
  427 }
  428 
  429 bool ArgParser::execPreInstall() const
  430 {
  431     return m_execPreInstall;
  432 }
  433 
  434 bool ArgParser::execPostInstall() const
  435 {
  436     return m_execPostInstall;
  437 }
  438 
  439 const list< pair<char*, ArgParser::ConfigArgType> >
  440 ArgParser::configData() const
  441 {
  442     return m_configData;
  443 }
  444 
  445 const string& ArgParser::installRoot() const
  446 {
  447     return m_installRoot;
  448 }
  449 
  450 const string& ArgParser::pkgrmArgs() const
  451 {
  452     return m_pkgrmArgs;
  453 }
  454 
  455 bool ArgParser::preferHigher() const
  456 {
  457     return m_preferHigher;
  458 }
  459 
  460 bool ArgParser::strictDiff() const
  461 {
  462     return m_strictDiff;
  463 }
  464 
  465 bool ArgParser::useRegex() const
  466 {
  467     return m_useRegex;
  468 }
  469 
  470 bool ArgParser::fullPath() const
  471 {
  472     return m_fullPath;
  473 }
  474 
  475 
  476 const string& ArgParser::ignore() const
  477 {
  478     return m_ignore;
  479 }

Generated by cgit