Skip to content

Commit

Permalink
Handle all possible exceptions in CommandLineParser
Browse files Browse the repository at this point in the history
  • Loading branch information
benvenutti committed Sep 15, 2024
1 parent 7f7cd83 commit 880347a
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions utilities/src/utilities/CommandLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

#include <exception>

namespace Utilities
namespace
{

CommandLineParser::Result CommandLineParser::parse( const int argc, char const* const* argv )
Utilities::CommandLineParser::Result createCLIAndParse( const int argc, char const* const* argv )
{
CLI::App app{ "hasm: assembler for the nand2tetris hack platform" };

std::filesystem::path inputFile{};
const auto inputFileOption = app.add_option( "-i,--input-file", inputFile, "input .asm file" );

const auto inputFileOption = app.add_option( "-i,--input-file", inputFile, "input .asm file" );

bool exportSymbolTable{ false };

app.add_flag( "-s,--symbol-table", exportSymbolTable, "export symbol table (to <input file>.sym)" )
->needs( inputFileOption );

Expand All @@ -24,15 +26,28 @@ CommandLineParser::Result CommandLineParser::parse( const int argc, char const*
{
app.parse( argc, argv );

return Config{ std::move( inputFile ), exportSymbolTable };
return Utilities::CommandLineParser::Config{ std::move( inputFile ), exportSymbolTable };
}
catch ( const CLI::CallForHelp& )
{
return RequestToPrintHelp{ app.help() };
return Utilities::CommandLineParser::RequestToPrintHelp{ app.help() };
}
catch ( const CLI::CallForVersion& )
{
return RequestToPrintVersion{};
return Utilities::CommandLineParser::RequestToPrintVersion{};
}
}

} // namespace

namespace Utilities
{

CommandLineParser::Result CommandLineParser::parse( const int argc, char const* const* argv )
{
try
{
return createCLIAndParse( argc, argv );
}
catch ( const std::exception& exception )
{
Expand Down

0 comments on commit 880347a

Please sign in to comment.