diff --git a/src/CLI.cpp b/src/CLI.cpp index f1215309..7921b086 100644 --- a/src/CLI.cpp +++ b/src/CLI.cpp @@ -34,6 +34,7 @@ #include #include #include +#include //////////////////////////////////////////////////////////////////////////////// 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; +} + +//////////////////////////////////////////////////////////////////////////////// diff --git a/src/CLI.h b/src/CLI.h index d75691da..f097ecb3 100644 --- a/src/CLI.h +++ b/src/CLI.h @@ -32,6 +32,7 @@ #include #include #include +#include // Represents a single argument. class A2 @@ -68,6 +69,7 @@ public: std::set getIds () const; std::vector getTags () const; std::string getAnnotation() const; + Duration getDuration() const; std::string dump (const std::string& title = "CLI Parser") const; private: @@ -86,4 +88,3 @@ public: }; #endif - diff --git a/src/commands/CmdLengthen.cpp b/src/commands/CmdLengthen.cpp index 9cff566e..3391c638 100644 --- a/src/commands/CmdLengthen.cpp +++ b/src/commands/CmdLengthen.cpp @@ -49,16 +49,7 @@ int CmdLengthen ( throw std::string ("IDs must be specified. See 'timew help lengthen'."); } - std::string delta; - - for (auto& arg : cli._args) - { - if (arg.hasTag ("FILTER") && - arg._lextype == Lexer::Type::duration) - { - delta = arg.attribute ("raw"); - } - } + Duration dur = cli.getDuration (); journal.startTransaction (); @@ -75,7 +66,6 @@ int CmdLengthen ( database.deleteInterval (interval); - Duration dur (delta); interval.end += dur.toTime_t (); validate (cli, rules, database, interval); database.addInterval (interval, verbose); diff --git a/src/commands/CmdResize.cpp b/src/commands/CmdResize.cpp index 3dda1849..23da05f8 100644 --- a/src/commands/CmdResize.cpp +++ b/src/commands/CmdResize.cpp @@ -48,15 +48,7 @@ int CmdResize ( throw std::string ("IDs must be specified. See 'timew help resize'."); } - std::string delta; - for (auto& arg : cli._args) - { - if (arg.hasTag ("FILTER") && - arg._lextype == Lexer::Type::duration) - { - delta = arg.attribute ("raw"); - } - } + Duration dur = cli.getDuration (); journal.startTransaction (); @@ -70,7 +62,6 @@ int CmdResize ( throw format ("Cannot resize open interval @{1}", interval.id); } - Duration dur (delta); database.deleteInterval (interval); interval.end = interval.start + dur.toTime_t (); diff --git a/src/commands/CmdShorten.cpp b/src/commands/CmdShorten.cpp index 3eac5459..379cf174 100644 --- a/src/commands/CmdShorten.cpp +++ b/src/commands/CmdShorten.cpp @@ -48,15 +48,7 @@ int CmdShorten ( throw std::string ("IDs must be specified. See 'timew help shorten'."); } - std::string delta; - for (auto& arg : cli._args) - { - if (arg.hasTag ("FILTER") && - arg._lextype == Lexer::Type::duration) - { - delta = arg.attribute ("raw"); - } - } + Duration dur = cli.getDuration (); journal.startTransaction (); @@ -71,7 +63,6 @@ int CmdShorten ( throw format ("Cannot shorten open interval @{1}", interval.id); } - Duration dur (delta); if (dur > (interval.end - interval.start)) { throw format ("Cannot shorten interval @{1} by {2} because it is only {3} in length.",