Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / tests / tools / tflite_loader / src / args.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "args.h"
18
19 #include <iostream>
20
21 #include <boost/filesystem.hpp>
22
23 namespace TFLiteRun
24 {
25
26 Args::Args(const int argc, char **argv) noexcept
27 {
28   Initialize();
29   Parse(argc, argv);
30 }
31
32 void Args::Initialize(void)
33 {
34   // General options
35   po::options_description general("General options");
36
37   // clang-format off
38   general.add_options()
39     ("help,h", "Display available options")
40     ("tflite", po::value<std::string>()->default_value("")->required(), "Input tflite model file for serialization")
41     ("data,d", po::value<std::vector<std::string>>()->multitoken()->default_value(std::vector<std::string>{}, ""), "Input data file for model");
42   // clang-format on
43
44   _options.add(general);
45   _positional.add("tflite", 1);
46 }
47
48 void Args::print(char **argv)
49 {
50   std::cout << "tflite_loader" << std::endl << std::endl;
51   std::cout << "Load tflite model by Loader and TFLite and compare their output" << std::endl;
52   std::cout << "Usage:" << std::endl;
53   std::cout << argv[0] << " --tflite model_file.tflite --data input_data.dat" << std::endl;
54   std::cout << _options;
55   std::cout << std::endl;
56 }
57
58 void Args::Parse(const int argc, char **argv)
59 {
60   po::variables_map vm;
61   po::store(po::command_line_parser(argc, argv).options(_options).positional(_positional).run(),
62             vm);
63   po::notify(vm);
64
65   if (vm.count("help"))
66   {
67     print(argv);
68
69     exit(0);
70   }
71
72   try
73   {
74     if (vm.count("tflite"))
75     {
76       _tflite_filename = vm["tflite"].as<std::string>();
77     }
78
79     if (vm.count("data"))
80     {
81       _data_filenames = vm["data"].as<std::vector<std::string>>();
82     }
83   }
84   catch (const std::bad_cast &e)
85   {
86     std::cerr << e.what() << '\n';
87     print(argv);
88     exit(1);
89   }
90 }
91
92 } // end of namespace TFLiteRun