diff --git a/libopenfpga/libopenfpgashell/src/command.cpp b/libopenfpga/libopenfpgashell/src/command.cpp index fb79c5259..8c20ffe5a 100644 --- a/libopenfpga/libopenfpgashell/src/command.cpp +++ b/libopenfpga/libopenfpgashell/src/command.cpp @@ -4,8 +4,8 @@ #include "vtr_assert.h" #include "command.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Public constructors @@ -169,4 +169,4 @@ bool Command::valid_option_id(const CommandOptionId& option_id) const { return ( size_t(option_id) < option_ids_.size() ) && ( option_id == option_ids_[option_id] ); } -} /* End namespace minshell */ +} /* End namespace openfpga */ diff --git a/libopenfpga/libopenfpgashell/src/command.h b/libopenfpga/libopenfpgashell/src/command.h index c4a1b9869..05bf89fed 100644 --- a/libopenfpga/libopenfpgashell/src/command.h +++ b/libopenfpga/libopenfpgashell/src/command.h @@ -23,8 +23,8 @@ #include "vtr_range.h" #include "command_fwd.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Supported date types of value which is followed by a command-line option @@ -139,6 +139,6 @@ class Command { std::map option_short_name2ids_; }; -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/src/command_context.cpp b/libopenfpga/libopenfpgashell/src/command_context.cpp index 30f9992ad..ee7c684d6 100644 --- a/libopenfpga/libopenfpgashell/src/command_context.cpp +++ b/libopenfpga/libopenfpgashell/src/command_context.cpp @@ -4,8 +4,8 @@ #include "vtr_assert.h" #include "command_context.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Public constructor @@ -66,4 +66,4 @@ void CommandContext::set_option_value(const Command& command, option_values_[option_id] = value; } -} /* End namespace minshell */ +} /* End namespace openfpga */ diff --git a/libopenfpga/libopenfpgashell/src/command_context.h b/libopenfpga/libopenfpgashell/src/command_context.h index fb983218e..2300888cf 100644 --- a/libopenfpga/libopenfpgashell/src/command_context.h +++ b/libopenfpga/libopenfpgashell/src/command_context.h @@ -6,8 +6,8 @@ #include "command_fwd.h" #include "command.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Data structure to stores parsing results for a command @@ -54,6 +54,6 @@ class CommandContext { vtr::vector option_values_; }; -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/src/command_echo.cpp b/libopenfpga/libopenfpgashell/src/command_echo.cpp index 78fc9aabb..c9d930dc8 100644 --- a/libopenfpga/libopenfpgashell/src/command_echo.cpp +++ b/libopenfpga/libopenfpgashell/src/command_echo.cpp @@ -6,18 +6,18 @@ #include "vtr_log.h" #include "command_echo.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Print all the options that are defined in an object Command * This function is mainly used to create help desk for a command ********************************************************************/ void print_command_options(const Command& cmd) { - VTR_LOG("Command '%s' usage:\n", - cmd.name().c_str()); + VTR_LOG("Command '%s' usage:\n%lu options available\n", + cmd.name().c_str(), cmd.options().size()); for (const CommandOptionId& opt : cmd.options()) { - VTR_LOG("--%s, -%s : %s\n", + VTR_LOG("\t--%s, -%s : %s\n", cmd.option_name(opt).c_str(), cmd.option_short_name(opt).c_str(), cmd.option_description(opt).c_str()); diff --git a/libopenfpga/libopenfpgashell/src/command_echo.h b/libopenfpga/libopenfpgashell/src/command_echo.h index eb3923100..2d8ce2c66 100644 --- a/libopenfpga/libopenfpgashell/src/command_echo.h +++ b/libopenfpga/libopenfpgashell/src/command_echo.h @@ -11,14 +11,14 @@ * Function declaration *******************************************************************/ -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { void print_command_options(const Command& cmd); void print_command_context(const Command& cmd, const CommandContext& cmd_context); -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/src/command_fwd.h b/libopenfpga/libopenfpgashell/src/command_fwd.h index 52dd81d65..3d59097dc 100644 --- a/libopenfpga/libopenfpgashell/src/command_fwd.h +++ b/libopenfpga/libopenfpgashell/src/command_fwd.h @@ -3,8 +3,8 @@ #include "vtr_strong_id.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * A strong id for the options used by a command @@ -13,6 +13,6 @@ struct command_option_id_tag; typedef vtr::StrongId CommandOptionId; -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/src/command_parser.cpp b/libopenfpga/libopenfpgashell/src/command_parser.cpp index 057660e2e..5e0397421 100644 --- a/libopenfpga/libopenfpgashell/src/command_parser.cpp +++ b/libopenfpga/libopenfpgashell/src/command_parser.cpp @@ -10,8 +10,8 @@ #include "vtr_log.h" #include "command_parser.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /******************************************************************** * Try to find an option in the command and update the CommandContext if needed @@ -25,6 +25,7 @@ CommandOptionId parse_option(const std::string& argv, if (CommandOptionId::INVALID() == option_id) { VTR_LOG("Detect unknown option '--%s'!\n", argv.c_str()); + return CommandOptionId::INVALID(); } /* Found, update the CommandContext */ cmd_context.set_option(cmd, option_id, true); @@ -45,6 +46,7 @@ CommandOptionId parse_short_option(const std::string& argv, if (CommandOptionId::INVALID() == option_id) { VTR_LOG("Detect unknown short option '-%s'!\n", argv.c_str()); + return CommandOptionId::INVALID(); } /* Found, update the CommandContext */ cmd_context.set_option(cmd, option_id, true); @@ -160,4 +162,4 @@ bool parse_command(const std::vector& argv, return true; } -} /* End namespace minshell */ +} /* End namespace openfpga */ diff --git a/libopenfpga/libopenfpgashell/src/command_parser.h b/libopenfpga/libopenfpgashell/src/command_parser.h index 6aee4291b..568bb7ffc 100644 --- a/libopenfpga/libopenfpgashell/src/command_parser.h +++ b/libopenfpga/libopenfpgashell/src/command_parser.h @@ -12,13 +12,13 @@ * Function declaration *******************************************************************/ -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { bool parse_command(const std::vector& argv, const Command& cmd, CommandContext& cmd_context); -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/src/shell.h b/libopenfpga/libopenfpgashell/src/shell.h index 2c64381a4..0f83b4e46 100644 --- a/libopenfpga/libopenfpgashell/src/shell.h +++ b/libopenfpga/libopenfpgashell/src/shell.h @@ -12,8 +12,8 @@ #include "command_context.h" #include "shell_fwd.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Data structure to define a working environment of a mini shell @@ -171,7 +171,7 @@ class Shell { vtr::vector> commands_by_classes_; }; -} /* End namespace minshell */ +} /* End namespace openfpga */ /* Include the template implementation functions in the header file */ #include "shell.tpp" diff --git a/libopenfpga/libopenfpgashell/src/shell.tpp b/libopenfpga/libopenfpgashell/src/shell.tpp index 809c3c59a..00b70ffed 100644 --- a/libopenfpga/libopenfpgashell/src/shell.tpp +++ b/libopenfpga/libopenfpgashell/src/shell.tpp @@ -19,8 +19,8 @@ #include "command_parser.h" #include "command_echo.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * Public constructors @@ -380,4 +380,4 @@ bool Shell::valid_command_class_id(const ShellCommandClassId& cmd_class_id) c return ( size_t(cmd_class_id) < command_class_ids_.size() ) && ( cmd_class_id == command_class_ids_[cmd_class_id] ); } -} /* End namespace minshell */ +} /* End namespace openfpga */ diff --git a/libopenfpga/libopenfpgashell/src/shell_fwd.h b/libopenfpga/libopenfpgashell/src/shell_fwd.h index cea5c70e2..5a1557210 100644 --- a/libopenfpga/libopenfpgashell/src/shell_fwd.h +++ b/libopenfpga/libopenfpgashell/src/shell_fwd.h @@ -3,8 +3,8 @@ #include "vtr_strong_id.h" -/* Begin namespace minishell */ -namespace minishell { +/* Begin namespace openfpga */ +namespace openfpga { /********************************************************************* * A strong id for the options used by a command @@ -15,6 +15,6 @@ struct shell_command_class_id_tag; typedef vtr::StrongId ShellCommandId; typedef vtr::StrongId ShellCommandClassId; -} /* End namespace minshell */ +} /* End namespace openfpga */ #endif diff --git a/libopenfpga/libopenfpgashell/test/test_command_parser.cpp b/libopenfpga/libopenfpgashell/test/test_command_parser.cpp index 2d18765a7..c3e004137 100644 --- a/libopenfpga/libopenfpgashell/test/test_command_parser.cpp +++ b/libopenfpga/libopenfpgashell/test/test_command_parser.cpp @@ -4,7 +4,7 @@ #include "command_parser.h" #include "command_echo.h" -using namespace minishell; +using namespace openfpga; int main(int argc, char** argv) { /* Create a new command */ diff --git a/libopenfpga/libopenfpgashell/test/test_shell.cpp b/libopenfpga/libopenfpgashell/test/test_shell.cpp index 24099fc9b..8fc00f055 100644 --- a/libopenfpga/libopenfpgashell/test/test_shell.cpp +++ b/libopenfpga/libopenfpgashell/test/test_shell.cpp @@ -7,11 +7,26 @@ #include "command_echo.h" #include "shell.h" -using namespace minishell; +using namespace openfpga; class ShellContext { + public: + int a; }; +static +void shell_execute_set(ShellContext& context, + const Command& cmd, const CommandContext& cmd_context) { + CommandOptionId opt_id = cmd.option("value"); + /* Get the value of a in the command context */ + context.a = std::atoi(cmd_context.option_value(cmd, opt_id).c_str()); +} + +static +void shell_execute_print(ShellContext& context) { + VTR_LOG("a=%d\n", context.a); +} + int main(int argc, char** argv) { /* Create the command to launch shell in different modes */ Command start_cmd("test_shell"); @@ -61,6 +76,27 @@ int main(int argc, char** argv) { shell.add_title(shell_title.c_str()); + /* Add a new class of commands */ + ShellCommandClassId arith_cmd_class = shell.add_command_class("Arithmetic"); + + /* Create a command of 'set' with a required option 'value' with a value + * This function sets a value to an internal variable of ShellContext + */ + Command shell_cmd_set("set"); + CommandOptionId set_opt_value = shell_cmd_set.add_option("value", true, "value of variable"); + shell_cmd_set.set_option_require_value(set_opt_value, OPT_STRING); + ShellCommandId shell_cmd_set_id = shell.add_command(shell_cmd_set, "Set a value to internal variable 'a'"); + shell.set_command_class(shell_cmd_set_id, arith_cmd_class); + shell.set_command_execute_function(shell_cmd_set_id, shell_execute_set); + + /* Create a command of 'print' + * This function will print the value of an internal variable of ShellContext + */ + Command shell_cmd_print("print"); + ShellCommandId shell_cmd_print_id = shell.add_command(shell_cmd_print, "Print the value of internal variable 'a'"); + shell.set_command_class(shell_cmd_print_id, arith_cmd_class); + shell.set_command_execute_function(shell_cmd_print_id, shell_execute_print); + /* Add a new class of commands */ ShellCommandClassId basic_cmd_class = shell.add_command_class("Basic");