3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <support/logging/CHIPLogging.h>
24 #include "qrcodetool_command_manager.h"
26 static int match_command(const char * command_name, const char * name)
28 return !strncmp(command_name, name, strlen(name));
31 static int help(int argc, char ** argv)
33 qrcodetool_command_t * cmd = nullptr;
34 for (cmd = commands; cmd->c_name != nullptr; cmd++)
36 ChipLogDetail(chipTool, "%s\t%s\n", cmd->c_name, cmd->c_help);
41 static int usage(const char * prog_name)
43 ChipLogDetail(chipTool,
44 "Usage: %s [-h] [command] [opt ...]\n"
46 prog_name, prog_name);
51 static int execute_command(int argc, char ** argv)
57 const qrcodetool_command_t * command_to_execute = nullptr;
60 for (command_to_execute = commands; command_to_execute->c_name; command_to_execute++)
62 if (match_command(command_to_execute->c_name, argv[0]))
71 ChipLogDetail(chipTool, "Executing cmd %s\n", command_to_execute->c_name);
72 return command_to_execute->c_func(argc, argv);
75 return help(0, nullptr);
78 int main(int argc, char ** argv)
84 /* Remember my name. */
85 char * prog_name = strrchr(argv[0], '/');
86 prog_name = prog_name ? prog_name + 1 : argv[0];
87 /* Do getopt stuff for global options. */
90 while ((ch = getopt(argc, argv, "h")) != -1)
100 return usage(prog_name);
109 /* Munge argc/argv so that argv[0] is something. */
110 result = help(0, nullptr);
114 result = execute_command(argc, argv);
118 result = usage(prog_name);