Extract function CLI::getDuration ()

Signed-off-by: Thomas Lauf <thomas.lauf@tngtech.com>
This commit is contained in:
Thomas Lauf 2020-02-29 16:08:42 +01:00
parent 0c1d7291e2
commit e8511c5efd
5 changed files with 22 additions and 32 deletions

View file

@ -34,6 +34,7 @@
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <set> #include <set>
#include <Duration.h>
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
A2::A2 (const std::string& raw, Lexer::Type lextype) A2::A2 (const std::string& raw, Lexer::Type lextype)
@ -589,3 +590,19 @@ std::string CLI::getAnnotation () const
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
Duration CLI::getDuration () const
{
std::string delta;
for (auto& arg : _args)
{
if (arg.hasTag ("FILTER") &&
arg._lextype == Lexer::Type::duration)
{
delta = arg.attribute ("raw");
}
}
Duration dur (delta);
return dur;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -32,6 +32,7 @@
#include <set> #include <set>
#include <vector> #include <vector>
#include <map> #include <map>
#include <Duration.h>
// Represents a single argument. // Represents a single argument.
class A2 class A2
@ -68,6 +69,7 @@ public:
std::set <int> getIds () const; std::set <int> getIds () const;
std::vector<std::string> getTags () const; std::vector<std::string> getTags () const;
std::string getAnnotation() const; std::string getAnnotation() const;
Duration getDuration() const;
std::string dump (const std::string& title = "CLI Parser") const; std::string dump (const std::string& title = "CLI Parser") const;
private: private:
@ -86,4 +88,3 @@ public:
}; };
#endif #endif

View file

@ -49,16 +49,7 @@ int CmdLengthen (
throw std::string ("IDs must be specified. See 'timew help lengthen'."); throw std::string ("IDs must be specified. See 'timew help lengthen'.");
} }
std::string delta; Duration dur = cli.getDuration ();
for (auto& arg : cli._args)
{
if (arg.hasTag ("FILTER") &&
arg._lextype == Lexer::Type::duration)
{
delta = arg.attribute ("raw");
}
}
journal.startTransaction (); journal.startTransaction ();
@ -75,7 +66,6 @@ int CmdLengthen (
database.deleteInterval (interval); database.deleteInterval (interval);
Duration dur (delta);
interval.end += dur.toTime_t (); interval.end += dur.toTime_t ();
validate (cli, rules, database, interval); validate (cli, rules, database, interval);
database.addInterval (interval, verbose); database.addInterval (interval, verbose);

View file

@ -48,15 +48,7 @@ int CmdResize (
throw std::string ("IDs must be specified. See 'timew help resize'."); throw std::string ("IDs must be specified. See 'timew help resize'.");
} }
std::string delta; Duration dur = cli.getDuration ();
for (auto& arg : cli._args)
{
if (arg.hasTag ("FILTER") &&
arg._lextype == Lexer::Type::duration)
{
delta = arg.attribute ("raw");
}
}
journal.startTransaction (); journal.startTransaction ();
@ -70,7 +62,6 @@ int CmdResize (
throw format ("Cannot resize open interval @{1}", interval.id); throw format ("Cannot resize open interval @{1}", interval.id);
} }
Duration dur (delta);
database.deleteInterval (interval); database.deleteInterval (interval);
interval.end = interval.start + dur.toTime_t (); interval.end = interval.start + dur.toTime_t ();

View file

@ -48,15 +48,7 @@ int CmdShorten (
throw std::string ("IDs must be specified. See 'timew help shorten'."); throw std::string ("IDs must be specified. See 'timew help shorten'.");
} }
std::string delta; Duration dur = cli.getDuration ();
for (auto& arg : cli._args)
{
if (arg.hasTag ("FILTER") &&
arg._lextype == Lexer::Type::duration)
{
delta = arg.attribute ("raw");
}
}
journal.startTransaction (); journal.startTransaction ();
@ -71,7 +63,6 @@ int CmdShorten (
throw format ("Cannot shorten open interval @{1}", interval.id); throw format ("Cannot shorten open interval @{1}", interval.id);
} }
Duration dur (delta);
if (dur > (interval.end - interval.start)) if (dur > (interval.end - interval.start))
{ {
throw format ("Cannot shorten interval @{1} by {2} because it is only {3} in length.", throw format ("Cannot shorten interval @{1} by {2} because it is only {3} in length.",