Fawkes API  Fawkes Development Version
main.cpp
1 /***************************************************************************
2  * main.cpp - Interface generator main app
3  *
4  * Generated: Tue Oct 10 17:42:05 2006
5  * Copyright 2006 Tim Niemueller [www.niemueller.de]
6  *
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "plugin_generator.h"
23 
24 #include <utils/system/argparser.h>
25 
26 #include <cstdio>
27 #include <ctime>
28 #include <string>
29 
30 using namespace std;
31 using namespace fawkes;
32 
33 void
34 print_usage(const char *program_name)
35 {
36  printf("Usage: %s [-h] <author_name> <plugin_name> <description> <directory> \n"
37  "Example: %s \"John Doe\" robot_mover \"Move the robot a meter forward\" \n"
38  " ~/fawkes/src/plugins/robot_mover/"
39  "\n"
40  "-h Print this usage information\n\n"
41  "Generate the necessary files to build a fawkes plugin\n",
42  program_name,
43  program_name);
44 }
45 
46 void
47 generate_plugin(std::string author_name,
48  std::string plugin_name,
49  std::string description,
50  std::string directory)
51 {
52  time_t now = time(0);
53  std::string date = ctime(&now);
54  tm * time_structure = localtime(&now);
55  std::string year = std::to_string(time_structure->tm_year + 1900);
56  //TimeStructure's year is the number of years since 1900
57 
58  PluginGenerator *generator =
59  new PluginGenerator(directory, author_name, year, date, plugin_name, description);
60  generator->generate();
61 }
62 
63 bool
64 plugin_name_valid(std::string plugin_name)
65 {
66  for (char &c : plugin_name) {
67  if (!isalpha(c) && c != '-' && c != '_') {
68  return false;
69  }
70  }
71  return true;
72 }
73 
74 int
75 main(int argc, char **argv)
76 {
77  ArgumentParser argp(argc, argv, "h");
78 
79  if (argp.has_arg("h")) {
80  print_usage(argv[0]);
81  exit(0);
82  }
83 
84  std::string author_name, plugin_name, description, directory;
85  if (argp.num_items() != 4) {
86  printf("ERROR: Invalid number of arguments\n");
87  print_usage(argv[0]);
88  exit(1);
89  } else if (!plugin_name_valid(argp.items()[1])) {
90  printf("ERROR: Invalid plugin name: Only alphanumerical chars allowed. \n"
91  "To separate multiple words use '-' or '_'\n");
92  exit(2);
93  } else {
94  author_name = argp.items()[0];
95  plugin_name = argp.items()[1];
96  description = argp.items()[2];
97  directory = argp.items()[3];
98  generate_plugin(author_name, plugin_name, description, directory);
99  }
100 
101  return 0;
102 }
Fawkes library namespace.
Parse command line arguments.
Definition: argparser.h:63
void generate()
Generator cpp and h files.
Generate basic plugins from minimal input.