Skip to content

Commit

Permalink
Implement handlers and organise commands lexicographically
Browse files Browse the repository at this point in the history
  • Loading branch information
Coal committed Feb 20, 2022
1 parent 1b5b701 commit 37822fc
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 245 deletions.
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ set_target_properties(lpm-cli PROPERTIES LINKER_LANGUAGE CXX OUTPUT_NAME lpm)
# Include the framework
add_subdirectory(src/library)

target_link_libraries(lpm-cli PRIVATE lpm-lib)
# Include source files
add_library(lpm-cli-src
./src/command.cpp
)

# Include command handlers
add_library(lpm-handlers
./src/handlers/install.cpp
)

target_link_libraries(lpm-cli PRIVATE lpm-lib lpm-cli-src lpm-handlers)
12 changes: 7 additions & 5 deletions src/argparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace ArgParser {
// Check if the arguments provided and the command are valid.
bool validate(std::string& command, args_t& args, const args_map_t& valid_args) {
for (auto& valid_arg_pair : valid_args) {
Command::Type validate(std::string& command, args_t& args, const args_map_t& valid_args) {
for (int index = 0; auto& valid_arg_pair : valid_args) {
// Check if the command provided is valid.
if (valid_arg_pair.first == command) {
auto& valid_values = valid_arg_pair.second;
Expand All @@ -24,16 +24,18 @@ namespace ArgParser {
) == valid_values.end()
) {
LPM_PRINT_ERROR("Unrecognized parameter: " << user_arg.first);
return false;
return Command::Type::UNKNOWN;
}
}

return true;
return (Command::Type) index;
}

++index;
}

LPM_PRINT_ERROR("Unrecognized command: " << command);
return false;
return Command::Type::UNKNOWN;
}

args_t parse(int& argc, char* argv[], int start_index) {
Expand Down
42 changes: 21 additions & 21 deletions src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
#include "lpm/types.h"

args_map_t const LPM_VALID_ARGS {
{"add", {
"package_name",
"global",
"version",
"lua-version"
}},
{"audit", {
"recursive"
}},
{"help", {""}},
{"init", {
"name",
Expand All @@ -17,44 +26,35 @@ args_map_t const LPM_VALID_ARGS {
{"install", {
"recursive"
}},
{"add", {
"package_name",
"global",
"version",
"lua-version"
{"list", {
"global"
}},
{"run", {
"package_name",
"lua-version"
{"purge", {
"recursive"
}},
{"remove", {
"global",
"version",
"lua-version"
}},
{"update", {
{"repository", {
"action"
}},
{"run", {
"package_name",
"global"
"lua-version"
}},
{"list", {
"global"
}},
{"search", {
"package_name"
}},
{"repository", {
"action"
}},
{"audit", {
"recursive"
}},
{"show", {
"package_name",
"global",
"version"
}},
{"purge", {
"recursive"
{"update", {
"package_name",
"global"
"lua-version"
}}
};
1 change: 1 addition & 0 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "lpm/macros.h"
#include "command.h"
#include "errorhandling.h"
#include "handlers/handlers.h"

bool Command::execute(Type command_type, args_t& args) {
try {
Expand Down
28 changes: 15 additions & 13 deletions src/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@

namespace Command {
enum Type {
HELP,
INIT,
INSTALL,
ADD,
RUN,
REMOVE,
UPDATE,
LIST,
SEARCH,
REPOSITORY,
AUDIT,
SHOW,
PURGE
ADD = 0,
AUDIT = 1,
HELP = 2,
INIT = 3,
INSTALL = 4,
LIST = 5,
PURGE = 6,
REMOVE = 7,
REPOSITORY = 8,
RUN = 9,
SEARCH = 10,
SHOW = 11,
UPDATE = 12,

UNKNOWN = -1
};

// Pass a command to its respective handler.
Expand Down
20 changes: 15 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
#include <algorithm>
#include "lpm/types.h"
#include "lpm/macros.h"
#include "command.h"
#include "args.h"
#include "argparser.h"
#include "static/messages.h"


int main(int argc, char *argv[]) {
if (argc < 2) {
LPM_PRINT(MESSAGES_HELP);
LPM_PRINT(MESSAGES_HELP << "\n");
return 1;
}

Expand All @@ -25,17 +26,26 @@ int main(int argc, char *argv[]) {
command == "show"
) {
if (argc < 3) {
LPM_PRINT_ERROR("Missing argument: package_name");
LPM_PRINT(MESSAGES_HELP);
LPM_PRINT_ERROR("Missing argument: package_name" << "\n");
LPM_PRINT(MESSAGES_HELP << "\n");

return 1;
}

args["package_name"] = argv[2];
}

if (!ArgParser::validate(command, args, LPM_VALID_ARGS)) {
LPM_PRINT(MESSAGES_HELP);
Command::Type command_type = ArgParser::validate(command, args, LPM_VALID_ARGS);

if (
command_type == Command::Type::UNKNOWN ||
command_type == Command::Type::HELP
) {
LPM_PRINT(MESSAGES_HELP << "\n");
return command_type < 0 ? 1 : 0;
}

if (!Command::execute(command_type, args)) {
return 1;
}
}
48 changes: 24 additions & 24 deletions src/static/MESSAGES_HELP
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
Usage: lpm <command> [... args]
├── add: Adds a new package
├── audit: Checks the integrity of all installed packages
├── help: Displays this help message
├── init: Iniializes a packages.toml with basic information
├── install: Finds and installs a packages.toml
├── add: Adds a new package
├── run: Runs a package
├── purge: Uninstalls lua_modules
├── remove: Removes a module
├── audit: Checks the integrity of all installed packages
├── run: Runs a package
├── show: Displays information about a package
├── purge: Uninstalls lua_modules
└── help: Displays this help message
└── update: Updates a package

Options:
add <package_name>: Installs a package
--global [-g]: Install package globally.
--version: Install specific version of this package. Defaults to latest version.
--lua-version <lua-version>: Installs the package for this lua version.

audit: Checks the integrity of all installed packages in the current directory.
--recursive [-r]: Does the above but recursively, starting at current directory.

help [--help, -h]: Displays this help message

init: Initializes a packages.toml file.
Expand All @@ -26,38 +35,29 @@ Options:
install: Finds a packages.toml file and installs all modules in it.
--recursive [-r]: Installs all packages.toml files recursively, starting at the current directory.

add <package_name>: Installs a package
--global [-g]: Install package globally.
--version: Install specific version of this package. Defaults to latest version.
--lua-version <lua-version>: Installs the package for this lua version.
list: Lists all installed packages
--global [-g]: Lists all globally installed packages.

run <package_name | path/to/package>: Runs a package's entry point, if runnable
--lua-version <lua-version>: Runs the package for this lua version.
Cant be used if path/to/package is specified.
purge: Uninstalls all lua_modules
--recursive [-r]: Uninstalls all lua_modules recursively, starting at current directory.

remove <package_name>: Removes a package
--global [-g]: Removes package globally.

update <package_name>: Updates a package
--global [-g]: Updates package globally.

list: Lists all installed packages
--global [-g]: Lists all globally installed packages.

search <package_name>: Searches for a package in all available repositories

repository <action>: Manages repositories
add <repository>: Adds a repository
remove <repository>: Removes a repository
list: Lists all available repositories
update: Updates all repositories

audit: Checks the integrity of all installed packages in the current directory.
--recursive [-r]: Does the above but recursively, starting at current directory.
run <package_name | path/to/package>: Runs a package's entry point, if runnable
--lua-version <lua-version>: Runs the package for this lua version.
Cant be used if path/to/package is specified.
search <package_name>: Searches for a package in all available repositories

show <package_name>: Displays information about a package
--global [-g]: Displays information about a package globally.
--version: Displays information about a specific version of this package.

purge: Uninstalls all lua_modules
--recursive [-r]: Uninstalls all lua_modules recursively, starting at current directory.
update <package_name>: Updates a package
--global [-g]: Updates package globally.
Loading

0 comments on commit 37822fc

Please sign in to comment.