Skip to content

Commit

Permalink
Add --version option to show version and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
kimci86 committed Jan 22, 2024
1 parent d566075 commit 902d7fe
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/Arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class Arguments
/// Zip archive about which to display information
std::optional<std::string> infoArchive;

/// Tell whether version information is needed or not
bool version = false;

/// Tell whether help message is needed or not
bool help = false;

Expand Down Expand Up @@ -157,6 +160,7 @@ class Arguments
jobs,
exhaustive,
infoArchive,
version,
help
};

Expand Down
11 changes: 6 additions & 5 deletions src/Arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ Arguments::Arguments(int argc, const char* argv[])
while(!finished())
parseArgument();

if(help)
return;

if(infoArchive)
return;
if(help || version || infoArchive)
return; // no further checks are needed for those options

// check constraints on arguments
if(keys)
Expand Down Expand Up @@ -296,6 +293,9 @@ void Arguments::parseArgument()
case Option::infoArchive:
infoArchive = readString("zipfile");
break;
case Option::version:
version = true;
break;
case Option::help:
help = true;
break;
Expand Down Expand Up @@ -340,6 +340,7 @@ Arguments::Option Arguments::readOption(const std::string& description)
PAIRS(-j, --jobs, jobs),
PAIRS(-e, --exhaustive, exhaustive),
PAIRS(-L, --list, infoArchive),
PAIR ( --version, version),
PAIRS(-h, --help, help)
};

Expand Down
7 changes: 7 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Other options:
passwords) instead of stopping after the first
solution is found
-L, --list <archive> List entries in a zip archive and exit
--version Show version information and exit
-h, --help Show this help and exit)_";

void listEntries(const std::string& archiveFilename);
Expand All @@ -116,6 +117,12 @@ try
return 0;
}

if(args.version)
{
// version information was already printed, nothing else to do
return 0;
}

if(args.infoArchive)
{
listEntries(*args.infoArchive);
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ set_tests_properties(cli.bruteforce.long PROPERTIES PASS_REGULAR_EXPRESSION "P
add_test(NAME cli.list COMMAND bkcrack -L secrets.zip WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/example)
set_tests_properties(cli.list PROPERTIES PASS_REGULAR_EXPRESSION "advice\.jpg.*spiral\.svg")

add_test(NAME cli.version COMMAND bkcrack --version)
set_tests_properties(cli.version PROPERTIES PASS_REGULAR_EXPRESSION "^bkcrack ${bkcrack_VERSION} - ${bkcrack_VERSION_DATE}\n\$")

add_test(NAME cli.help COMMAND bkcrack -h)
set_tests_properties(cli.help PROPERTIES PASS_REGULAR_EXPRESSION "usage:")

0 comments on commit 902d7fe

Please sign in to comment.