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

Generated by cgit