BashSpark
Loading...
Searching...
No Matches
command.h
Go to the documentation of this file.
1
33#pragma once
34
35#include <span>
36
38
39namespace bs {
40 class shell;
41
50 class command {
51 friend class shell;
52
53 public:
58 explicit command(std::string sName)
59 : m_sName(std::move(sName)) {
60 }
61
62 public:
66 virtual ~command() = default;
67
68 public:
76 [[nodiscard]] virtual shell_status run(
77 const std::span<const std::string> &vArgs,
78 shell_session &oSession
79 ) const = 0;
80
81 public:
86 [[nodiscard]] std::string get_name() const {
87 return this->m_sName;
88 }
89
94 [[nodiscard]] const std::string &get_name_ref() const noexcept {
95 return this->m_sName;
96 }
97
98 private:
100 std::string m_sName;
101 };
102
111 class command_echo : public command {
112 public:
117 : command("echo") {
118 }
119
120 public:
133 [[nodiscard]] shell_status run(
134 const std::span<const std::string> &vArgs,
135 shell_session &oSession
136 ) const override;
137 };
138
146 class command_eval : public command {
147 public:
151 explicit command_eval()
152 : command("eval") {
153 }
154
155 public:
164 [[nodiscard]] shell_status run(
165 const std::span<const std::string> &vArgs,
166 shell_session &oSession
167 ) const override;
168
169 protected:
174 virtual void msg_error_max_depth_reached(std::ostream &oStdErr) const;
175 };
176}
Built-in command that prints all arguments to stdout.
Definition command.h:111
command_echo()
Constructs the command.
Definition command.h:116
shell_status run(const std::span< const std::string > &vArgs, shell_session &oSession) const override
Prints all arguments to stdout.
Definition command.cpp:50
Built-in command that evaluates its arguments as a shell command.
Definition command.h:146
virtual void msg_error_max_depth_reached(std::ostream &oStdErr) const
Print an error if the depth limit of the shell is surpassed.
Definition command.cpp:114
command_eval()
Constructs the command.
Definition command.h:151
shell_status run(const std::span< const std::string > &vArgs, shell_session &oSession) const override
Execute the provided arguments as if typed in shell. The corresponding status code is returned.
Definition command.cpp:87
Abstract base class for all shell commands.
Definition command.h:50
command(std::string sName)
Construct a command with a given name.
Definition command.h:58
const std::string & get_name_ref() const noexcept
Get the command name (reference).
Definition command.h:94
virtual shell_status run(const std::span< const std::string > &vArgs, shell_session &oSession) const =0
Execute the command.
std::string get_name() const
Get the command name (copy).
Definition command.h:86
virtual ~command()=default
Virtual destructor.
Represents an execution environment for a shell instance.
Definition shell_session.h:57
A simplified shell environment inspired by Bash.
Definition shell.h:63
BashSpark main namespace.
Definition command.h:39
shell_status
Shell status codes.
Definition shell_status.h:47
Defines the class bs::shell_session.