2 * Copyright (c) 2018 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>
27 #include <dali/public-api/common/dali-vector.h>
42 const char * const opt;
43 const char * const optDescription;
47 const std::ios_base::fmtflags flags = std::cout.flags();
48 std::cout << std::left << " --";
49 std::cout.width( 18 );
51 std::cout << optDescription;
52 std::cout << std::endl;
53 std::cout.flags( flags );
57 Argument EXPECTED_ARGS[] =
59 { "width", "Stage Width" },
60 { "height", "Stage Height" },
61 { "dpi", "Emulated DPI" },
68 OPTION_STAGE_WIDTH = 0,
74 typedef Dali::Vector< int32_t > UnhandledContainer;
78 std::cout << "Available options:" << std::endl;
79 Argument* arg = EXPECTED_ARGS;
87 } // unnamed namespace
89 CommandLineOptions::CommandLineOptions(int32_t *argc, char **argv[])
93 // Exit gracefully if no arguments provided
101 // We do not want to print out errors.
102 int32_t origOptErrValue( opterr );
107 const struct option options[]=
109 { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt, required_argument, NULL, 'w' }, // "--width"
110 { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL, 'h' }, // "--height"
111 { EXPECTED_ARGS[OPTION_DPI].opt, required_argument, NULL, 'd' }, // "--dpi"
112 { EXPECTED_ARGS[OPTION_HELP].opt, no_argument, &help, '?' }, // "--help"
113 { 0, 0, 0, 0 } // end of options
116 int32_t shortOption( 0 );
117 int32_t optionIndex( 0 );
119 const char* optString = "-w:h:d:"; // The '-' ensures that argv is NOT permuted
120 bool optionProcessed( false );
122 UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
126 shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
128 switch ( shortOption )
132 // Check if we want help
136 optionProcessed = true;
145 stageWidth = atoi( optarg );
146 optionProcessed = true;
155 stageHeight = atoi( optarg );
156 optionProcessed = true;
165 stageDPI.assign( optarg );
166 optionProcessed = true;
173 // All command-line options have been parsed.
179 unhandledOptions.PushBack( optind - 1 );
183 } while ( shortOption != -1 );
185 // Take out the options we have processed
186 if ( optionProcessed )
188 if ( unhandledOptions.Count() > 0 )
192 // Overwrite the argv with the values from the unhandled indices
193 const UnhandledContainer::ConstIterator endIter = unhandledOptions.End();
194 for ( UnhandledContainer::Iterator iter = unhandledOptions.Begin(); iter != endIter; ++iter )
196 (*argv)[ index++ ] = (*argv)[ *iter ];
198 *argc = unhandledOptions.Count() + 1; // +1 for the program name
202 // There are no unhandled options, so we should just have the program name
206 optind = 1; // Reset to start
209 opterr = origOptErrValue; // Reset opterr value.
213 CommandLineOptions::~CommandLineOptions()
217 } // namespace Adaptor
219 } // namespace Internal