Skip to content

Commit

Permalink
Add completion generator for bash/zsh (#361)
Browse files Browse the repository at this point in the history
* Add completion generator for bash/zsh

* Lint, add execution coverage for new code
  • Loading branch information
vintagepc authored Apr 23, 2022
1 parent 6de3ab1 commit 2444165
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
url = https://github.com/tinyobjloader/tinyobjloader.git
[submodule "3rdParty/TCLAP"]
path = 3rdParty/TCLAP
url = https://git.code.sf.net/p/tclap/code
url = https://github.com/vintagepc/TCLAP.git
[submodule "3rdParty/gitversion"]
path = 3rdParty/gitversion
url = https://github.com/vintagepc/gitversion.git
Expand Down
2 changes: 1 addition & 1 deletion 3rdParty/TCLAP
Submodule TCLAP updated 210 files
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,9 @@ add_test(core_SD_image7a chmod a-w RO_SDcard.bin_test)
add_test(core_Printer_Trace MK404 Test_Printer -g none --trace ?)
add_test(core_Printer_Script MK404 Test_Printer -g none --scripthelp)
add_test(core_Printer_Keyprint MK404 Test_Printer -g none -k)
add_test(core_bash_complete MK404 --complete-bash)
add_test(core_zsh_complete MK404 --complete-zsh)

foreach (TEST_SRC IN LISTS TESTS_SOURCES)
get_filename_component(TEST_BASE ${TEST_SRC} NAME_WE)
message(STATUS "Adding test: ${TEST_BASE}" )
Expand Down
50 changes: 39 additions & 11 deletions MK404.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
#include "parts/Board.h" // for Board
#include "sim_avr.h" // for avr_t
#include "tclap/ArgException.h" // for ArgParseException
#include "tclap/BashCompletionOutput.h"
#include "tclap/CmdLine.h" // for CmdLine
#include "tclap/MultiArg.h" // for MultiArg
#include "tclap/MultiSwitchArg.h" // for MultiSwitchArg
#include "tclap/SwitchArg.h" // for SwitchArg
#include "tclap/UnlabeledValueArg.h" // for UnlabeledValueArg
#include "tclap/ValueArg.h" // for ValueArg
#include "tclap/ValuesConstraint.h" // for ValuesConstraint
#include "tclap/ZshCompletionOutput.h" // for ValuesConstraint
// NOLINTNEXTLINE - glew has to be included before the freeglut libraries.
#include <GL/glew.h> // for glVertex2f, glEnable, glTranslatef
#include <GL/freeglut_std.h> // for glutGet, glutTimerFunc, glutCre...
Expand Down Expand Up @@ -327,17 +329,18 @@ int main(int argc, char *argv[])
SwitchArg argTest("","test","Run it test mode (don't auto-exit due to lack of GL event loop and waiting for the window to close)", cmd);
SwitchArg argSkew("","skew-correct","Attempt to correct for fast clock skew of the simulated board", cmd);
SwitchArg argSerial("s","serial","Connect a printer's serial port to a PTY instead of printing its output to the console.", cmd);
ValueArg<string> argSD("","sdimage","Use the given SD card .img file instead of the default", false ,"", "filename.img", cmd);
ValueArg<string> argSD("","sdimage","Use the given SD card .img file instead of the default", false ,"", "file:*.img|*.bin", cmd);
SwitchArg argScriptHelp("","scripthelp", "Prints the available scripting commands for the current printer/context",cmd, false);
ValueArg<string> argScript("","script","Execute the given script. Use --scripthelp for syntax.", false ,"", "filename.txt", cmd);
ValueArg<string> argScript("","script","Execute the given script. Use --scripthelp for syntax.", false ,"", "file:*.txt", cmd);
std::vector<string> vstrEnabled = EnabledType::GetOpts();
ValuesConstraint<string> vcEnabledOpts(vstrEnabled);
ValueArg<string> argSoftPWM("p","softPWM","enable/disable software PWM (currently only valid for MK2)",false,"",&vcEnabledOpts,cmd);
SwitchArg argNoHacks("n","no-hacks","Disable any special hackery that might have been implemented for a board to run its manufacturer firmware, e.g. if you want to run stock marlin and have issues. Effects depend on the board and firmware.",cmd);
SwitchArg argMute("m","mute","Tell a printer to mute any audio it may produce.", cmd);
SwitchArg argMarlin("","marlin","Synonym for --no-hacks",cmd,false);
SwitchArg argLoad("l","loadfw","Directs the printer to load the default firmware file. (-f implies -l) If neither -l or -f are provided, the printer executes solely from its persisted flash.", cmd);
ValueArg<uint8_t> argLCDSCheme("","lcd-scheme", "Sets the default LCD colour scheme index, 0, 1, or 2",false, 0,"0-2",cmd);
SwitchArg argLegacyModel("","legacythermal","Use the legacy thermal model for the nozzle heater.", cmd);
ValueArg<uint8_t> argLCDSCheme("","lcd-scheme", "Sets the default LCD colour scheme index, 0, 1, or 2",false, 0,"0|1|2",cmd);
SwitchArg argKlipper("","klipper","Synonym for --skew-correct and --no-hacks",cmd,false);
SwitchArg argKeyHelp("k","keys","Prints the list of available keyboard controls",cmd,false);
std::vector<string> vstrSizes = FatImage::GetSizes();
Expand All @@ -347,15 +350,17 @@ int main(int argc, char *argv[])
std::vector<string> vstrGfx = {"none","lite","fancy", "bear"};
ValuesConstraint<string> vcGfxAllowed(vstrGfx);
ValueArg<string> argGfx("g","graphics","Whether to enable fancy (advanced) or lite (minimal advanced) visuals. If not specified, only the basic 2D visuals are shown.",false,"lite",&vcGfxAllowed, cmd);
ValueArg<string> argFW("f","firmware","hex/afx/elf Firmware file to load (default MK3S.afx)",false,"MK3S.afx","filename", cmd);
ValueArg<string> argFW2("F","firmware2","secondary hex/afx/elf Firmware file to load to MMU, if present (default MM-control-01.hex)",false,"MM-control-01.hex","filename", cmd);
ValueArg<string> argFW("f","firmware","hex/afx/elf Firmware file to load (default MK3S.afx)",false,"MK3S.afx","file:*.hex|*.afx|*.elf", cmd);
ValueArg<string> argFW2("F","firmware2","secondary hex/afx/elf Firmware file to load to MMU, if present (default MM-control-01.hex)",false,"MM-control-01.hex","file:*.hex|*.afx|*.elf", cmd);
std::vector<string> vstrExts = PrintVisualType::GetOpts();
ValuesConstraint<string> vcPrintOpts(vstrExts);
ValueArg<string> argExtrusion("","extrusion","Set Print visual type. HR options create a LOT of triangles, do not use for large prints!",false, "Line", &vcPrintOpts, cmd);
SwitchArg argDebugCore("","debugcore","Uses a debug version of the chip with more flash space. Board-dependent.", cmd);
MultiSwitchArg argDebug("d","debug","Increases debugging output, where supported.", cmd);
SwitchArg argCmplZsh("","complete-zsh","Generate zsh auto-complete script for supported arguments",cmd);
SwitchArg argCmplBash("","complete-bash","Generate bash auto-complete script for supported arguments",cmd);
SwitchArg argColourE("", "colour-extrusion", "Colours extrusion by width (for advanced step/extrusion debugging.", cmd, false);
ValueArg<string> argStrBoot("","bootloader-file", "Specifies a .hex file to load as the bootloader. If empty, ("") no bootloader is loaded, if unspecified the default is used.",false,"stk500boot_v2_mega2560.hex","string",cmd);
ValueArg<string> argStrBoot("","bootloader-file", "Specifies a .hex file to load as the bootloader. If empty, ("") no bootloader is loaded, if unspecified the default is used.",false,"stk500boot_v2_mega2560.hex","file:*.hex",cmd);
SwitchArg argBootloader("b","bootloader","Run bootloader on first start instead of going straight to the firmware.",cmd);
SwitchArg argMD("","markdown","Used to auto-generate the items in refs/ as markdown",cmd);

Expand All @@ -364,17 +369,40 @@ int main(int argc, char *argv[])

TCLAP::UnlabeledValueArg<string> argModel("printer","Model name of the printer to run",false,"Prusa_MK3S",&vcAllowed, cmd);



if (version::IS_DEV_VERSION)
{
std::cout << "***************************************" << '\n';
std::cout << "* " << std::setw(35) << version::VERSION_STRING << " *" << '\n';
std::cout << "* This is a DEV version. Features may *" << '\n';
std::cout << "* behave unexpectedly, or not at all. *" << '\n';
std::cout << "***************************************" << '\n';
std::cout << "#***************************************" << '\n';
std::cout << "#* " << std::setw(35) << version::VERSION_STRING << " *" << '\n';
std::cout << "#* This is a DEV version. Features may *" << '\n';
std::cout << "#* behave unexpectedly, or not at all. *" << '\n';
std::cout << "#***************************************" << '\n';
}

cmd.parse(argc,argv);

if (argCmplBash.isSet() || argCmplZsh.isSet())
{
TCLAP::BashCompletionOutput fmtOutBash;
TCLAP::ZshCompletionOutput fmtOutZsh;
if (argCmplBash.isSet())
{
cmd.setOutput(&fmtOutBash);
}
else
{
cmd.setOutput(&fmtOutZsh);
}
const char *const args[2] =
{
*argv,
"--help"
};
cmd.parse(2,&args[0]);
return 0; // pragma: LCOV_EXCL_LINE - returns don't always get correctly marked as covered due to optimization
}

// Handle the convenience synonyms:
// Longer term it'd be neat to have a synonym handler in TCLAP....
bool bArgHacks = argNoHacks.isSet() || argKlipper.isSet() || argMarlin.isSet();
Expand Down

0 comments on commit 2444165

Please sign in to comment.