Ignore:
Timestamp:
12/20/03 13:46:17 (21 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MArgs.cc

    r2529 r2728  
    3030// This is a helper class for executables to parse command line arguments
    3131//
     32// Arguments beginning with a trailing '-' are called 'options'.
     33// Arguments without a trailing '-' are considered 'arguments'
     34//
    3235//////////////////////////////////////////////////////////////////////////////
    3336#include "MArgs.h"
     
    8285// --------------------------------------------------------------------------
    8386//
    84 // Print all arguments parsed.
     87// Print everything parsed.
     88// Using 'options' as option only 'options' are printed.
     89// Using 'arguments' as option only 'arguments' are printed.
    8590//
    8691void MArgs::Print(const Option_t *o) const
    8792{
    8893    gLog << all << underline << fName << ":" << endl;
     94
     95    const TString str(o);
     96
     97    if (!str.CompareTo("options", TString::kIgnoreCase))
     98    {
     99        TIter Next(fArgv);
     100        TString *s = NULL;
     101        while ((s=dynamic_cast<TString*>(Next())))
     102            if (s->BeginsWith("-"))
     103                gLog << *s << endl;
     104        return;
     105    }
     106
     107    if (!str.CompareTo("arguments", TString::kIgnoreCase))
     108    {
     109        TIter Next(fArgv);
     110        TString *s = NULL;
     111        while ((s=dynamic_cast<TString*>(Next())))
     112            if (!s->BeginsWith("-"))
     113                gLog << *s << endl;
     114        return;
     115    }
     116
    89117    fArgv->Print();
    90118}
     
    246274// --------------------------------------------------------------------------
    247275//
     276// return the number of arguments with a trainling '-'
     277//
     278Int_t MArgs::GetNumOptions() const
     279{
     280    Int_t num = 0;
     281
     282    TIter Next(fArgv);
     283    TString *s = NULL;
     284    while ((s=dynamic_cast<TString*>(Next())))
     285        if (s->BeginsWith("-"))
     286            num++;
     287
     288    return num;
     289}
     290
     291// --------------------------------------------------------------------------
     292//
     293// return the total number of entries
     294//
     295Int_t MArgs::GetNumEntries() const
     296{
     297    return fArgv->GetSize();
     298}
     299
     300// --------------------------------------------------------------------------
     301//
    248302// Checks whether an argument beginning with 'n' is existing, eg:
    249303//  executable -value5
     
    259313    while ((s=dynamic_cast<TString*>(Next())))
    260314        if (s->BeginsWith(name))
     315            return kTRUE;
     316    return kFALSE;
     317}
     318
     319// --------------------------------------------------------------------------
     320//
     321// Checks whether an argument beginning with 'n' is existing, eg:
     322//  executable -value5
     323//   HasOption("-value") will return false
     324//  executable -value
     325//   HasOption("-value") will return true
     326//
     327Bool_t MArgs::HasOnly(const TString n) const
     328{
     329    const TString name = n.Strip(TString::kBoth);
     330
     331    TIter Next(fArgv);
     332    TString *s = NULL;
     333    while ((s=dynamic_cast<TString*>(Next())))
     334        if (*s==name)
    261335            return kTRUE;
    262336    return kFALSE;
     
    284358    return kFALSE;
    285359}
     360
     361// --------------------------------------------------------------------------
     362//
     363// Checks whether an argument beginning with 'n' is exists and a
     364// corresponding option is available, eg.
     365//  executable -value5
     366//  HasOption("-value") will return false
     367// but:
     368//  executable -value
     369//  HasOption("-value") will return true
     370//
     371// The argument is removed from the internal list.
     372//
     373Bool_t MArgs::HasOnlyAndRemove(const TString n)
     374{
     375    const TString name = n.Strip(TString::kBoth);
     376
     377    TIter Next(fArgv);
     378    TString *s = NULL;
     379    while ((s=dynamic_cast<TString*>(Next())))
     380        if (*s==name)
     381        {
     382            delete fArgv->Remove(dynamic_cast<TObject*>(s));
     383            return kTRUE;
     384        }
     385
     386    return kFALSE;
     387}
Note: See TracChangeset for help on using the changeset viewer.