Skip to content

Commit 246c270

Browse files
committed
Use an open source argparse
1 parent a11fe63 commit 246c270

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

.github/workflows/main_cpp.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ jobs:
3131
run: |
3232
sudo apt -y install libtbb-dev
3333
sudo apt install libopencv-dev
34-
34+
- name: Install argparse
35+
run: |
36+
git clone https://github.com/p-ranav/argparse
37+
cd argparse
38+
mkdir build
39+
cd build
40+
cmake -DARGPARSE_BUILD_SAMPLES=off -DARGPARSE_BUILD_TESTS=off ..
41+
sudo make install
3542
# Alternatively, you can install OpenCV from source
3643
# - name: Install OpenCV from source
3744
# run: |

cpp/dcgan/dcgan.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <torch/torch.h>
2-
2+
#include <argparse/argparse.hpp>
33
#include <cmath>
44
#include <cstdio>
55
#include <iostream>
@@ -95,18 +95,21 @@ nn::Sequential create_discriminator() {
9595
}
9696

9797
int main(int argc, const char* argv[]) {
98-
99-
if (argc > 1) {
100-
std::string arg = argv[1];
101-
if (std::all_of(arg.begin(), arg.end(), ::isdigit)) {
102-
try {
103-
kNumberOfEpochs = std::stoll(arg);
104-
} catch (const std::invalid_argument& ia) {
105-
// If unable to parse, do nothing and keep the default value
106-
}
107-
}
98+
argparse::ArgumentParser parser("cpp/dcgan example");
99+
parser.add_argument("--epochs")
100+
.help("Number of epochs to train")
101+
.default_value(kNumberOfEpochs)
102+
.scan<'i', int64_t>();
103+
try {
104+
parser.parse_args(argc, argv);
105+
} catch (const std::exception& err) {
106+
std::cout << err.what() << std::endl;
107+
std::cout << parser;
108+
std::exit(1);
108109
}
109-
std::cout << "Traning with number of epochs: " << kNumberOfEpochs << std::endl;
110+
kNumberOfEpochs = parser.get<int64_t>("--epochs");
111+
std::cout << "Traning with number of epochs: " << kNumberOfEpochs
112+
<< std::endl;
110113

111114
torch::manual_seed(1);
112115

run_cpp_examples.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function dcgan() {
102102
make
103103
if [ $? -eq 0 ]; then
104104
echo "Successfully built $EXAMPLE"
105-
./$EXAMPLE 5 # Run the executable with kNumberOfEpochs = 5
105+
./$EXAMPLE --epochs 5 # Run the executable with kNumberOfEpochs = 5
106106
check_run_success $EXAMPLE
107107
else
108108
error "Failed to build $EXAMPLE"

0 commit comments

Comments
 (0)