-
Notifications
You must be signed in to change notification settings - Fork 27
Zeth tool (depends on #376) #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
040c21e
libtool: extract the subcommand framework from mpc_tool
dtebbs 16d7284
mpc_tools: refactor to use generic tool framework
dtebbs aeb26e4
zeth-tool: new tool with verifier as a subcommand
dtebbs 4396223
zeth_tool: fully generic resolution of curve and snark types from str…
dtebbs ae1959e
libzeth: support for reading assignment as separate primary and auxil…
dtebbs bdd397c
zeth_tool: prove command
dtebbs a8f3a18
zeth_tool: dump-proof command
dtebbs 65c1e51
zeth-tool: replace some boiler-plate with generic code to handle mult…
dtebbs ae52719
libtool: rename stream helper functions
dtebbs b628a3c
libzeth: formatting change for clang-format update
dtebbs 36d2b85
client: rename zeth_helper commands to zeth-helper
dtebbs f3f7846
zeth-tool: add readme
dtebbs e6dd0e1
libtool: fix inconsistent naming of util functions
dtebbs a9d6175
zeth-tool: capitalization of OPTIONS etc in usage line (review)
dtebbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| include_directories(.) | ||
|
|
||
| find_package(Boost REQUIRED COMPONENTS program_options) | ||
| include_directories(${Boost_INCLUDE_DIR}) | ||
|
|
||
| file(GLOB_RECURSE LIBTOOL_SOURCE **.?pp **.tcc) | ||
|
|
||
| add_library(tool ${LIBTOOL_SOURCE}) | ||
| target_include_directories(tool PUBLIC ..) | ||
| target_link_libraries(tool ${Boost_PROGRAM_OPTIONS_LIBRARY}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0+ | ||
|
|
||
| #ifndef __ZETH_LIBTOOL_COMMAND_HPP__ | ||
| #define __ZETH_LIBTOOL_COMMAND_HPP__ | ||
|
|
||
| #include "libtool/subcommand.hpp" | ||
|
|
||
| namespace libtool | ||
| { | ||
|
|
||
| /// Represents a top-level command, implementing parsing of global options. | ||
| template<typename GlobalOptionsT> class command | ||
| { | ||
| public: | ||
| /// Set up global options which are valid for all subcommands. | ||
| virtual void initialize_global_options( | ||
| boost::program_options::options_description &global, | ||
| boost::program_options::options_description &all_options) = 0; | ||
|
|
||
| /// Parse the variables map to update the GlobalOptionsT object. | ||
| virtual void parse_global_options( | ||
| GlobalOptionsT &out_options, | ||
| const boost::program_options::variables_map &vm) = 0; | ||
| }; | ||
|
|
||
| } // namespace libtool | ||
|
|
||
| #endif // __ZETH_LIBTOOL_COMMAND_HPP__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0+ | ||
|
|
||
| #ifndef __ZETH_LIBTOOL_RUN_HPP__ | ||
| #define __ZETH_LIBTOOL_RUN_HPP__ | ||
|
|
||
| #include "libtool/command.hpp" | ||
|
|
||
| namespace libtool | ||
| { | ||
|
|
||
| /// Execute a command object, with some global options object (initialized to | ||
| /// default values), supporting the given set of subcommands. | ||
| template<typename GlobalOptionsT> | ||
| int run_command( | ||
| command<GlobalOptionsT> &command, | ||
| GlobalOptionsT &options, | ||
| const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands, | ||
| int argc, | ||
| char **argv); | ||
|
|
||
| } // namespace libtool | ||
|
|
||
| #include "libtool/run.tcc" | ||
|
|
||
| #endif // __ZETH_LIBTOOL_RUN_HPP__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0+ | ||
|
|
||
| #ifndef __ZETH_LIBTOOL_RUN_TCC__ | ||
| #define __ZETH_LIBTOOL_RUN_TCC__ | ||
|
|
||
| #include "libtool/run.hpp" | ||
|
|
||
| namespace libtool | ||
| { | ||
|
|
||
| namespace internal | ||
| { | ||
|
|
||
| template<typename GlobalOptionsT> | ||
| void print_usage( | ||
| const char *const *argv, | ||
| const po::options_description &options, | ||
| const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands) | ||
| { | ||
| std::cout << "Usage:\n" | ||
| << " " << argv[0] << " [OPTIONS] COMMAND [ARGS] ...\n\n" | ||
| << options; | ||
|
|
||
| std::cout << "\nCommands:\n"; | ||
|
|
||
| // list_commands(commands); | ||
| using entry_t = std::pair<std::string, subcommand<GlobalOptionsT> *>; | ||
|
|
||
| // +4 here to ensure minimal space between command name and description | ||
| const size_t cmd_name_padded = | ||
| 4 + std::max_element( | ||
|
AntoineRondelet marked this conversation as resolved.
|
||
| subcommands.begin(), | ||
| subcommands.end(), | ||
| [](const entry_t &a, const entry_t &b) { | ||
| return a.first.size() < b.first.size(); | ||
| }) | ||
| ->first.size(); | ||
|
|
||
| for (const auto &cmd : subcommands) { | ||
| const size_t padding = cmd_name_padded - cmd.first.size(); | ||
| std::cout << " " << cmd.first << std::string(padding, ' ') | ||
| << cmd.second->description() << "\n"; | ||
| } | ||
|
|
||
| std::cout << std::endl; | ||
| } | ||
|
|
||
| template<typename GlobalOptionsT> | ||
| int run_subcommand( | ||
| const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands, | ||
| const std::string &command_name, | ||
| const char *argv0, | ||
| const std::vector<std::string> &command_args, | ||
| const GlobalOptionsT &global_options) | ||
| { | ||
| const typename std::map<std::string, subcommand<GlobalOptionsT> *>:: | ||
| const_iterator sub_it = subcommands.find(command_name); | ||
| if (sub_it == subcommands.end()) { | ||
| throw po::error("invalid command"); | ||
| } | ||
|
|
||
| subcommand<GlobalOptionsT> *sub = sub_it->second; | ||
| return sub->execute(argv0, command_args, global_options); | ||
| } | ||
|
|
||
| } // namespace internal | ||
|
|
||
| template<typename GlobalOptionsT> | ||
| int run_command( | ||
| command<GlobalOptionsT> &command, | ||
| GlobalOptionsT &options, | ||
| const std::map<std::string, subcommand<GlobalOptionsT> *> &subcommands, | ||
| int argc, | ||
| char **argv) | ||
| { | ||
| po::options_description global("Global options"); | ||
| po::options_description all(""); | ||
|
|
||
| // Default --help option | ||
| global.add_options()("help,h", "Show this help message and exit"); | ||
|
|
||
| // Global options | ||
| command.initialize_global_options(global, all); | ||
|
|
||
| // Add a single positional "command" option. | ||
| po::positional_options_description pos; | ||
| all.add_options()( | ||
| "command", po::value<std::string>(), "Command to execute")( | ||
| "subargs", | ||
| po::value<std::vector<std::string>>(), | ||
| "Arguments to command"); | ||
| pos.add("command", 1).add("subargs", -1); | ||
|
|
||
| auto usage = [&argv, &global, &subcommands]() { | ||
| internal::print_usage<GlobalOptionsT>(argv, global, subcommands); | ||
| }; | ||
|
|
||
| try { | ||
| po::variables_map vm; | ||
| po::parsed_options parsed = po::command_line_parser(argc, argv) | ||
| .options(all) | ||
| .positional(pos) | ||
| .allow_unregistered() | ||
| .run(); | ||
| po::store(parsed, vm); | ||
|
|
||
| const bool help_flag = (bool)vm.count("help"); | ||
|
|
||
| // If no command was given, print the top-level usage message. If a | ||
| // help flag was specified, exit normally, otherwise print an error | ||
| // message and exit with error. (If a command was given, the help flag | ||
| // is passed to the subcommand). | ||
| if (!vm.count("command")) { | ||
| if (help_flag) { | ||
| usage(); | ||
| return 0; | ||
| } | ||
| std::cerr << "error: no command specified\n"; | ||
| usage(); | ||
| return 1; | ||
| } | ||
|
|
||
| // Parse the global options | ||
| command.parse_global_options(options, vm); | ||
|
|
||
| // Execute the subcommand | ||
| const std::string subcommand(vm["command"].as<std::string>()); | ||
| std::vector<std::string> subargs = | ||
| po::collect_unrecognized(parsed.options, po::include_positional); | ||
| subargs[0] = std::string(argv[0]) + " " + subargs[0]; | ||
|
|
||
| // Add the --help flag back, if given (it was absorbed by the global | ||
| // parser above). | ||
| if (help_flag) { | ||
| subargs.push_back("--help"); | ||
| } | ||
|
|
||
| return internal::run_subcommand( | ||
| subcommands, subcommand, argv[0], subargs, options); | ||
| } catch (po::error &error) { | ||
| std::cerr << " ERROR: " << error.what() << std::endl; | ||
| usage(); | ||
| } | ||
|
|
||
| return 1; | ||
| } | ||
|
|
||
| } // namespace libtool | ||
|
|
||
| #endif // __ZETH_LIBTOOL_RUN_TCC__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (c) 2015-2021 Clearmatics Technologies Ltd | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0+ | ||
|
|
||
| #ifndef __ZETH_LIBTOOL_SUBCOMMAND_HPP__ | ||
| #define __ZETH_LIBTOOL_SUBCOMMAND_HPP__ | ||
|
|
||
| #include <boost/program_options.hpp> | ||
| #include <map> | ||
| #include <string> | ||
|
|
||
| namespace po = boost::program_options; | ||
|
|
||
| namespace libtool | ||
| { | ||
|
|
||
| /// Class representing a tool subcommand. | ||
| template<typename GlobalOptionsT> class subcommand | ||
| { | ||
| public: | ||
| subcommand( | ||
| const std::string &subcommand_name, const std::string &description); | ||
| virtual ~subcommand(); | ||
| const std::string &description() const; | ||
|
|
||
| /// Common code to parse options and invoke the virtual execute entrypoint. | ||
| int execute( | ||
| const char *argv0, | ||
| const std::vector<std::string> command_args, | ||
| const GlobalOptionsT &global); | ||
|
|
||
| protected: | ||
| void usage( | ||
| const char *argv0, | ||
| const boost::program_options::options_description &options); | ||
|
|
||
| /// Instantiation can now set up the boost program_options structures. | ||
| virtual void initialize_suboptions( | ||
| boost::program_options::options_description &options, | ||
| boost::program_options::options_description &all_options, | ||
| boost::program_options::positional_options_description &pos) = 0; | ||
|
|
||
| /// Instantiation can record any command-specific information from the | ||
| /// parsed variables_map. | ||
| virtual void parse_suboptions( | ||
| const boost::program_options::variables_map &vm) = 0; | ||
|
|
||
| /// Any command-specific output for usage. | ||
| virtual void subcommand_usage(const char *argv0) = 0; | ||
|
|
||
| /// Execute the command using global options defined by the caller. | ||
| virtual int execute_subcommand(const GlobalOptionsT &global) = 0; | ||
|
|
||
| std::string subcommand_name; | ||
| std::string subcommand_description; | ||
| }; | ||
|
|
||
| } // namespace libtool | ||
|
|
||
| #include "libtool/subcommand.tcc" | ||
|
|
||
| #endif // __ZETH_LIBTOOL_SUBCOMMAND_HPP__ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.