Graeme Walker 245a01d527 v0.9.2
2001-09-12 12:00:00 +00:00

115 lines
2.9 KiB
C++

//
// Copyright (C) 2001 Graeme Walker <graeme_walker@users.sourceforge.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ===
//
// garg.h
//
#ifndef G_ARG_H
#define G_ARG_H
#include "gdef.h"
#include <vector>
#include <string>
namespace G
{
class Arg ;
} ;
// Class: G::Arg
// Description: A command line parser class.
//
// Also does command line splitting for Windows:
// the single command line string is split into
// an argv[] array, including argv[0].
//
class G::Arg
{
public:
Arg( int argc , char *argv[] ) ;
// Constructor taking argc/argv.
Arg() ;
// Default constructor for Windows.
// Initialise (once) with parse().
void parse( HINSTANCE hinstance , const std::string & command_line ) ;
// Windows only.
//
// Parses the given command line, splitting
// it up into an array of tokens.
// The program name is automatically
// added as the first token (cf. argv[0]).
~Arg() ;
// Destructor.
size_t c() const ;
// Returns the number of tokens in the
// command line, including the program
// name.
std::string v( size_t i ) const ;
// Returns the i'th argument.
// Precondition: i < c()
std::string prefix() const ;
// Returns the basename of v(0) without
// any extension.
bool contains( const std::string & sw , size_t sw_args = 0U ) const ;
// Returns true if the command line
// contains the given switch with enough
// command line arguments left to satisfy
// the given number of switch arguments.
size_t index( const std::string & sw , size_t sw_args = 0U ) const ;
// Returns the index of the given switch.
// Returns zero if not present.
void remove( const std::string & sw , size_t sw_args = 0U ) ;
// Removes the given switch and its
// arguments.
//
// Precondition: contains()
void removeAt( size_t sw_index , size_t sw_args = 0U ) ;
// Removes the given argument and the
// following <sw_args> ones.
Arg & operator=( const Arg & ) ;
// Assignment operator.
Arg( const Arg & ) ;
// Copy constructor.
private:
static std::string moduleName( HINSTANCE h ) ;
bool find( const std::string & sw , size_t sw_args , size_t *index_p ) const ;
void setPrefix() ;
private:
typedef std::vector<std::string GAllocator(std::string) > Array ;
Array m_array ;
std::string m_prefix ;
} ;
#endif