From 818e0e869837734378247ddb1e5b066e44bc71c6 Mon Sep 17 00:00:00 2001 From: Hans Ulrich Niedermann Date: Wed, 21 Feb 2024 11:48:20 +0100 Subject: [PATCH] Add long options like --part Switch the command line argument parser from getopt(3) to getopt_long(3) and add a few long options as aliases to existing short options, e.g. "--help" and "--part" being aliases for "-?" and "-p", respectively. The getopt_long(3) function is available on GNU, BSD, and the existing msvc/getopt.[ch] already implements getopt_long() on Windows. This should cover all systems avrdude supports. Adapt the avrdude usage message shown by "-?" or "--help" to show the new long options. TODO: Adapt man page and texi manual reflecting the long options. Closes: #1922 --- src/main.c | 96 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index cec73e250..a26002328 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -237,39 +238,41 @@ static void usage(void) { msg_error("Usage: %s [options]\n" "Options:\n" - " -p Specify AVR device; -p ? lists all known parts\n" - " -p / Run developer options for matched AVR devices,\n" - " e.g., -p ATmega328P/s or /S for part definition\n" - " -b Override RS-232 baud rate\n" - " -B Specify bit clock period (us)\n" - " -C Specify location of configuration file\n" - " -C + Specify additional config file, can be repeated\n" - " -N Do not load %s%s\n" - " -c Specify programmer; -c ? and -c ?type list all\n" - " -c / Run developer options for matched programmers,\n" - " e.g., -c 'ur*'/s for programmer info/definition\n" - " -A Disable trailing-0xff removal for file/AVR read\n" - " -D Disable auto-erase for flash memory; implies -A\n" - " -i ISP Clock Delay [in microseconds]\n" - " -P Connection; -P ?s or -P ?sa lists serial ones\n" - " -r Reconnect to -P port after \"touching\" it; wait\n" - " 400 ms for each -r; needed for some USB boards\n" - " -F Override invalid signature or initial checks\n" - " -e Perform a chip erase at the beginning\n" - " -O Perform RC oscillator calibration (see AVR053)\n" - " -t Run an interactive terminal when it is its turn\n" - " -T Run terminal line when it is its turn\n" - " -U :r|w|v:[:format]\n" - " Carry out memory operation when it is its turn\n" - " Multiple -t, -T and -U options can be specified\n" - " -n Do not write to the device whilst processing -U\n" - " -V Do not automatically verify during -U\n" - " -E [,] List programmer exit specifications\n" - " -x Pass to programmer, see -x help\n" - " -v Verbose output; -v -v for more\n" - " -q Quell progress output; -q -q for less\n" - " -l logfile Use logfile rather than stderr for diagnostics\n" - " -? Display this usage\n" + " -p Specify AVR device; -p ? lists all known parts\n" + " -p / Run developer options for matched AVR devices,\n" + " e.g., -p ATmega328P/s or /S for part definition\n" + " -b, --baud Override RS-232 baud rate\n" + " -B, --bitclock Specify bit clock period (us)\n" + " -C Specify location of configuration file\n" + " -C + Specify additional config file, can be repeated\n" + " -N Do not load %s%s\n" + " -c, --programmer \n" + " Specify programmer; -c ? and -c ?type list all\n" + " -c, --programmer /\n" + " Run developer options for matched programmers,\n" + " e.g., -c 'ur*'/s for programmer info/definition\n" + " -A Disable trailing-0xff removal for file/AVR read\n" + " -D, --noerase Disable auto-erase for flash memory; implies -A\n" + " -i ISP Clock Delay [in microseconds]\n" + " -P, --port Connection; -P ?s or -P ?sa lists serial ones\n" + " -r, --reconnect Reconnect to -P port after \"touching\" it; wait\n" + " 400 ms for each -r; needed for some USB boards\n" + " -F Override invalid signature or initial checks\n" + " -e, --erase Perform a chip erase at the beginning\n" + " -O Perform RC oscillator calibration (see AVR053)\n" + " -t, --terminal, --tty Run an interactive terminal when it is its turn\n" + " -T Run terminal line when it is its turn\n" + " -U, --memory :r|w|v:[:format]\n" + " Carry out memory operation when it is its turn\n" + " Multiple -t, -T and -U options can be specified\n" + " -n, --test Do not write to the device whilst processing -U\n" + " -V, --noverify Do not automatically verify during -U\n" + " -E [,] List programmer exit specifications\n" + " -x Pass to programmer, see -x help\n" + " -v, --verbose Verbose output; -v -v for more\n" + " -q Quell progress output; -q -q for less\n" + " -l, --logfile logfile Use logfile rather than stderr for diagnostics\n" + " -?, --help Display this usage\n" "\navrdude version %s, https://github.com/avrdudes/avrdude\n", progname, strlen(cfg) < 24? "config file ": "", cfg, AVRDUDE_FULL_VERSION); @@ -814,7 +817,32 @@ int main(int argc, char *argv[]) { #endif // Process command line arguments - while((ch = getopt(argc, argv, "?Ab:B:c:C:DeE:Fi:l:nNp:OP:qrtT:U:vVx:")) != -1) { + struct option longopts[] = { + {"help", no_argument, NULL, '?'}, + {"baud", required_argument, NULL, 'b'}, + {"bitclock", required_argument, NULL, 'B'}, + {"programmer", required_argument, NULL, 'c'}, + {"config", required_argument, NULL, 'C'}, + {"noerase", no_argument, NULL, 'D'}, + {"erase", no_argument, NULL, 'e'}, + {"logfile", required_argument, NULL, 'l'}, + {"test", no_argument, NULL, 'n'}, + {"noconfig", no_argument, NULL, 'N'}, + {"part", required_argument, NULL, 'p'}, + {"chip", required_argument, NULL, 'p'}, + {"port", required_argument, NULL, 'P'}, + {"quiet", no_argument, NULL, 'q'}, + {"reconnect", no_argument, NULL, 'r'}, + {"terminal", no_argument, NULL, 't'}, + {"tty", no_argument, NULL, 't'}, + {"memory", required_argument, NULL, 'U'}, + {"verbose", no_argument, NULL, 'v'}, + {"noverify", no_argument, NULL, 'V'}, + {NULL, 0, NULL, 0} + }; + while((ch = getopt_long(argc, argv, + "?Ab:B:c:C:DeE:Fi:l:nNp:OP:qrtT:U:vVx:", + longopts, NULL)) != -1) { switch(ch) { case 'b': // Override default programmer baud rate baudrate = str_int(optarg, STR_INT32, &errstr);