Skip to content

Commit

Permalink
enh(Application): add ignoreUnknownOptions() function (#4661)
Browse files Browse the repository at this point in the history
* enh(Application): remove useless function

* enh(Application): add ignoreUnknownOptions() function

* fix(OptionSet): comment error

* enh(Application): public two functions: getApplicationPath/getApplicationDirectory

* enh(Application): make findAppConfigFile function public and virtual

* style(Application): code alignment

* fix(Application): do not re-throw exception
  • Loading branch information
siren186 authored Sep 11, 2024
1 parent 6a6c5f7 commit 2812dd7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
26 changes: 22 additions & 4 deletions Util/include/Poco/Util/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ class Util_API Application: public Subsystem
/// will be propagated to the caller. If uninitialize() throws
/// an exception, the exception will be propagated to the caller.

void getApplicationPath(Poco::Path& path) const;
/// Returns the file path of the application executable.

void getApplicationDirectory(Poco::Path& dir) const;
/// Returns the directory that contains the application executable.

std::string commandName() const;
/// Returns the command name used to invoke the application.

Expand Down Expand Up @@ -300,6 +306,14 @@ class Util_API Application: public Subsystem
/// help information has been encountered and no other things
/// besides displaying help shall be done.

void ignoreUnknownOptions();
/// Ignore unknown options. So that we can skip those options
/// we don't care about.
///
/// When your application command line has many options,
/// calling this function can help you handle only the options
/// you want to handle

static WindowSize windowSize();
/// Returns the current window size of the console window,
/// if available.
Expand Down Expand Up @@ -364,6 +378,12 @@ class Util_API Application: public Subsystem
/// Returns an exit code which should be one of the values
/// from the ExitCode enumeration.

virtual bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const;
/// Find the application config file.
///
/// loadConfiguration will call this function to find config file,
/// you can override this function to find your own config file.

bool findFile(Poco::Path& path) const;
/// Searches for the file in path in the application directory.
///
Expand All @@ -386,10 +406,7 @@ class Util_API Application: public Subsystem
void setup();
void setArgs(int argc, char* argv[]);
void setArgs(const ArgVec& args);
void getApplicationPath(Poco::Path& path) const;
void processOptions();
bool findAppConfigFile(const std::string& appName, const std::string& extension, Poco::Path& path) const;
bool findAppConfigFile(const Path& basePath, const std::string& appName, const std::string& extension, Poco::Path& path) const;

typedef LayeredConfiguration::Ptr ConfigPtr;
typedef Poco::Logger::Ptr LoggerPtr;
Expand All @@ -402,9 +419,10 @@ class Util_API Application: public Subsystem
ArgVec _unprocessedArgs;
OptionSet _options;
bool _unixOptions;
Logger* _pLogger;
Logger* _pLogger;
Poco::Timestamp _startTime;
bool _stopOptionsProcessing;
bool _ignoreUnknownOptions;

#if defined(POCO_OS_FAMILY_UNIX) && !defined(POCO_VXWORKS)
std::string _workingDirAtLaunch;
Expand Down
4 changes: 2 additions & 2 deletions Util/include/Poco/Util/OptionSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class Util_API OptionSet
/// The name must either match the short or full name of an
/// option. Comparison case sensitive for the short name and
/// not case sensitive for the full name.
/// Throws a NotFoundException if no matching option has been found.
/// Throws an UnknownOptionException if a partial full name matches
/// Throws a UnknownOptionException if no matching option has been found.
/// Throws an AmbiguousOptionException if a partial full name matches
/// more than one option.

Iterator begin() const;
Expand Down
66 changes: 39 additions & 27 deletions Util/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionProcessor.h"
#include "Poco/Util/Validator.h"
#include "Poco/Util/OptionException.h"
#include "Poco/Environment.h"
#include "Poco/Exception.h"
#include "Poco/NumberFormatter.h"
Expand Down Expand Up @@ -76,7 +77,8 @@ Application::Application():
_initialized(false),
_unixOptions(true),
_pLogger(&Logger::get("ApplicationStartup"s)),
_stopOptionsProcessing(false)
_stopOptionsProcessing(false),
_ignoreUnknownOptions(false)
{
setup();
}
Expand All @@ -87,7 +89,8 @@ Application::Application(int argc, char* argv[]):
_initialized(false),
_unixOptions(true),
_pLogger(&Logger::get("ApplicationStartup"s)),
_stopOptionsProcessing(false)
_stopOptionsProcessing(false),
_ignoreUnknownOptions(false)
{
setup();
init(argc, argv);
Expand Down Expand Up @@ -330,6 +333,11 @@ void Application::stopOptionsProcessing()
}


void Application::ignoreUnknownOptions()
{
_ignoreUnknownOptions = true;
}

Application::WindowSize Application::windowSize()
{
WindowSize size{0, 0};
Expand Down Expand Up @@ -430,18 +438,37 @@ void Application::processOptions()
{
std::string name;
std::string value;
if (processor.process(*it, name, value))
bool success = false;

if (_ignoreUnknownOptions)
{
try
{
success = processor.process(*it, name, value);
} catch (Poco::Util::UnknownOptionException&) { }
}
else
{
success = processor.process(*it, name, value);
}

if (success)
{
if (!name.empty()) // "--" option to end options processing or deferred argument
{
handleOption(name, value);
}
it = _unprocessedArgs.erase(it);
}
else ++it;
else
{
++it;
}
}
if (!_stopOptionsProcessing)
{
processor.checkRequired();
}
}


Expand Down Expand Up @@ -483,6 +510,14 @@ void Application::getApplicationPath(Poco::Path& appPath) const
}


void Application::getApplicationDirectory(Poco::Path& dir) const
{
Path appPath;
getApplicationPath(appPath);
dir = appPath.parent();
}


bool Application::findFile(Poco::Path& path) const
{
if (path.isAbsolute()) return true;
Expand Down Expand Up @@ -529,29 +564,6 @@ bool Application::findAppConfigFile(const std::string& appName, const std::strin
}


bool Application::findAppConfigFile(const Path& basePath, const std::string& appName, const std::string& extension, Path& path) const
{
poco_assert (!appName.empty());

Path p(basePath,appName);
p.setExtension(extension);
bool found = findFile(p);
if (!found)
{
#if defined(_DEBUG)
if (appName[appName.length() - 1] == 'd')
{
p.setBaseName(appName.substr(0, appName.length() - 1));
found = findFile(p);
}
#endif
}
if (found)
path = p;
return found;
}


void Application::defineOptions(OptionSet& options)
{
for (auto& pSub: _subsystems)
Expand Down

0 comments on commit 2812dd7

Please sign in to comment.