From 24f6d1835b883498105c6b2939e9411f75808f5a Mon Sep 17 00:00:00 2001 From: Fabio Pellacini Date: Tue, 19 Oct 2021 23:17:35 +0200 Subject: [PATCH] Shorter cli commands (#1302) --- apps/yimage/yimage.cpp | 11 +++++------ apps/ymesh/ymesh.cpp | 11 +++++------ apps/yscene/yscene.cpp | 11 +++++------ apps/yshape/yshape.cpp | 20 +++++++++----------- libs/yocto/yocto_cli.h | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/apps/yimage/yimage.cpp b/apps/yimage/yimage.cpp index 4481365d7..a80a607a1 100644 --- a/apps/yimage/yimage.cpp +++ b/apps/yimage/yimage.cpp @@ -234,12 +234,11 @@ int main(int argc, const char* argv[]) { auto params = app_params{}; auto cli = make_cli("yimage", "process and view images"); add_command_var(cli, params.command); - add_options(add_command(cli, "convert", "convert images"), params.convert); - add_options(add_command(cli, "view", "view images"), params.view); - add_options(add_command(cli, "grade", "grade images"), params.grade); - add_options(add_command(cli, "diff", "diff two images"), params.diff); - add_options( - add_command(cli, "setalpha", "set images alpha"), params.setalpha); + add_command(cli, "convert", params.convert, "convert images"); + add_command(cli, "view", params.view, "view images"); + add_command(cli, "grade", params.grade, "grade images"); + add_command(cli, "diff", params.diff, "diff two images"); + add_command(cli, "setalpha", params.setalpha, "set images alpha"); parse_cli(cli, argc, argv); // dispatch commands diff --git a/apps/ymesh/ymesh.cpp b/apps/ymesh/ymesh.cpp index 5c18e1713..67fca4c4f 100644 --- a/apps/ymesh/ymesh.cpp +++ b/apps/ymesh/ymesh.cpp @@ -1227,12 +1227,11 @@ int main(int argc, const char* argv[]) { auto params = app_params{}; auto cli = make_cli("ymesh", "process and view meshes"); add_command_var(cli, params.command); - add_options(add_command(cli, "view", "view shapes"), params.view); - add_options( - add_command(cli, "glview", "view shapes with OpenGL"), params.glview); - add_options(add_command(cli, "glpath", "trace paths"), params.glpath); - add_options(add_command(cli, "glpathd", "debug paths"), params.glpathd); - add_options(add_command(cli, "glsculpt", "sculpt meshes"), params.glsculpt); + add_command(cli, "view", params.view, "view shapes"); + add_command(cli, "glview", params.glview, "view shapes with OpenGL"); + add_command(cli, "glpath", params.glpath, "trace paths"); + add_command(cli, "glpathd", params.glpathd, "debug paths"); + add_command(cli, "glsculpt", params.glsculpt, "sculpt meshes"); parse_cli(cli, argc, argv); // dispatch commands diff --git a/apps/yscene/yscene.cpp b/apps/yscene/yscene.cpp index e21679888..57ef3243c 100644 --- a/apps/yscene/yscene.cpp +++ b/apps/yscene/yscene.cpp @@ -362,12 +362,11 @@ int main(int argc, const char* argv[]) { auto params = app_params{}; auto cli = make_cli("yscene", "process and view scenes"); add_command_var(cli, params.command); - add_options(add_command(cli, "convert", "convert scenes"), params.convert); - add_options(add_command(cli, "info", "print scenes info"), params.info); - add_options(add_command(cli, "render", "render scenes"), params.render); - add_options(add_command(cli, "view", "view scenes"), params.view); - add_options( - add_command(cli, "glview", "view scenes with OpenGL"), params.glview); + add_command(cli, "convert", params.convert, "convert scenes"); + add_command(cli, "info", params.info, "print scenes info"); + add_command(cli, "render", params.render, "render scenes"); + add_command(cli, "view", params.view, "view scenes"); + add_command(cli, "glview", params.glview, "view scenes with OpenGL"); parse_cli(cli, argc, argv); // dispatch commands diff --git a/apps/yshape/yshape.cpp b/apps/yshape/yshape.cpp index deca4e7f1..2bb897526 100644 --- a/apps/yshape/yshape.cpp +++ b/apps/yshape/yshape.cpp @@ -457,17 +457,15 @@ int main(int argc, const char** argv) { auto params = app_params{}; auto cli = make_cli("yshape", "Process and view shapes"); add_command_var(cli, params.command); - add_options(add_command(cli, "convert", "convert shapes"), params.convert); - add_options(add_command(cli, "fvconvert", "convert face-varying shapes"), - params.fvconvert); - add_options(add_command(cli, "view", "view shapes"), params.view); - add_options(add_command(cli, "heightfield", "create an heightfield"), - params.heightfield); - add_options(add_command(cli, "hair", "grow hairs on a shape"), params.hair); - add_options( - add_command(cli, "sample", "sample points on a shape"), params.sample); - add_options( - add_command(cli, "glview", "view shapes with OpenGL"), params.glview); + add_command(cli, "convert", params.convert, "convert shapes"); + add_command( + cli, "fvconvert", params.fvconvert, "convert face-varying shapes"); + add_command(cli, "view", params.view, "view shapes"); + add_command( + cli, "heightfield", params.heightfield, "create an heightfield"); + add_command(cli, "hair", params.hair, "grow hairs on a shape"); + add_command(cli, "sample", params.sample, "sample points on a shape"); + add_command(cli, "glview", params.glview, "view shapes with OpenGL"); parse_cli(cli, argc, argv); // dispatch commands diff --git a/libs/yocto/yocto_cli.h b/libs/yocto/yocto_cli.h index a93b73a87..9fd527f47 100644 --- a/libs/yocto/yocto_cli.h +++ b/libs/yocto/yocto_cli.h @@ -76,6 +76,10 @@ inline cli_command& add_command( // add command variable template inline void add_command_var(cli_command& cli, T& value); +// add command by invoking add_options for the struct type passed +template +inline cli_command& add_command( + cli_command& cli, const string& name, T& value, const string& usage); // add option template @@ -174,6 +178,9 @@ namespace yocto { // Cli map. Cannot use map or unordered_map template struct cli_map { + // reserve to ensure pointer stability + cli_map() { _data.reserve(256); } + bool empty() const { return _data.empty(); } bool contains(const Key& key) const { for (auto& [key_, value] : _data) @@ -383,6 +390,15 @@ inline void add_command_var(cli_command& cli, T& value) { }; } +// add command by invoking add_options for the struct type passed +template +inline cli_command& add_command( + cli_command& cli, const string& name, T& value, const string& usage) { + auto& cmd = add_command(cli, name, usage); + add_options(cmd, value); + return cmd; +} + // add option template inline void add_option(