[go: up one dir, main page]

Skip to content

Commit

Permalink
Merge pull request #465 from Hind-M/magics_help_message
Browse files Browse the repository at this point in the history
Add custom `help` for magic commands
  • Loading branch information
JohanMabille authored Dec 5, 2022
2 parents 516aa7e + 71b7b3a commit 79d2bb8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion include/xeus-cling/xmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ namespace xcpp
{
if (cell.empty())
{
std::cerr << "UsageError: %%" << magic_name << " is a cell magic, but the cell body is empty.";
std::cerr << "UsageError: %%" << magic_name << " is a cell magic, but the cell body is empty." << std::endl;
std::cerr << "If you only intend to display %%" << magic_name << " help, please use a double line break to fill in the cell body.";
if (contains(magic_name, xmagic_type::line))
{
std::cerr << " Did you mean the line magic %" << magic_name << " (single %)?";
Expand Down
2 changes: 2 additions & 0 deletions include/xeus-cling/xoptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include <argparse/argparse.hpp>

#include "xeus-cling/xeus_cling_config.hpp"

namespace xcpp
{
struct argparser : public argparse::ArgumentParser
Expand Down
12 changes: 11 additions & 1 deletion src/xmagics/executable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ namespace xcpp
{
argparser executable::get_options()
{
argparser argpars("executable");
argparser argpars("executable", XEUS_CLING_VERSION, argparse::default_arguments::none);
argpars.add_description("write executable");
argpars.add_argument("filename")
.help("filename")
.required();
argpars.add_argument("options")
.help("options")
.default_value<std::vector<std::string>>({ "" });
// Add custom help (does not call `exit` avoiding to restart the kernel)
argpars.add_argument("-h", "--help")
.action([&](const std::string & /*unused*/)
{
std::cout << argpars.help().str();
})
.default_value(false)
.help("shows help message")
.implicit_value(true)
.nargs(0);
return argpars;
}

Expand Down
14 changes: 12 additions & 2 deletions src/xmagics/execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace xcpp

argparser timeit::get_options()
{
argparser argpars("timeit");
argparser argpars("timeit", XEUS_CLING_VERSION, argparse::default_arguments::none);
argpars.add_description("Time execution of a C++ statement or expression");
argpars.add_argument("-n", "--number")
.help("execute the given statement n times in a loop. If this value is not given, a fitting value is chosen")
Expand All @@ -52,6 +52,16 @@ namespace xcpp
argpars.add_argument("expression")
.help("expression to be evaluated")
.remaining();
// Add custom help (does not call `exit` avoiding to restart the kernel)
argpars.add_argument("-h", "--help")
.action([&](const std::string & /*unused*/)
{
std::cout << argpars.help().str();
})
.default_value(false)
.help("shows help message")
.implicit_value(true)
.nargs(0);
return argpars;
}

Expand Down Expand Up @@ -112,7 +122,7 @@ namespace xcpp
}
catch (std::logic_error& e)
{
if (trim(cell).empty())
if (trim(cell).empty() && (argpars["-h"] == false))
{
std::cerr << "No expression given to evaluate" << std::endl;
}
Expand Down
12 changes: 11 additions & 1 deletion src/xmagics/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace xcpp
{
argparser writefile::get_options()
{
argparser argpars("file");
argparser argpars("file", XEUS_CLING_VERSION, argparse::default_arguments::none);
argpars.add_description("write file");
argpars.add_argument("-a", "--append")
.help("append")
Expand All @@ -31,6 +31,16 @@ namespace xcpp
argpars.add_argument("filename")
.help("filename")
.required();
// Add custom help (does not call `exit` avoiding to restart the kernel)
argpars.add_argument("-h", "--help")
.action([&](const std::string & /*unused*/)
{
std::cout << argpars.help().str();
})
.default_value(false)
.help("shows help message")
.implicit_value(true)
.nargs(0);
return argpars;
}

Expand Down

0 comments on commit 79d2bb8

Please sign in to comment.