mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Commands: Reduce code duplication
- Extract function to gather ids
This commit is contained in:
parent
c5b2e5a5cd
commit
dc6d3b9672
13 changed files with 70 additions and 81 deletions
14
src/CLI.cpp
14
src/CLI.cpp
|
@ -524,3 +524,17 @@ bool CLI::exactMatch (
|
|||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
std::vector<int> CLI::getIds() const
|
||||
{
|
||||
std::vector <int> ids;
|
||||
|
||||
for (auto& arg : _args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
@ -64,6 +64,7 @@ public:
|
|||
bool canonicalize (std::string&, const std::string&, const std::string&) const;
|
||||
std::string getBinary () const;
|
||||
std::string getCommand () const;
|
||||
std::vector <int> getIds () const;
|
||||
std::string dump (const std::string& title = "CLI Parser") const;
|
||||
|
||||
private:
|
||||
|
|
|
@ -37,13 +37,7 @@ int CmdContinue (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
}
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.size() > 1)
|
||||
throw std::string ("You can only specify one ID to continue.");
|
||||
|
|
|
@ -37,12 +37,9 @@ int CmdDelete (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs.
|
||||
std::vector <int> ids;
|
||||
for (auto& arg : cli._args)
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (! ids.size ())
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help delete'.");
|
||||
|
||||
// Load the data.
|
||||
|
|
|
@ -38,13 +38,9 @@ int CmdFill (
|
|||
Rules& rules,
|
||||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
for (auto& arg : cli._args)
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (! ids.size ())
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help fill'.");
|
||||
|
||||
// Load the data.
|
||||
|
|
|
@ -39,20 +39,17 @@ int CmdJoin (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
for (auto& arg : cli._args)
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
// Only 2 IDs allowed in a join.
|
||||
if (ids.size () != 2)
|
||||
throw std::string ("Two IDs must be specified. See 'timew help join'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
auto tracked = getTracked (database, rules, filter);
|
||||
|
||||
// Only 2 IDs allowed in a join.
|
||||
if (ids.size () != 2)
|
||||
throw std::string ("Two IDs must be specified. See 'timew help join'.");
|
||||
|
||||
// ID values must be in range.
|
||||
for (auto& id : ids)
|
||||
if (id > static_cast <int> (tracked.size ()))
|
||||
|
|
|
@ -39,21 +39,20 @@ int CmdLengthen (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help lengthen'.");
|
||||
|
||||
std::string delta;
|
||||
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
|
||||
if (arg.hasTag ("FILTER") &&
|
||||
arg._lextype == Lexer::Type::duration)
|
||||
delta = arg.attribute ("raw");
|
||||
}
|
||||
|
||||
if (! ids.size ())
|
||||
throw std::string ("IDs must be specified. See 'timew help lengthen'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
|
|
|
@ -39,20 +39,26 @@ int CmdMove (
|
|||
Database& database)
|
||||
{
|
||||
// Gather ID and TAGs.
|
||||
int id = 0;
|
||||
std::vector<int> ids = cli.getIds();
|
||||
|
||||
if (ids.size() > 1)
|
||||
throw std::string ("The 'move' command only supports a single ID.");
|
||||
|
||||
int id;
|
||||
|
||||
if (ids.empty())
|
||||
{
|
||||
id = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
id = ids[0];
|
||||
}
|
||||
|
||||
std::string new_start;
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
{
|
||||
if (id)
|
||||
throw std::string ("The 'move' command only supports a single ID.");
|
||||
else
|
||||
id = strtol (arg.attribute ("value").c_str (), NULL, 10);
|
||||
}
|
||||
|
||||
if (arg.hasTag ("FILTER") &&
|
||||
arg._lextype == Lexer::Type::date)
|
||||
if (arg.hasTag ("FILTER") && arg._lextype == Lexer::Type::date)
|
||||
new_start = arg.attribute ("raw");
|
||||
}
|
||||
|
||||
|
@ -64,7 +70,6 @@ int CmdMove (
|
|||
Interval filter;
|
||||
auto tracked = getTracked (database, rules, filter);
|
||||
|
||||
|
||||
if (id > static_cast <int> (tracked.size ()))
|
||||
throw format ("ID '@{1}' does not correspond to any tracking.", id);
|
||||
|
||||
|
|
|
@ -38,22 +38,19 @@ int CmdResize (
|
|||
Rules& rules,
|
||||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help resize'.");
|
||||
|
||||
std::string delta;
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
|
||||
if (arg.hasTag ("FILTER") &&
|
||||
arg._lextype == Lexer::Type::duration)
|
||||
delta = arg.attribute ("raw");
|
||||
}
|
||||
|
||||
if (! ids.size ())
|
||||
throw std::string ("IDs must be specified. See 'timew help resize'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
|
|
|
@ -38,22 +38,19 @@ int CmdShorten (
|
|||
Rules& rules,
|
||||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help shorten'.");
|
||||
|
||||
std::string delta;
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
|
||||
if (arg.hasTag ("FILTER") &&
|
||||
arg._lextype == Lexer::Type::duration)
|
||||
delta = arg.attribute ("raw");
|
||||
}
|
||||
|
||||
if (! ids.size ())
|
||||
throw std::string ("IDs must be specified. See 'timew help shorten'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
|
|
|
@ -38,13 +38,9 @@ int CmdSplit (
|
|||
Rules& rules,
|
||||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
for (auto& arg : cli._args)
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (! ids.size ())
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help split'.");
|
||||
|
||||
// Load the data.
|
||||
|
|
|
@ -38,20 +38,18 @@ int CmdTag (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help tag'.");
|
||||
|
||||
std::vector <std::string> tags;
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
|
||||
if (arg.hasTag ("TAG"))
|
||||
tags.push_back (arg.attribute ("raw"));
|
||||
}
|
||||
|
||||
if (! ids.size ())
|
||||
throw std::string ("IDs must be specified. See 'timew help tag'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
|
|
|
@ -38,20 +38,18 @@ int CmdUntag (
|
|||
Database& database)
|
||||
{
|
||||
// Gather IDs and TAGs.
|
||||
std::vector <int> ids;
|
||||
std::vector <int> ids = cli.getIds();
|
||||
|
||||
if (ids.empty ())
|
||||
throw std::string ("IDs must be specified. See 'timew help untag'.");
|
||||
|
||||
std::vector <std::string> tags;
|
||||
for (auto& arg : cli._args)
|
||||
{
|
||||
if (arg.hasTag ("ID"))
|
||||
ids.push_back (strtol (arg.attribute ("value").c_str (), NULL, 10));
|
||||
|
||||
if (arg.hasTag ("TAG"))
|
||||
tags.push_back (arg.attribute ("raw"));
|
||||
}
|
||||
|
||||
if (! ids.size ())
|
||||
throw std::string ("IDs must be specified. See 'timew help untag'.");
|
||||
|
||||
// Load the data.
|
||||
// Note: There is no filter.
|
||||
Interval filter;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue