// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Commandline.h,v 1.20 2003/09/14 10:30:21 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_UTIL_COMMANDLINE_H
# define INCLUDE_JAKELIB2_UTIL_COMMANDLINE_H

namespace jakelib {
  namespace util {
    class ArrayListIterator;
    class Hashtable;
    class ArrayList;

    /**
     * This class provides a simple commandline parser.
     *
     * \b Example:
     * \code
     * int main(int argc, char* argv[]) {
     *   Commandline* cmd = new Commandline(argc, argv);
     *   cmd->allowOption(`"file"`, `"f"`, true);
     *   cmd->allowOption(`"extract"`, `"x"`);
     *   if (cmd->optionSet(`"extract"`))
     *     printf("EXTRACT!\n");
     *   System::out->println(`"Filename: "` .. cmd->getAsString(`"file"`));
     * }
     * \endcode
     *
     * @class Commandline
     * @version $Id: Commandline.h,v 1.20 2003/09/14 10:30:21 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class Commandline : public jakelib::lang::Object
      {
      public:
	/**
	 * @param argc The <TT>argc</TT> argument from your <TT>main</TT> function.
	 * @param argv The <TT>argv[]</TT> argument from your <TT>main</TT> function.
	 */
	Commandline(int argc, char* argv[]);

	~Commandline();

	static int UNDEFINED;

	/**
	 * Returns a certain argument.
	 *
	 * @param idx Index in <TT>params</TT> array.
         * @return The string value or \c null.
	 */  
	jakelib::lang::String* get(int idx);

	/**
	 * Returns the filename of the executable.
	 */
	jakelib::lang::String* getExeFile();

	/**
	 * Returns the path where the executable is located in.
	 */
	jakelib::lang::String* getExePath();

	/**
	 * Returns the number of arguments.
	 */
	int size();

	/**
	 * Returns an iterator over all arguments.
	 * Don't use an iterator after calling <TT>allowOption</TT>. This might cause
	 * inconsistency! Create a new one!
	 */
	ArrayListIterator* iterator();

	/**
	 * Searches the current arguments (<TT>params</TT>) for the specified option name.
	 * If found, it is added to the <TT>options</TT> hash and removed from the
	 * <TT>params</TT> array.
	 *
	 * @param key The long version of the option name (without leading \c -- ).
	 * @param shortkey The short (should be one character only) version of the
         *                 option name (without leading \c - ).
	 * @param expectValue Whether to expect a value for this option (e.g. "--file=output.txt").
	 */
	void allowOption(jakelib::lang::String* key,
                         jakelib::lang::String* shortkey = null,
                         jboolean expectValue = false);

	/**
	 * Determines whether a certain option is set.
	 *
	 * @param key The long version of the option name (without leading <TT>--</TT>).
	 * @return \c true if the specified option is set, <TT>FALSE</TT> otherwise.
	 */
	jboolean optionSet(jakelib::lang::String* key);

	/**
	 * Returns the string representation of a given option.
	 *
	 * @param key The long version of the option name (without leading \c -- ).
         * @param def The default value.
	 * @return The string or the specified default value if the options is not set.
	 */
	jakelib::lang::String* getAsString(jakelib::lang::String* key,
                                           jakelib::lang::String* def = null);

	/**
	 * Returns the integer representation of a given option.
	 *
	 * @param key The long version of the option name (without leading \c -- ).
         * @param def The default value.
	 * @return The integer value or the specified default value if the option is not set.
	 */
	int getAsInt(jakelib::lang::String* key, int def);

      protected:

	/** Holds the options that have been propagated via the <TT>allowOption</TT> method. */
	Hashtable* options;

	/** Holds the argument list. */
	ArrayList* params;

	/** Holds the filename of the executable. */
	jakelib::lang::String* filename;

	/** Holds the path to the executable. */
	jakelib::lang::String* pathname;

	JAKELIB_DECLARE_CLASS(Commandline);
      };

  }
}

#endif

