/*
 * Copyright (c) 1999 Johnny C. Lam.
 * All rights reserved.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  The author makes no representations
 * about the suitability of this software for any purpose.  It is
 * provided "as is" without express or implied warranty.
 */

/* -*- C++ -*- */

#include <cmdargs.h>

template <class InputIterator>
void
CommandLine::Parse(int argc, char* argv[],
		   InputIterator use_f, InputIterator use_l,
		   InputIterator req_f, InputIterator req_l)
{
    while (req_f != req_l) {
	_options[*req_f]->require();
	++req_f;
    }
    
    CmdLine cmd(argv[0]);
    CmdArgvIter arg_iter(--argc, ++argv);

    while (use_f != use_l) {
	cmd.append(_options[*use_f]->cmdarg());
	++use_f;
    }
    cmd.parse(arg_iter);

    for (unsigned int i = 0; i < NUM_OPTIONS; ++i)
	_options[i]->assign();

    /* Allow "-v" to mean "-v 1" */
    if (_options[VERBOSE]->given() && !_options[VERBOSE]->value_given())
	_verbose = 1;
}

template <class InputIterator>
void
CommandLine::Write(std::ostream& ostr,
		   InputIterator use_f, InputIterator use_l) const
{
    ostr << "Arguments:" << endl;
    while (use_f != use_l) {
	_options[*use_f]->write(ostr);
	++use_f;
    }
    ostr << "End Arguments." << endl;
}
