[v0.96] Add short options. Improve --help flag. Update man page

This commit is contained in:
Dr-Noob
2021-03-31 12:40:19 +02:00
parent 8abbd8f69f
commit ca7091bc5e
6 changed files with 205 additions and 152 deletions

View File

@@ -1,57 +1,64 @@
.TH man 8 "1 Sep 2020" "0.7" "cpufetch man page" .\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.48.2.
.TH CPUFETCH "1" "March 2021" "cpufetch v0.96 (x86_64 build)" "User Commands"
.SH NAME .SH NAME
cpufetch \- Simplistic yet fancy CPU architecture fetching tool cpufetch \- manual page for cpufetch v0.96 (x86_64 build)
.SH SYNOPSIS .SH SYNOPSIS
cpufetch [--version] [--help] [--levels] [--style fancy|retro|legacy] [--color intel|amd|'R,G,B:R,G,B:R,G,B:R,G,B'] .B cpufetch
[\fI\,OPTION\/\fR]...
.SH DESCRIPTION .SH DESCRIPTION
cpufetch will print CPU information, for which will query CPUID instructions and udev directories on Linux as a fallback method. Some of this features are: Simplistic yet fancy CPU architecture fetching tool
.IP \[bu] 2
Name
.IP \[bu]
Frequency
.IP \[bu]
Number of cores (Physical and Logical)
.IP \[bu]
Cache sizes
.IP \[bu]
Theoretical peak performance in floating point operations per second (FLOP/s)
.SH OPTIONS .SH OPTIONS
.TP .TP
\fB\-\-style\fR \f[I][intel|amd|R,G,B:R,G,B:R,G,B:R,G,B]\f[] \fB\-c\fR, \fB\-\-color\fR
Set the color scheme. By default, cpufetch uses the system color scheme. This option lets the user use different colors to print the CPU art: Set the color scheme (by default, cpufetch uses the system color scheme)
.IP \[bu]
\fB"intel"\fR: Use intel color scheme
.IP \[bu]
\fB"amd"\fR: Use amd color scheme
.IP \[bu]
\fBcustom\fR: If color do not match "intel" or "amd", a custom scheme can be specified: 4 colors must be given in RGB with the format: R,G,B:R,G,B:...
These colors correspond to CPU art color (2 colors) and for the text colors (following 2)
.TP .TP
\fB\-\-style\fR \f[I]STYLE\f[] \fB\-s\fR, \fB\-\-style\fR
Specify the style of ascii logo: Set the style of CPU art
.IP \[bu]
\fB"fancy"\fR: Default style
.IP \[bu]
\fB"retro"\fR: Old cpufetch style
.IP \[bu]
\fB"legacy"\fR: Fallback style for terminals that does not support colors
.TP .TP
\fB\-\-levels\fR \fB\-d\fR, \fB\-\-debug\fR
Prints CPUID levels and CPU name Prints CPU model and cpuid levels (debug purposes)
.TP .TP
\fB\-\-verbose\fR \fB\-v\fR, \fB\-\-verbose\fR
Prints extra information (if available) about how cpufetch tried fetching information Prints extra information (if available) about how cpufetch tried fetching information
.TP .TP
\fB\-\-help\fR \fB\-h\fR, \fB\-\-help\fR
Prints help Prints this help and exit
.TP .TP
\fB\-\-version\fR \fB\-V\fR, \fB\-\-version\fR
Prints cpufetch version Prints cpufetch version and exit
.SH BUGS .SH "COLORS:"
Bugs should be posted on: https://github.com/Dr-Noob/cpufetch/issues .TP
.SH NOTES * "intel":
Peak performance information is NOT accurate. cpufetch computes peak performance using the max Use Intel default color scheme
frequency. However, to properly compute peak performance, you need to know the frequency of the .TP
CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. * "amd":
.SH AUTHOR Use AMD default color scheme
.TP
* "arm":
Use ARM default color scheme
.TP
* custom:
If color argument do not match "intel", "amd" or "arm", a custom scheme can be specified.
4 colors must be given in RGB with the format: R,G,B:R,G,B:...The first 2 colors are the CPU art color and the next 2 colors are the text colors
.SH "STYLES:"
.TP
* "fancy":
Default style
.TP
* "retro":
Old cpufetch style
.TP
* "legacy":
Fallback style for terminals that do not support colors
.SH "EXAMPLES:"
Run peakperf with Intel color scheme:
\&./cpufetch \fB\-\-color\fR intel
Run peakperf with a custom color scheme:
\&./cpufetch \fB\-\-color\fR 239,90,45:210,200,200:100,200,45:0,200,200
.SH "BUGS:"
Report bugs to https://github.com/Dr\-Noob/cpufetch/issues
.SH "NOTE:"
Peak performance information is NOT accurate. cpufetch computes peak performance using the max frequency. However, to properly compute peak performance, you need to know the frequency of the CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. For peak performance measurement see: https://github.com/Dr\-Noob/peakperf
.SH "AUTHOR:"
Dr-Noob (https://github.com/Dr-Noob) Dr-Noob (https://github.com/Dr-Noob)

View File

@@ -17,15 +17,6 @@ static const char *SYTLES_STR_LIST[] = {
[STYLE_INVALID] = NULL [STYLE_INVALID] = NULL
}; };
enum {
ARG_CHAR_STYLE,
ARG_CHAR_COLOR,
ARG_CHAR_HELP,
ARG_CHAR_DEBUG,
ARG_CHAR_VERBOSE,
ARG_CHAR_VERSION
};
struct args_struct { struct args_struct {
bool debug_flag; bool debug_flag;
bool help_flag; bool help_flag;
@@ -35,6 +26,24 @@ struct args_struct {
struct colors* colors; struct colors* colors;
}; };
const char args_chr[] = {
/* [ARG_CHAR_STYLE] = */ 's',
/* [ARG_CHAR_COLOR] = */ 'c',
/* [ARG_CHAR_HELP] = */ 'h',
/* [ARG_CHAR_DEBUG] = */ 'd',
/* [ARG_CHAR_VERBOSE] = */ 'v',
/* [ARG_CHAR_VERSION] = */ 'V',
};
const char *args_str[] = {
/* [ARG_CHAR_STYLE] = */ "style",
/* [ARG_CHAR_COLOR] = */ "color",
/* [ARG_CHAR_HELP] = */ "help",
/* [ARG_CHAR_DEBUG] = */ "debug",
/* [ARG_CHAR_VERBOSE] = */ "verbose",
/* [ARG_CHAR_VERSION] = */ "version",
};
static struct args_struct args; static struct args_struct args;
STYLE get_style() { STYLE get_style() {
@@ -61,6 +70,15 @@ bool verbose_enabled() {
return args.verbose_flag; return args.verbose_flag;
} }
int max_arg_str_length() {
int max_len = -1;
int len = sizeof(args_str) / sizeof(args_str[0]);
for(int i=0; i < len; i++) {
max_len = max(max_len, (int) strlen(args_str[i]));
}
return max_len;
}
STYLE parse_style(char* style) { STYLE parse_style(char* style) {
uint8_t i = 0; uint8_t i = 0;
uint8_t styles_count = sizeof(SYTLES_STR_LIST) / sizeof(SYTLES_STR_LIST[0]); uint8_t styles_count = sizeof(SYTLES_STR_LIST) / sizeof(SYTLES_STR_LIST[0]);
@@ -158,8 +176,21 @@ bool parse_color(char* optarg_str, struct colors** cs) {
return true; return true;
} }
char* build_short_options() {
const char *c = args_chr;
int len = sizeof(args_chr) / sizeof(args_chr[0]);
char* str = (char *) malloc(sizeof(char) * (len*2 + 1));
memset(str, 0, sizeof(char) * (len*2 + 1));
sprintf(str, "%c:%c:%c%c%c%c",
c[ARG_STYLE], c[ARG_COLOR], c[ARG_HELP],
c[ARG_DEBUG], c[ARG_VERBOSE], c[ARG_VERSION]);
return str;
}
bool parse_args(int argc, char* argv[]) { bool parse_args(int argc, char* argv[]) {
int c; int opt;
int option_index = 0; int option_index = 0;
opterr = 0; opterr = 0;
@@ -170,20 +201,21 @@ bool parse_args(int argc, char* argv[]) {
args.style = STYLE_EMPTY; args.style = STYLE_EMPTY;
args.colors = NULL; args.colors = NULL;
static struct option long_options[] = { const struct option long_options[] = {
{"style", required_argument, 0, ARG_CHAR_STYLE }, {args_str[ARG_STYLE], required_argument, 0, args_chr[ARG_STYLE] },
{"color", required_argument, 0, ARG_CHAR_COLOR }, {args_str[ARG_COLOR], required_argument, 0, args_chr[ARG_COLOR] },
{"help", no_argument, 0, ARG_CHAR_HELP }, {args_str[ARG_HELP], no_argument, 0, args_chr[ARG_HELP] },
{"debug", no_argument, 0, ARG_CHAR_DEBUG }, {args_str[ARG_DEBUG], no_argument, 0, args_chr[ARG_DEBUG] },
{"verbose", no_argument, 0, ARG_CHAR_VERBOSE }, {args_str[ARG_VERBOSE], no_argument, 0, args_chr[ARG_VERBOSE] },
{"version", no_argument, 0, ARG_CHAR_VERSION }, {args_str[ARG_VERSION], no_argument, 0, args_chr[ARG_VERSION] },
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
c = getopt_long(argc, argv, "", long_options, &option_index); char* short_options = build_short_options();
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (c != -1) { while (!args.help_flag && !args.debug_flag && !args.version_flag && opt != -1) {
if(c == ARG_CHAR_COLOR) { if(opt == args_chr[ARG_COLOR]) {
if(color_flag) { if(color_flag) {
printErr("Color option specified more than once"); printErr("Color option specified more than once");
return false; return false;
@@ -194,7 +226,7 @@ bool parse_args(int argc, char* argv[]) {
return false; return false;
} }
} }
else if(c == ARG_CHAR_STYLE) { else if(opt == args_chr[ARG_STYLE]) {
if(args.style != STYLE_EMPTY) { if(args.style != STYLE_EMPTY) {
printErr("Style option specified more than once"); printErr("Style option specified more than once");
return false; return false;
@@ -204,45 +236,27 @@ bool parse_args(int argc, char* argv[]) {
printErr("Invalid style '%s'",optarg); printErr("Invalid style '%s'",optarg);
return false; return false;
} }
}
else if(c == ARG_CHAR_HELP) {
if(args.help_flag) {
printErr("Help option specified more than once");
return false;
}
args.help_flag = true;
}
else if(c == ARG_CHAR_VERBOSE) {
if(args.verbose_flag) {
printErr("Verbose option specified more than once");
return false;
}
args.verbose_flag = true;
}
else if(c == ARG_CHAR_DEBUG) {
if(args.debug_flag) {
printErr("Debug option specified more than once");
return false;
}
args.debug_flag = true;
}
else if (c == ARG_CHAR_VERSION) {
if(args.version_flag) {
printErr("Version option specified more than once");
return false;
}
args.version_flag = true;
}
else if(c == '?') {
printWarn("Invalid options");
args.help_flag = true;
break; break;
} }
else else if(opt == args_chr[ARG_HELP]) {
printBug("Bug at line number %d in file %s", __LINE__, __FILE__); args.help_flag = true;
}
else if(opt == args_chr[ARG_VERBOSE]) {
args.verbose_flag = true;
}
else if(opt == args_chr[ARG_DEBUG]) {
args.debug_flag = true;
}
else if(opt == args_chr[ARG_VERSION]) {
args.version_flag = true;
}
else {
printWarn("Invalid options");
args.help_flag = true;
}
option_index = 0; option_index = 0;
c = getopt_long(argc, argv,"",long_options, &option_index); opt = getopt_long(argc, argv, short_options, long_options, &option_index);
} }
if(optind < argc) { if(optind < argc) {

View File

@@ -26,8 +26,21 @@ enum {
STYLE_INVALID STYLE_INVALID
}; };
enum {
ARG_STYLE,
ARG_COLOR,
ARG_HELP,
ARG_DEBUG,
ARG_VERBOSE,
ARG_VERSION
};
extern const char args_chr[];
extern const char *args_str[];
#include "printer.h" #include "printer.h"
int max_arg_str_length();
bool parse_args(int argc, char* argv[]); bool parse_args(int argc, char* argv[]);
bool show_help(); bool show_help();
bool show_debug(); bool show_debug();

View File

@@ -64,3 +64,7 @@ void set_log_level(bool verbose) {
if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE; if(verbose) LOG_LEVEL = LOG_LEVEL_VERBOSE;
else LOG_LEVEL = LOG_LEVEL_NORMAL; else LOG_LEVEL = LOG_LEVEL_NORMAL;
} }
int max(int a, int b) {
return a > b ? a : b;
}

View File

@@ -7,5 +7,6 @@ void set_log_level(bool verbose);
void printWarn(const char *fmt, ...); void printWarn(const char *fmt, ...);
void printErr(const char *fmt, ...); void printErr(const char *fmt, ...);
void printBug(const char *fmt, ...); void printBug(const char *fmt, ...);
int max(int a, int b);
#endif #endif

View File

@@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "args.h" #include "args.h"
#include "printer.h" #include "printer.h"
@@ -16,38 +17,52 @@
static const char* VERSION = "0.96"; static const char* VERSION = "0.96";
void print_help(char *argv[]) { void print_help(char *argv[]) {
printf("Usage: %s [--version] [--help] [--debug] [--style \"fancy\"|\"retro\"|\"legacy\"] [--color \"intel\"|\"amd\"|'R,G,B:R,G,B:R,G,B:R,G,B']\n\n", argv[0]); const char **t = args_str;
const char *c = args_chr;
int max_len = max_arg_str_length();
printf("Options: \n\ printf("Usage: %s [OPTION]...\n", argv[0]);
--color Set the color scheme. By default, cpufetch uses the system color scheme. This option \n\ printf("Simplistic yet fancy CPU architecture fetching tool\n\n");
lets the user use different colors to print the CPU art: \n\
* \"intel\": Use Intel default color scheme \n\
* \"amd\": Use AMD default color scheme \n\
* \"arm\": Use ARM default color scheme \n\
* custom: If color argument do not match \"Intel\", \"AMD\" or \"ARM\", a custom scheme can be specified: \n\
4 colors must be given in RGB with the format: R,G,B:R,G,B:... \n\
These colors correspond to CPU art color (2 colors) and for the text colors (following 2) \n\
For example: --color 239,90,45:210,200,200:100,200,45:0,200,200 \n\n\
--style Set the style of CPU art: \n\
* \"fancy\": Default style \n\
* \"retro\": Old cpufetch style \n\
* \"legacy\": Fallback style for terminals that does not support colors \n\n");
printf("Options: \n");
printf(" -%c, --%s %*s Set the color scheme (by default, cpufetch uses the system color scheme)\n", c[ARG_COLOR], t[ARG_COLOR], (int) (max_len-strlen(t[ARG_COLOR])), "");
printf(" -%c, --%s %*s Set the style of CPU art\n", c[ARG_STYLE], t[ARG_STYLE], (int) (max_len-strlen(t[ARG_STYLE])), "");
#ifdef ARCH_X86 #ifdef ARCH_X86
printf(" --debug Prints CPU model and cpuid levels (debug purposes)\n\n"); printf(" -%c, --%s %*s Prints CPU model and cpuid levels (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
#elif ARCH_ARM #elif ARCH_ARM
printf(" --debug Prints main ID register values for all cores (debug purposes)\n\n"); printf(" -%c, --%s %*s Prints main ID register values for all cores (debug purposes)\n", c[ARG_DEBUG], t[ARG_DEBUG], (int) (max_len-strlen(t[ARG_DEBUG])), "");
#endif #endif
printf(" -%c, --%s %*s Prints extra information (if available) about how cpufetch tried fetching information\n", c[ARG_VERBOSE], t[ARG_VERBOSE], (int) (max_len-strlen(t[ARG_VERBOSE])), "");
printf(" -%c, --%s %*s Prints this help and exit\n", c[ARG_HELP], t[ARG_HELP], (int) (max_len-strlen(t[ARG_HELP])), "");
printf(" -%c, --%s %*s Prints cpufetch version and exit\n", c[ARG_VERSION], t[ARG_VERSION], (int) (max_len-strlen(t[ARG_VERSION])), "");
printf(" --verbose Prints extra information (if available) about how cpufetch tried fetching information\n\n\ printf("\nCOLORS: \n");
--help Prints this help and exit\n\n\ printf(" * \"intel\": Use Intel default color scheme \n");
--version Prints cpufetch version and exit\n\n\ printf(" * \"amd\": Use AMD default color scheme \n");
\n\ printf(" * \"arm\": Use ARM default color scheme \n");
NOTES: \n\ printf(" * custom: If color argument do not match \"intel\", \"amd\" or \"arm\", a custom scheme can be specified.\n");
- Bugs or improvements should be submitted to: github.com/Dr-Noob/cpufetch/issues \n\ printf(" 4 colors must be given in RGB with the format: R,G,B:R,G,B:...\n");
- Peak performance information is NOT accurate. cpufetch computes peak performance using the max \n\ printf(" The first 2 colors are the CPU art color and the next 2 colors are the text colors\n");
frequency. However, to properly compute peak performance, you need to know the frequency of the \n\
CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU. \n"); printf("\nSTYLES: \n");
printf(" * \"fancy\": Default style\n");
printf(" * \"retro\": Old cpufetch style\n");
printf(" * \"legacy\": Fallback style for terminals that do not support colors\n");
printf("\nEXAMPLES: \n");
printf(" Run peakperf with Intel color scheme:\n");
printf(" ./cpufetch --color intel\n");
printf(" Run peakperf with a custom color scheme:\n");
printf(" ./cpufetch --color 239,90,45:210,200,200:100,200,45:0,200,200\n");
printf("\nBUGS: \n");
printf(" Report bugs to https://github.com/Dr-Noob/cpufetch/issues\n");
printf("\nNOTE: \n");
printf(" Peak performance information is NOT accurate. cpufetch computes peak performance using the max\n");
printf(" frequency. However, to properly compute peak performance, you need to know the frequency of the\n");
printf(" CPU running AVX code, which is not be fetched by cpufetch since it depends on each specific CPU.\n");
printf(" For peak performance measurement see: https://github.com/Dr-Noob/peakperf\n");
} }
void print_version() { void print_version() {
@@ -59,7 +74,6 @@ int main(int argc, char* argv[]) {
return EXIT_FAILURE; return EXIT_FAILURE;
if(show_help()) { if(show_help()) {
print_version();
print_help(argv); print_help(argv);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }