Skip to content

Commit

Permalink
Use an open source argparse
Browse files Browse the repository at this point in the history
  • Loading branch information
lancerts committed Jan 25, 2024
1 parent a11fe63 commit 246c270
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/main_cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ jobs:
run: |
sudo apt -y install libtbb-dev
sudo apt install libopencv-dev
- name: Install argparse
run: |
git clone https://github.com/p-ranav/argparse
cd argparse
mkdir build
cd build
cmake -DARGPARSE_BUILD_SAMPLES=off -DARGPARSE_BUILD_TESTS=off ..
sudo make install
# Alternatively, you can install OpenCV from source
# - name: Install OpenCV from source
# run: |
Expand Down
27 changes: 15 additions & 12 deletions cpp/dcgan/dcgan.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <torch/torch.h>

#include <argparse/argparse.hpp>
#include <cmath>
#include <cstdio>
#include <iostream>
Expand Down Expand Up @@ -95,18 +95,21 @@ nn::Sequential create_discriminator() {
}

int main(int argc, const char* argv[]) {

if (argc > 1) {
std::string arg = argv[1];
if (std::all_of(arg.begin(), arg.end(), ::isdigit)) {
try {
kNumberOfEpochs = std::stoll(arg);
} catch (const std::invalid_argument& ia) {
// If unable to parse, do nothing and keep the default value
}
}
argparse::ArgumentParser parser("cpp/dcgan example");
parser.add_argument("--epochs")
.help("Number of epochs to train")
.default_value(kNumberOfEpochs)
.scan<'i', int64_t>();
try {
parser.parse_args(argc, argv);
} catch (const std::exception& err) {
std::cout << err.what() << std::endl;
std::cout << parser;
std::exit(1);
}
std::cout << "Traning with number of epochs: " << kNumberOfEpochs << std::endl;
kNumberOfEpochs = parser.get<int64_t>("--epochs");
std::cout << "Traning with number of epochs: " << kNumberOfEpochs
<< std::endl;

torch::manual_seed(1);

Expand Down
2 changes: 1 addition & 1 deletion run_cpp_examples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function dcgan() {
make
if [ $? -eq 0 ]; then
echo "Successfully built $EXAMPLE"
./$EXAMPLE 5 # Run the executable with kNumberOfEpochs = 5
./$EXAMPLE --epochs 5 # Run the executable with kNumberOfEpochs = 5
check_run_success $EXAMPLE
else
error "Failed to build $EXAMPLE"
Expand Down

0 comments on commit 246c270

Please sign in to comment.