Remove command-line option inconsistencies

Optional arguments, as parsed by the getopt_long mechanism, are
inherently inconsistent with how you define required arguments.

To avoid confusion we decide to avoid this inconsistency by replacing
optional options with additional options with required argmuments.
This commit is contained in:
Martin Lund 2022-06-04 19:07:11 +02:00
parent 8d2ad8d82d
commit 9476422922
6 changed files with 103 additions and 59 deletions

View file

@ -38,9 +38,11 @@ The command-line interface is straightforward as reflected in the output from
-o, --output-delay <ms> Character output delay (default: 0) -o, --output-delay <ms> Character output delay (default: 0)
-n, --no-autoconnect Disable automatic connect -n, --no-autoconnect Disable automatic connect
-e, --local-echo Enable local echo -e, --local-echo Enable local echo
-t, --timestamp[=<format>] Enable timestamp (default: 24hour) -t, --timestamp Enable line timestamp
--timestamp-format <format> Set timestamp format (default: 24hour)
-L, --list-devices List available serial devices -L, --list-devices List available serial devices
-l, --log[=<filename>] Log to file -l, --log Enable log to file
--log-filename <filename> Set log filename
-m, --map <flags> Map special characters -m, --map <flags> Map special characters
-c, --color <0..255> Colorize tio text -c, --color <0..255> Colorize tio text
-S, --socket <socket> Listen on socket -S, --socket <socket> Listen on socket
@ -95,13 +97,13 @@ color = 46
[ftdi] [ftdi]
tty = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0 tty = /dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTGQVXBL-if00-port0
baudrate = 9600 baudrate = 9600
no-autoconnect = 1 no-autoconnect = enable
color = 12 color = 12
[usb devices] [usb devices]
pattern=usb([0-9]*) pattern=usb([0-9]*)
tty = /dev/ttyUSB%s tty = /dev/ttyUSB%s
log = 1 log = enable
log-filename = usb.log log-filename = usb.log
color = 13 color = 13
``` ```

3
TODO Normal file
View file

@ -0,0 +1,3 @@
* Improve error/warning messaging when parsing configuration file

View file

@ -57,9 +57,14 @@ option is provided, tio will exit if the device is not present or an established
Enable local echo. Enable local echo.
.TP .TP
.BR \-t ", " \-\-timestamp[=\fI<format>\fR\fB] .BR \-t ", " \-\-timestamp
Enable timestamp. Optionally you can specify any of the following timestamp formats: Enable line timestamp.
.TP
.BR " \-\-timestamp-format \fI<format>
Set timestamp format to any of the following timestamp formats:
.RS .RS
.TP 16n .TP 16n
.IP "\fB24hour" .IP "\fB24hour"
@ -79,9 +84,14 @@ Default format is
List available serial devices. List available serial devices.
.TP .TP
.BR \-l ", " \-\-log[=\fI<filename>\fR\fB] .BR \-l ", " \-\-log
Log to file. If no filename is provided the filename will be automatically generated. Enable log to file. If no filename is provided the filename will be automatically generated.
.TP
.BR " \-\-log-filename \fI<filename>
Set log filename.
.TP .TP
.BR \-m ", " "\-\-map " \fI<flags> .BR \-m ", " "\-\-map " \fI<flags>
@ -188,6 +198,7 @@ Options without any config section name sets the default options.
.TP .TP
The following configuration file options are available: The following configuration file options are available:
.TP 20n
.IP "\fBpattern" .IP "\fBpattern"
pattern matching user input. This pattern can be an extended regular expression with a single group. pattern matching user input. This pattern can be an extended regular expression with a single group.
.IP "\fBtty" .IP "\fBtty"
@ -207,13 +218,15 @@ Set output delay
.IP "\fBno-autoconnect" .IP "\fBno-autoconnect"
Disable automatic connect Disable automatic connect
.IP "\fBlog" .IP "\fBlog"
Log to file Enable log to file
.IP "\fBlog-filename" .IP "\fBlog-filename"
Set log filename Set log filename
.IP "\fBlocal-echo" .IP "\fBlocal-echo"
Enable local echo Enable local echo
.IP "\fBtimestamp" .IP "\fBtimestamp"
Prefix each new line with a timestamp Enable line timestamp
.IP "\fBtimestamp-format"
Set timestamp format
.IP "\fBmap" .IP "\fBmap"
Map special characters on input or output Map special characters on input or output
.IP "\fBcolor" .IP "\fBcolor"

View file

@ -144,6 +144,13 @@ static int data_handler(void *user, const char *section, const char *name,
} }
} }
else if (!strcmp(name, "timestamp")) else if (!strcmp(name, "timestamp"))
{
if (!strcmp(value, "enable"))
{
option.timestamp = TIMESTAMP_24HOUR;
}
}
else if (!strcmp(name, "timestamp-format"))
{ {
option.timestamp = timestamp_option_parse(value); option.timestamp = timestamp_option_parse(value);
} }

View file

@ -35,6 +35,13 @@
#include "misc.h" #include "misc.h"
#include "print.h" #include "print.h"
enum opt_t
{
OPT_NONE,
OPT_TIMESTAMP_FORMAT,
OPT_LOG_FILENAME,
};
/* Default options */ /* Default options */
struct option_t option = struct option_t option =
{ {
@ -69,9 +76,11 @@ void print_help(char *argv[])
printf(" -o, --output-delay <ms> Output delay (default: 0)\n"); printf(" -o, --output-delay <ms> Output delay (default: 0)\n");
printf(" -n, --no-autoconnect Disable automatic connect\n"); printf(" -n, --no-autoconnect Disable automatic connect\n");
printf(" -e, --local-echo Enable local echo\n"); printf(" -e, --local-echo Enable local echo\n");
printf(" -t, --timestamp[=<format>] Enable timestamp (default: 24hour)\n"); printf(" -t, --timestamp Enable line timestamp\n");
printf(" --timestamp-format <format> Set timestamp format (default: 24hour)\n");
printf(" -L, --list-devices List available serial devices\n"); printf(" -L, --list-devices List available serial devices\n");
printf(" -l, --log[=<filename>] Log to file\n"); printf(" -l, --log Enable log to file\n");
printf(" --log-filename <filename> Set log filename\n");
printf(" -m, --map <flags> Map special characters\n"); printf(" -m, --map <flags> Map special characters\n");
printf(" -c, --color <code> Colorize tio text\n"); printf(" -c, --color <code> Colorize tio text\n");
printf(" -S, --socket <socket> Listen on socket\n"); printf(" -S, --socket <socket> Listen on socket\n");
@ -129,7 +138,7 @@ enum timestamp_t timestamp_option_parse(const char *arg)
} }
else else
{ {
printf("Warning: Unknown timestamp type, falling back to '24hour' default format\n"); warning_printf("Unknown timestamp type, falling back to '24hour' default format");
} }
} }
@ -178,9 +187,11 @@ void options_parse(int argc, char *argv[])
{"output-delay", required_argument, 0, 'o' }, {"output-delay", required_argument, 0, 'o' },
{"no-autoconnect", no_argument, 0, 'n' }, {"no-autoconnect", no_argument, 0, 'n' },
{"local-echo", no_argument, 0, 'e' }, {"local-echo", no_argument, 0, 'e' },
{"timestamp", optional_argument, 0, 't'}, {"timestamp", no_argument, 0, 't' },
{"timestamp-format", required_argument, 0, OPT_TIMESTAMP_FORMAT },
{"list-devices", no_argument, 0, 'L' }, {"list-devices", no_argument, 0, 'L' },
{"log", optional_argument, 0, 'l'}, {"log", no_argument, 0, 'l' },
{"log-filename", required_argument, 0, OPT_LOG_FILENAME },
{"socket", required_argument, 0, 'S' }, {"socket", required_argument, 0, 'S' },
{"map", required_argument, 0, 'm' }, {"map", required_argument, 0, 'm' },
{"color", required_argument, 0, 'c' }, {"color", required_argument, 0, 'c' },
@ -193,7 +204,7 @@ void options_parse(int argc, char *argv[])
int option_index = 0; int option_index = 0;
/* Parse argument using getopt_long */ /* Parse argument using getopt_long */
c = getopt_long(argc, argv, "b:d:f:s:p:o:net::Ll::S:m:c:vh", long_options, &option_index); c = getopt_long(argc, argv, "b:d:f:s:p:o:netLlS:m:c:vh", long_options, &option_index);
/* Detect the end of the options */ /* Detect the end of the options */
if (c == -1) if (c == -1)
@ -244,6 +255,10 @@ void options_parse(int argc, char *argv[])
break; break;
case 't': case 't':
option.timestamp = TIMESTAMP_24HOUR;
break;
case OPT_TIMESTAMP_FORMAT:
option.timestamp = timestamp_option_parse(optarg); option.timestamp = timestamp_option_parse(optarg);
break; break;
@ -253,6 +268,9 @@ void options_parse(int argc, char *argv[])
case 'l': case 'l':
option.log = true; option.log = true;
break;
case OPT_LOG_FILENAME:
option.log_filename = optarg; option.log_filename = optarg;
break; break;

View file

@ -33,6 +33,7 @@ enum timestamp_t
TIMESTAMP_24HOUR_START, TIMESTAMP_24HOUR_START,
TIMESTAMP_ISO8601, TIMESTAMP_ISO8601,
}; };
const char* timestamp_token(enum timestamp_t timestamp); const char* timestamp_token(enum timestamp_t timestamp);
enum timestamp_t timestamp_option_parse(const char *arg); enum timestamp_t timestamp_option_parse(const char *arg);