-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "cmd-args.h" | ||
#include "directory.h" | ||
|
||
static bool isCmdArg(const std::string& arg) | ||
{ | ||
return arg.substr(0, 2) == "--"; | ||
} | ||
|
||
CmdArgs::CmdArgs(int argc, char* argv[]) : _path(Directory::cwd()), _watch(false) | ||
{ | ||
for (int i=1; i<argc; ++i) | ||
{ | ||
std::string arg(argv[i]); | ||
|
||
if (!isCmdArg(arg)) | ||
this->_path = Directory::absolute(arg); | ||
else | ||
parseArg(arg); | ||
} | ||
} | ||
|
||
void CmdArgs::parseArg(const std::string& arg) | ||
{ | ||
if (arg == "--watchAll") | ||
this->_watch = true; | ||
} | ||
|
||
const std::string& CmdArgs::path() | ||
{ | ||
return this->_path; | ||
} | ||
|
||
bool CmdArgs::watch() | ||
{ | ||
return this->_watch; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include <string> | ||
|
||
class CmdArgs | ||
{ | ||
public: | ||
CmdArgs(int argc, char* argv[]); | ||
const std::string& path(); | ||
bool watch(); | ||
|
||
private: | ||
void parseArg(const std::string& arg); | ||
std::string _path; | ||
bool _watch; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <cest> | ||
#include "../directory.h" | ||
#include "../cmd-args.h" | ||
|
||
describe("CmdArgs", []() { | ||
describe("path()", []() { | ||
it("defaults to CWD when path is not present in args", []() { | ||
std::string cmd_path = "/bin/test"; | ||
std::array<char *, 1> argv = { (char *)cmd_path.c_str() }; | ||
|
||
auto cmd_args = CmdArgs(argv.size(), argv.data()); | ||
|
||
expect(cmd_args.path()).toEqual(Directory::cwd()); | ||
}); | ||
|
||
it("assumes non-first argument not starting with -- to be the path", []() { | ||
std::string cmd_path = "/bin/test"; | ||
std::string watch_mode = "--watchAll"; | ||
std::string path = "/home/tests"; | ||
std::array<char *, 3> argv = { | ||
(char *)cmd_path.c_str(), | ||
(char *)watch_mode.c_str(), | ||
(char *)path.c_str() | ||
}; | ||
|
||
auto cmd_args = CmdArgs(argv.size(), argv.data()); | ||
|
||
expect(cmd_args.path()).toEqual("/home/tests"); | ||
}); | ||
}); | ||
|
||
describe("watch()", []() { | ||
it("defaults to false", []() { | ||
std::string cmd_path = "/bin/test"; | ||
std::array<char *, 1> argv = { (char *)cmd_path.c_str() }; | ||
|
||
auto cmd_args = CmdArgs(argv.size(), argv.data()); | ||
|
||
expect(cmd_args.watch()).toBeFalsy(); | ||
}); | ||
|
||
it("sets to true when --watchAll is passed", []() { | ||
std::string cmd_path = "/bin/test"; | ||
std::string watch = "--watchAll"; | ||
std::array<char *, 2> argv = { (char *)cmd_path.c_str(), (char *)watch.c_str() }; | ||
|
||
auto cmd_args = CmdArgs(argv.size(), argv.data()); | ||
|
||
expect(cmd_args.watch()).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters