Files
wlan-cloud-lib-poco/Shell/include/Poco/Shell/AbstractCommand.h
Aleksandar Fabijanic d712530c7d added Poco::Shell
2013-04-23 20:06:03 -05:00

165 lines
5.5 KiB
C++

//
// AbstractCommand.h
//
// $Id: //poco/1.4/Shell/include/Poco/Shell/AbstractCommand.h#1 $
//
// Library: Shell
// Package: Shell
// Module: AbstractCommand
//
// Definition of the AbstractCommand class.
//
// Copyright (c) 2009-2013, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Shell_AbstractCommand_INCLUDED
#define Shell_AbstractCommand_INCLUDED
#include "Poco/Shell/Command.h"
#include "Poco/Util/OptionSet.h"
namespace Poco {
namespace Shell {
class Shell_API AbstractCommand: public Command
/// AbstractCommand provides a partial implementation of Command,
/// doing automatic argument handling based on the options handling
/// framework from the Util library.
///
/// Options must be given in Unix-style syntax (-o or --option).
///
/// Option handling for commands works in a similar way to
/// command line options handling in the Poco::Util::Application
/// class.
{
public:
// Command
virtual int execute(Session& session, const std::vector<std::string>& args, std::ostream& ostr);
protected:
AbstractCommand();
/// Creates the AbstractCommand.
AbstractCommand(const std::string& description, const std::string& usage);
/// Creates the AbstractCommand, using the given description and usage hint.
///
/// The given description and usage hint is used in the help text given
/// if the command is invoked with the --help option.
virtual ~AbstractCommand();
/// Destroys the AbstractCommand.
virtual int executeImpl(Session& session, const std::vector<std::string>& args, std::ostream& ostr) = 0;
/// Execute the command, using the given arguments.
/// Called by AbstractCommand::execute() after options have been handled.
///
/// Any output produced by executing the command can be
/// written to the given output stream.
///
/// An integer must be returned that denotes success or failure of the command.
/// Upon success, 0 shall be returned. The CommandStatus enumeration defines
/// a few commonly used error codes that should be used.
virtual void defineOptions(Poco::Util::OptionSet& options);
/// Called before command line processing begins.
/// If a Command wants to support command line arguments,
/// it must override this method and call the base class
/// implementation as well.
///
/// The default implementation only defines the --help option.
///
/// A Command should specify a callback to handle the option.
const Poco::Util::OptionSet& options() const;
/// Returns the application's option set.
void processOptions(std::vector<std::string>& args);
/// Processes command-line options.
void stopOptionsProcessing();
/// If called from an option callback, stops all further
/// options processing.
///
/// If called, the following options on the command line
/// will not be processed, and required options will not
/// be checked.
///
/// This is useful, for example, if an option for displaying
/// help information has been encountered and no other things
/// besides displaying help shall be done.
virtual void handleOption(const std::string& name, const std::string& value);
/// Called when the option with the given name is encountered
/// during command line arguments processing.
///
/// The default implementation does option validation, bindings
/// and callback handling.
///
/// Overriding implementations must call the base class implementation.
const std::string& commandName() const;
/// Returns the command name.
void handleHelp(const std::string& name, const std::string& value);
/// Handles the --help command line option.
void displayHelp(std::ostream& ostr);
/// Writes a help text to the given stream.
private:
std::string _description;
std::string _usage;
Poco::Util::OptionSet _options;
bool _stopOptionsProcessing;
bool _showHelp;
std::string _commandName;
};
//
// inlines
//
inline const Poco::Util::OptionSet& AbstractCommand::options() const
{
return _options;
}
inline const std::string& AbstractCommand::commandName() const
{
return _commandName;
}
} } // namespace Poco::Shell
#endif // Shell_AbstractCommand_INCLUDED