Skip to content

Commit

Permalink
Added parsing with unused arguments, so multiple images can be loaded…
Browse files Browse the repository at this point in the history
…, with:

getUnusedArgs().

See mrv2's lib/mrvApp/App.cpp source code for more info.
  • Loading branch information
ggarra13 committed Sep 25, 2023
1 parent 3bdcefe commit c4f392e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
13 changes: 10 additions & 3 deletions lib/tlApp/CmdLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ namespace tl
ICmdLineArg(
const std::string& name,
const std::string& help,
bool optional);
bool optional,
bool unused = false);

public:
virtual ~ICmdLineArg() = 0;
Expand All @@ -119,11 +120,15 @@ namespace tl

//! Get whether this argument is optional.
bool isOptional() const;

//! Get whether this argument is for unused arguments.
bool isUnused() const;

protected:
std::string _name;
std::string _help;
bool _optional = false;
bool _unused = false;
};

//! Command line value argument.
Expand All @@ -135,15 +140,17 @@ namespace tl
T& value,
const std::string& name,
const std::string& help,
bool optional);
bool optional,
bool unused = false);

public:
//! Create a new command line argument.
static std::shared_ptr<CmdLineValueArg<T> > create(
T& value,
const std::string& name,
const std::string& help,
bool optional = false);
bool optional = false,
bool unused = false);

void parse(std::vector<std::string>& args) override;

Expand Down
22 changes: 16 additions & 6 deletions lib/tlApp/CmdLineInline.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <tlCore/String.h>

#include <algorithm>
#include <iostream>

namespace tl
{
Expand Down Expand Up @@ -112,10 +113,12 @@ namespace tl
inline ICmdLineArg::ICmdLineArg(
const std::string& name,
const std::string& help,
bool optional) :
bool optional,
bool unused) :
_name(name),
_help(help),
_optional(optional)
_optional(optional),
_unused(unused)
{}

inline ICmdLineArg::~ICmdLineArg()
Expand All @@ -136,13 +139,19 @@ namespace tl
return _optional;
}

inline bool ICmdLineArg::isUnused() const
{
return _unused;
}

template<typename T>
inline CmdLineValueArg<T>::CmdLineValueArg(
T& value,
const std::string& name,
const std::string& help,
bool optional) :
ICmdLineArg(name, help, optional),
bool optional,
bool unused) :
ICmdLineArg(name, help, optional, unused),
_value(value)
{}

Expand All @@ -151,9 +160,10 @@ namespace tl
T& value,
const std::string& name,
const std::string& help,
bool optional)
bool optional,
bool unused)
{
return std::shared_ptr<CmdLineValueArg<T> >(new CmdLineValueArg<T>(value, name, help, optional));
return std::shared_ptr<CmdLineValueArg<T> >(new CmdLineValueArg<T>(value, name, help, optional, unused));
}

template<typename T>
Expand Down
39 changes: 30 additions & 9 deletions lib/tlApp/IApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ namespace tl
return _exit;
}

const std::vector<std::string> IApp::getUnusedArgs() const
{
return _unusedArgs;
}

void IApp::_log(const std::string& value, log::Type type)
{
_context->log(_p->cmdLine.name, value, type);
Expand Down Expand Up @@ -151,11 +156,16 @@ namespace tl
arg(e.what()));
}
}
bool unusedArgs = false;
size_t requiredArgs = 0;
size_t optionalArgs = 0;
for (const auto& i : p.cmdLine.args)
{
if (!i->isOptional())
if (i->isUnused())
{
unusedArgs = true;
}
else if (!i->isOptional())
{
++requiredArgs;
}
Expand All @@ -165,8 +175,8 @@ namespace tl
}
}
if (p.cmdLine.argv.size() < requiredArgs ||
p.cmdLine.argv.size() > requiredArgs + optionalArgs ||
_options.help)
(p.cmdLine.argv.size() > requiredArgs + optionalArgs &&
!unusedArgs) || _options.help)
{
_printCmdLineHelp();
return 1;
Expand All @@ -175,9 +185,17 @@ namespace tl
{
try
{
if (!(p.cmdLine.argv.empty() && i->isOptional()))
if (!p.cmdLine.argv.empty())
{
i->parse(p.cmdLine.argv);
if(i->isUnused())
{
_unusedArgs = p.cmdLine.argv;
break;
}
if (i->isOptional())
{
i->parse(p.cmdLine.argv);
}
}
}
catch (const std::exception& e)
Expand All @@ -199,23 +217,26 @@ namespace tl
{
std::stringstream ss;
ss << " " + p.cmdLine.name;
if (p.cmdLine.options.size())
{
ss << " [options]...";
}
if (p.cmdLine.args.size())
{
std::vector<std::string> args;
for (const auto& i : p.cmdLine.args)
{
const bool optional = i->isOptional();
const bool unused = i->isUnused();
args.push_back(
(optional ? "[" : "(") +
string::toLower(i->getName()) +
(optional ? "]" : ")"));
if (unused)
args.push_back("...");
}
ss << " " << string::join(args, " ");
}
if (p.cmdLine.options.size())
{
ss << " [option],...";
}
_print(ss.str());
_printNewline();
}
Expand Down
4 changes: 4 additions & 0 deletions lib/tlApp/IApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ namespace tl
//! Get the exit code.
int getExit() const;

//! Get unused arguments
const std::vector<std::string> getUnusedArgs() const;

protected:
void _log(const std::string&, log::Type = log::Type::Message);

void _print(const std::string&);
void _printNewline();
void _printError(const std::string&);

std::vector<std::string> _unusedArgs;
std::shared_ptr<system::Context> _context;
Options _options;
int _exit = 0;
Expand Down

0 comments on commit c4f392e

Please sign in to comment.