Skip to content

Commit

Permalink
generate man page and install it
Browse files Browse the repository at this point in the history
  • Loading branch information
SGSSGene committed Sep 28, 2018
1 parent f5e7ae5 commit 16b20f0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PREFIX ?=
INSTALL_BIN_DIR ?= $(PREFIX)/usr/bin
BASH_COMPLETION_DIR ?= $(PREFIX)/usr/share/bash-completion/completions/
ZSH_COMPLETION_DIR ?= $(PREFIX)/usr/share/zsh-completion/completions/
MAN_DIR ?= $(PREFIX)/usr/share/man/man1/

SRC_FOLDERS = src/
LIBS = c pthread stdc++fs
Expand Down Expand Up @@ -100,6 +101,8 @@ install: $(TARGET)
$(SILENT) cp scripts/bash_completion $(BASH_COMPLETION_DIR)/$<
$(SILENT) mkdir -p $(ZSH_COMPLETION_DIR)
$(SILENT) cp scripts/zsh_completion $(ZSH_COMPLETION_DIR)/$<
$(SILENT) mkdir -p $(MAN_DIR)
$(SILENT) scripts/generateMan.sh | gzip > $(MAN_DIR)/$<.1.gz

$(TARGET): $(CPP_OBJ_FILES) $(C_OBJ_FILES)
@echo linking $(TARGET)
Expand Down
59 changes: 59 additions & 0 deletions scripts/generateMan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

echo "
.\" Manpage for inspexel
.\" Contact info@gottliebtfreitag.de
"

./inspexel --groff | head -n 1

echo "
.SH NAME
inspexel - inspecting motors using the dynamixel protocol
.SH SYNOPSIS
inspexel <COMMAND> [OPTION]...
.SH DESCRIPTION
inspexel is a program to inspect connected motors that uses the dynamixel protocol. It supports protocol v1 and v2.
"
./inspexel --groff | tail -n +2

echo ".SH EXAMPLE
.SH
$ inspexel
.TP
detects all connected motors
.SH
$ inspexel detect --read_all
.TP
detects all connected motors and gives verbose information about them
.SH
$ inspexel meta
.TP
list all supported motor types
.SH
$ inspexel reboot --id 3
.TP
reboots motor with id 3
.SH
$ inspexel set_register --register 0x44 --values 1 --id 0x03
.TP
set register 0x44 of motor 3 to value 1
"

echo "
.SH BUGS
No known bugs.
.SH AUTHOR
Developed by Lutz Freitag <lutz@gottliebtfreitag.de> and Simon Gene Gottlieb <simon@gottliebtfreitag.de>
"

echo ".SH LICENSE"
cat LICENSE
8 changes: 4 additions & 4 deletions src/globalOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ auto listTypicalBaudrates(std::vector<std::string> const& _str) -> std::pair<boo

auto getDefaultSerialPort() -> std::string;

inline auto g_device = parameter::Parameter<std::string>(getDefaultSerialPort(), "device", "the usb2dynamixel device", {}, &listDeviceFiles);
inline auto g_id = parameter::Parameter<int>(0, "id", "the target Id");
inline auto g_baudrate = parameter::Parameter<int>(1000000, "baudrate", "baudrate to use", {}, &listTypicalBaudrates);
inline auto g_device = parameter::Parameter<std::string>(getDefaultSerialPort(), "device", "the usb2dynamixel device (e.g.: /dev/ttyUSB0)", {}, &listDeviceFiles);
inline auto g_id = parameter::Parameter<int>(0, "id", "the target Id (values: 0x00 - 0xfd)");
inline auto g_baudrate = parameter::Parameter<int>(1000000, "baudrate", "baudrate to use (e.g.: 1m)", {}, &listTypicalBaudrates);
inline auto g_timeout = parameter::Parameter<int>(10000, "timeout", "timeout in us");
inline auto g_protocolVersion = parameter::Parameter<int>(1, "protocol_version", "the dynamixel protocol version");
inline auto g_protocolVersion = parameter::Parameter<int>(1, "protocol_version", "the dynamixel protocol version (values: 1, 2)");
14 changes: 11 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace {

auto printHelp = parameter::Parameter<std::optional<std::string>>{{}, "help", "print this help add a string which will be used in a grep-like search through the parameters"};
auto printHelp = parameter::Parameter<std::optional<std::string>>{{}, "help", "print this help add a string which will be used in a grep-like search through the parameters"};

}

#define VERSION "1.1.0"
#define DATE "28 September 2018"

#define TERM_RED "\033[31m"
#define TERM_RESET "\033[0m"

Expand All @@ -20,16 +23,21 @@ int main(int argc, char** argv)
}
return 0;
}
if (argc == 2 and std::string(argv[1]) == "--groff") {
std::cout << ".TH man 1 \"" << DATE << "\" \"" << VERSION << "\" inspexel man page\"\n";
std::cout << parameter::generateGroffString();
return 0;
}

try {
// pass everything except the name of the application
parameter::parseArguments(argc-1, argv+1);

if (printHelp.isSpecified()) {
std::cout << "inspexel version 1.1.0\n";
std::cout << "inspexel version " << VERSION << " - " << DATE << "\n";
std::cout << parameter::generateHelpString(std::regex{".*" + printHelp.get().value_or("") + ".*"});
return 0;
}

parameter::callCommands();
} catch (std::exception const& e) {
std::cerr << "exception: " << TERM_RED << e.what() << TERM_RESET "\n";
Expand Down
45 changes: 45 additions & 0 deletions src/parameter/ArgumentParsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,51 @@ std::string generateHelpString(std::regex const& filter) {
return helpString;
}

std::string generateGroffString() {
std::string helpString;

auto const & commands = detail::CommandRegistry::getInstance().getCommands();

if (commands.size() != 1) { // if there is more than just the default command

helpString += ".SH COMMANDS\n";

for (auto it = commands.begin(); it != commands.end(); it = commands.upper_bound(it->first)) {
if (it->second != &Command::getDefaultCommand()) {
helpString += ".TP\n\\fB" + it->first + "\\fR\n" + it->second->getDescription() + "\n";
}
}
helpString += "\n";
}
bool lastLocal{false};
for (auto it = commands.begin(); it != commands.end();) {
bool globalOptions = it->second == &Command::getDefaultCommand();
if (globalOptions) {
helpString += ".SH GLOBAL OPTIONS\n";
}
if (not lastLocal and not globalOptions) {
lastLocal = true;
helpString += ".SH SPECIFIC OPTIONS\n";
}

auto end = commands.upper_bound(it->first);
for (auto command = it; command != end; ++command) {
if (not globalOptions) {
helpString += ".SS\n\\fB" + it->first + "\\fR\n";
}

auto const& params = command->second->getParameters();
for (auto paramIt = params.begin(); paramIt != params.end(); paramIt = params.upper_bound(paramIt->first)) {
helpString += ".TP\n";
helpString += std::string{"\\fB--"} + paramIt->second->getArgName() + "\\fR\n";
helpString += paramIt->second->describe() + "\n";
}
}
it = end;
}
return helpString;
}

std::set<std::string> getNextArgHint(int argc, char const* const* argv) {
std::vector<Command*> argProviders = {&Command::getDefaultCommand()};
std::string lastArgName;
Expand Down
1 change: 1 addition & 0 deletions src/parameter/ArgumentParsing.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void parseArguments(int argc, char const* const* argv);
void parseArguments(int argc, char const* const* argv, std::set<ParameterBase*> const& targetParameters);

std::string generateHelpString(std::regex const& filter=std::regex{".*"});
std::string generateGroffString();

std::set<std::string> getNextArgHint(int argc, char const* const* argv);

Expand Down

0 comments on commit 16b20f0

Please sign in to comment.