2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/command-line-options.h>
26 #include <dali/public-api/common/dali-vector.h>
39 const char* const opt;
40 const char* const optDescription;
44 const std::ios_base::fmtflags flags = std::cout.flags();
45 std::cout << std::left << " --";
48 std::cout << optDescription;
49 std::cout << std::endl;
50 std::cout.flags(flags);
54 Argument EXPECTED_ARGS[] =
56 {"width", "Stage Width"},
57 {"height", "Stage Height"},
58 {"dpi", "Emulated DPI"},
64 OPTION_STAGE_WIDTH = 0,
70 typedef Dali::Vector<int32_t> UnhandledContainer;
74 std::cout << "Available options:" << std::endl;
75 Argument* arg = EXPECTED_ARGS;
83 } // unnamed namespace
85 CommandLineOptions::CommandLineOptions(int32_t* argc, char** argv[])
89 // Exit gracefully if no arguments provided
97 // We do not want to print out errors.
98 int32_t origOptErrValue(opterr);
103 const struct option options[] =
105 {EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt, required_argument, NULL, 'w'}, // "--width"
106 {EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL, 'h'}, // "--height"
107 {EXPECTED_ARGS[OPTION_DPI].opt, required_argument, NULL, 'd'}, // "--dpi"
108 {EXPECTED_ARGS[OPTION_HELP].opt, no_argument, &help, '?'}, // "--help"
109 {0, 0, 0, 0} // end of options
112 int32_t shortOption(0);
113 int32_t optionIndex(0);
115 const char* optString = "-w:h:d:"; // The '-' ensures that argv is NOT permuted
116 bool optionProcessed(false);
118 UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
122 shortOption = getopt_long(*argc, *argv, optString, options, &optionIndex);
128 // Check if we want help
132 optionProcessed = true;
141 stageWidth = atoi(optarg);
142 optionProcessed = true;
151 stageHeight = atoi(optarg);
152 optionProcessed = true;
161 stageDPI.assign(optarg);
162 optionProcessed = true;
169 // All command-line options have been parsed.
175 unhandledOptions.PushBack(optind - 1);
179 } while(shortOption != -1);
181 // Take out the options we have processed
184 if(unhandledOptions.Count() > 0)
188 // Overwrite the argv with the values from the unhandled indices
189 const UnhandledContainer::ConstIterator endIter = unhandledOptions.End();
190 for(UnhandledContainer::Iterator iter = unhandledOptions.Begin(); iter != endIter; ++iter)
192 (*argv)[index++] = (*argv)[*iter];
194 *argc = unhandledOptions.Count() + 1; // +1 for the program name
198 // There are no unhandled options, so we should just have the program name
202 optind = 1; // Reset to start
205 opterr = origOptErrValue; // Reset opterr value.
209 CommandLineOptions::~CommandLineOptions()
213 } // namespace Adaptor
215 } // namespace Internal