aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKristen Carlson Accardi <kristen@linux.intel.com>2011-04-21 13:21:26 -0700
committerKristen Carlson Accardi <kristen@linux.intel.com>2011-04-21 14:48:58 -0700
commitb7ce5316d6a8264aba3cbadee8e6b99a2925688b (patch)
treeeac1f6c7a860e9908614e10c5c4b0bf9d5cea9b7
parentfaf52fc1c6a2f16a734bf5c77f16af15cab0df47 (diff)
downloadpowertop-b7ce5316d6a8264aba3cbadee8e6b99a2925688b.tar.gz
This patch converts the commandline parsing to use getopt_long. Although powertop has only a few commandline options, this conversion makes it easier to add new options, and is possibly less error prone.
To demonstrate how easy it is to add new options, the patch also introduces --help and --version command line options. Code is taken from The GNU C Library reference manual, section 25.2. http://www.gnu.org/s/libc/manual/html_node/Getopt-Long-Option-Example.html Signed-off-by: Peter Huewe <peterhuewe@gmx.de> Signed-off-by: Kristen Carlson Accardi <kristen@linux.intel.com>
-rw-r--r--main.cpp114
-rw-r--r--po/powertop.pot54
2 files changed, 130 insertions, 38 deletions
diff --git a/main.cpp b/main.cpp
index c5b8a60..5c5e691 100644
--- a/main.cpp
+++ b/main.cpp
@@ -19,6 +19,11 @@
* Boston, MA 02110-1301 USA
* or just google for it.
*
+ * getopt code is taken from "The GNU C Library" reference manual,
+ * section 24.2 "Parsing program options using getopt"
+ * http://www.gnu.org/s/libc/manual/html_node/Getopt-Long-Option-Example.html
+ * Manual published under the terms of the Free Documentation License.
+ *
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
*/
@@ -27,6 +32,7 @@
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
+#include <getopt.h>
#include "cpu/cpu.h"
#include "process/process.h"
@@ -47,10 +53,38 @@
#include "display.h"
#include "devlist.h"
-int debug_learning;
+int debug_learning = 0;
int leave_powertop = 0;
+static const struct option long_options[] =
+{
+ /* These options set a flag. */
+ {"debug", no_argument, &debug_learning, 'd'},
+ {"version", no_argument, NULL, 'V'},
+ {"help",no_argument, NULL, 'u'}, /* u for usage */
+ {"calibrate",no_argument, NULL, 'c'},
+ {"html", optional_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+};
+
+static void print_version()
+{
+ printf(_("Powertop version" POWERTOP_VERSION ", compiled on "__DATE__ "\n"));
+}
+
+static void print_usage()
+{
+ printf(_("Usage: powertop [OPTIONS]\n\n"));
+ printf(_("--debug \t run in \"debug\" mode\n"));
+ printf(_("--version \t print version information\n"));
+ printf(_("--calibrate \t runs powertop in calibration mode\n"));
+ printf(_("--html[=FILENAME]\t\t generate a html report\n"));
+ printf(_("--help \t\t print this help menu\n"));
+ printf("\n");
+ printf(_("For more help please refer to the README\n\n"));
+}
+
static void do_sleep(int seconds)
{
time_t target;
@@ -159,6 +193,8 @@ void out_of_memory()
int main(int argc, char **argv)
{
int uid;
+ int option_index;
+ int c;
set_new_handler(out_of_memory);
@@ -195,41 +231,53 @@ int main(int argc, char **argv)
register_parameter("disk-operations-hard", 0.2);
register_parameter("disk-operations", 0.0);
- if (argc > 1) {
- if (strcmp(argv[1], "--calibrate") == 0)
- calibrate();
- }
- if (argc > 1) {
- if (strcmp(argv[1], "--debug") == 0)
- debug_learning = 1;
- }
+ while (1) { /* parse commandline options */
+ c = getopt_long (argc, argv, "ch:uV", long_options, &option_index);
- if (argc > 2) {
- if (strcmp(argv[2], "--debug") == 0)
- debug_learning = 1;
- }
+ /* Detect the end of the options. */
+ if (c == -1)
+ break;
- if (argc > 1) {
- if (strcmp(argv[1], "--html") == 0) {
- fprintf(stderr, _("Measuring for 20 seconds\n"));
- /* one to warm up everything */
- utf_ok = 0;
- one_measurement(1);
- init_html_output("powertop.html");
- initialize_tuning();
- /* and then the real measurement */
- one_measurement(20);
- html_show_tunables();
-
- finish_html_output();
-
- /* and wrap up */
- learn_parameters(50, 0);
- save_all_results("/var/cache/powertop/saved_results.powertop");
- save_parameters("/var/cache/powertop/saved_parameters.powertop");
- end_pci_access();
- exit(0);
+ switch (c) {
+ case 'V':
+ print_version();
+ exit(0);
+ break;
+
+ case 'u':
+ print_usage();
+ exit(0);
+ break;
+
+ case 'c':
+ calibrate();
+ break;
+
+ case 'h': /* html report */
+ fprintf(stderr, _("Measuring for 20 seconds\n"));
+ /* one to warm up everything */
+ utf_ok = 0;
+ one_measurement(1);
+ init_html_output( (optarg ? optarg : "powertop.html"));
+ initialize_tuning();
+ /* and then the real measurement */
+ one_measurement(20);
+ html_show_tunables();
+
+ finish_html_output();
+
+ /* and wrap up */
+ learn_parameters(50, 0);
+ save_all_results("/var/cache/powertop/saved_results.powertop");
+ save_parameters("/var/cache/powertop/saved_parameters.powertop");
+ end_pci_access();
+ exit(0);
+ break;
+
+ case '?': /* Unknown option */
+ /* getopt_long already printed an error message. */
+ break;
}
}
diff --git a/po/powertop.pot b/po/powertop.pot
index 800f135..1342857 100644
--- a/po/powertop.pot
+++ b/po/powertop.pot
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2011-01-10 21:48-0800\n"
+"POT-Creation-Date: 2011-04-21 13:20-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -88,6 +88,31 @@ msgstr ""
msgid "%s device %s has no runtime power management"
msgstr ""
+#: main.cpp:81
+#, c-format
+msgid "--calibrate \t runs powertop in calibration mode\n"
+msgstr ""
+
+#: main.cpp:79
+#, c-format
+msgid "--debug \t run in \"debug\" mode\n"
+msgstr ""
+
+#: main.cpp:83
+#, c-format
+msgid "--help \t\t print this help menu\n"
+msgstr ""
+
+#: main.cpp:82
+#, c-format
+msgid "--html[=FILENAME]\t\t generate a html report\n"
+msgstr ""
+
+#: main.cpp:80
+#, c-format
+msgid "--version \t print version information\n"
+msgstr ""
+
#: calibrate/calibrate.cpp:350 calibrate/calibrate.cpp:367
#: calibrate/calibrate.cpp:375 calibrate/calibrate.cpp:392
#, c-format
@@ -258,6 +283,13 @@ msgstr ""
msgid "Finishing PowerTOP power estimate calibration \n"
msgstr ""
+#: main.cpp:85
+#, c-format
+msgid ""
+"For more help please refer to the README\n"
+"\n"
+msgstr ""
+
#: display.cpp:72
msgid "Frequency stats"
msgstr ""
@@ -281,7 +313,7 @@ msgstr ""
msgid "Intel built in USB hub"
msgstr ""
-#: main.cpp:269
+#: main.cpp:317
#, c-format
msgid "Leaving PowerTOP\n"
msgstr ""
@@ -291,7 +323,7 @@ msgstr ""
msgid "Loaded %i prior measurements\n"
msgstr ""
-#: main.cpp:215
+#: main.cpp:258
#, c-format
msgid "Measuring for 20 seconds\n"
msgstr ""
@@ -345,7 +377,7 @@ msgstr ""
msgid "Power est. Usage Device name\n"
msgstr ""
-#: main.cpp:172
+#: main.cpp:208
#, c-format
msgid "PowerTOP "
msgstr ""
@@ -355,6 +387,11 @@ msgstr ""
msgid "PowerTOP %s needs the kernel to support the 'perf' subsystem\n"
msgstr ""
+#: main.cpp:73
+#, c-format
+msgid "Powertop version"
+msgstr ""
+
#: devices/rfkill.cpp:64 devices/rfkill.cpp:68
#, c-format
msgid "Radio device: %s"
@@ -429,6 +466,13 @@ msgstr ""
msgid "Unknown"
msgstr ""
+#: main.cpp:78
+#, c-format
+msgid ""
+"Usage: powertop [OPTIONS]\n"
+"\n"
+msgstr ""
+
#: tuning/cpufreq.cpp:45
#, c-format
msgid "Using 'ondemand' cpufreq governor"
@@ -462,7 +506,7 @@ msgstr ""
msgid "cpu package %i"
msgstr ""
-#: main.cpp:173
+#: main.cpp:209
#, c-format
msgid "exiting...\n"
msgstr ""