Webmaster  |  Imprint 
Platinum
Platinum C++ Framework
Main  |  License  |  Documentation  |  Download  |  Support 

Arg Class Template Reference

Read and extract commandline parameters from argc/argv. More...

#include <Pt/Arg.h>

List of all members.

Public Member Functions

 Arg (const T &def=T())
 Default constructor. Initializes value.
 Arg (int &argc, char *argv[], char ch, const T &def=T())
 extract parameter.
 Arg (int &argc, char *argv[], const char *str, const T &def=T())
 definition of long options.
 Arg (int &argc, char *argv[])
bool set (int &argc, char *argv[], char ch)
 extract parameter.
bool set (int &argc, char *argv[], const char *str)
 definition of long options.
bool set (int &argc, char *argv[])
 Reads next parameter and removes it.
const T & getValue () const
 returns the value.
 operator T () const
 returns the value.
bool isSet () const
 returns true, if the option was found and the default was not used.


Detailed Description

template<typename T>
class Pt::Arg< T >

Programs usually need some parameters. Usually they start with a '-' followed by a single character and optionally a value. Arg<T> extracts these and other parametes.

This default class processes paramters with a value, which defines a input-extractor-operator operator>> (istream&, T&).

Options are removed from the option-list, so programs can easily check after all options are extracted, if there are parameters left.

example:

          int main(int argc, char* argv[])
          {
            cxxtools::Arg<int> option_n(argc, argv, 'n', 0);
            std::cout << "value for -n: " << option_n << endl;
          }

Constructor & Destructor Documentation

Arg ( const T &  def = T()  ) 

Parameters:
def initial value

Arg ( int &  argc,
char *  argv[],
char  ch,
const T &  def = T() 
)

Parameters:
argc 1. parameter of main
argv 2. of main
ch optioncharacter
def default-value
example:
                Arg<unsigned> offset(argc, argv, 'o', 0);
                unsigned value = offset.getValue();

Arg ( int &  argc,
char *  argv[],
const char *  str,
const T &  def = T() 
)

GNU defines long options starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.

example:

                 Arg<int> option_number(argc, argv, "--number", 0);
                 std::cout << "number =" << option_number.getValue() << std::endl;


Member Function Documentation

bool set ( int &  argc,
char *  argv[],
char  ch 
)

Parameters:
argc 1. parameter of main
argv 2. of main
ch optioncharacter
example:
                Arg<unsigned> offset;
                offset.set(argc, argv, 'o');
                unsigned value = offset.getValue();

bool set ( int &  argc,
char *  argv[],
const char *  str 
)

GNU defines long options starting with "--". This (and more) is supported here. Instead of giving a single option-character, you specify a string.

example:

                  Arg<int> option_number;
                  number.set(argc, argv, "--number");
                  std::cout << "number =" << option_number.getValue() << std::endl;

operator T (  )  const

Instead of calling getValue() the argument can be converted implicitly.

example:

                void print(int i)
                { std::cout << i << std::endl; }

                int main(int argc, char* argv[])
                {
                  cxxtools::Arg<int> value(argc, argv, 'v', 0);
                  print(value);   // pass argument as a int to the function
                }

Copyright © 2003-2007 The Pt Development Team
Pt 1.0