Skip to content

Commit

Permalink
Add "echo" command
Browse files Browse the repository at this point in the history
  • Loading branch information
Serious-senpai committed Mar 10, 2024
1 parent b2a1429 commit dd15163
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/commands/echo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "echo.hpp"

int main(int argc, const char **argv)
{
auto command = EchoCommand();
return command.run(argc, argv);
}
32 changes: 32 additions & 0 deletions src/commands/echo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <base.hpp>
#include <standard.hpp>

class EchoCommand : public BaseCommand
{
public:
using BaseCommand::run;

EchoCommand() : BaseCommand("echo") {}

bool accept_any_arguments() override
{
return true;
}

int run(const ParseResult &arguments) override
{
if (arguments.original.size() > 1) // Skip first argument
{
for (unsigned i = 1; i < arguments.original.size() - 1; i++)
{
std::cout << arguments.original[i] << " ";
}
std::cout << arguments.original.back();
}

std::cout << std::endl;
return 0;
}
};
2 changes: 1 addition & 1 deletion src/include/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class BaseCommand
}
}

return ParseResult(groups, results, positional_arguments);
return ParseResult(groups, results, positional_arguments, args);
}

virtual void add_argument(const std::initializer_list<std::string> &names) final
Expand Down
6 changes: 4 additions & 2 deletions src/include/parse_results.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ class ParseResult
}

public:
const std::vector<std::string> positional_arguments;
const std::vector<std::string> positional_arguments, original;
const std::vector<std::set<std::string>> alias_groups;

ParseResult(
const std::vector<std::vector<std::string>> &groups,
const std::map<std::string, unsigned> &results,
const std::vector<std::string> positional_arguments)
const std::vector<std::string> &positional_arguments,
const std::vector<std::string> &original)
: groups(groups),
results(results),
positional_arguments(positional_arguments),
original(original),
alias_groups(get_alias_group(groups, results)) {}

const std::vector<std::string> &get(const std::string &name) const
Expand Down
11 changes: 9 additions & 2 deletions src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <strip.hpp>

#include "commands/args.hpp"
#include "commands/echo.hpp"
#include "commands/exit.hpp"
#include "commands/type.hpp"

Expand Down Expand Up @@ -34,19 +35,25 @@ int main()
{
std::vector<CommandInvoker<BaseCommand>> commands;
commands.emplace_back(std::make_shared<ArgsCommand>());
commands.emplace_back(std::make_shared<EchoCommand>());
commands.emplace_back(std::make_shared<ExitCommand>());
commands.emplace_back(std::make_shared<TypeCommand>());

int errorlevel = 0;
std::cout << title << std::endl;
while (true)
{
std::cout << std::endl
<< "liteshell(" << errorlevel << ")~" << get_working_directory() << ">";
std::cout << "\nliteshell(" << errorlevel << ")~" << get_working_directory() << ">";
std::cout.flush();

std::string input;
std::getline(std::cin, input);

if (input.empty())
{
continue;
}

auto arguments = split(strip(input));

std::vector<CommandInvoker<BaseCommand>> matched;
Expand Down

0 comments on commit dd15163

Please sign in to comment.