Skip to content

Commit

Permalink
Add long options like --part
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ndim committed Nov 27, 2024
1 parent 69c3293 commit 818e0e8
Showing 1 changed file with 62 additions and 34 deletions.
96 changes: 62 additions & 34 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <time.h>
#include <unistd.h>
#include <ctype.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
Expand Down Expand Up @@ -237,39 +238,41 @@ static void usage(void) {

msg_error("Usage: %s [options]\n"
"Options:\n"
" -p <partno> Specify AVR device; -p ? lists all known parts\n"
" -p <wildcard>/<flags> Run developer options for matched AVR devices,\n"
" e.g., -p ATmega328P/s or /S for part definition\n"
" -b <baudrate> Override RS-232 baud rate\n"
" -B <bitclock> Specify bit clock period (us)\n"
" -C <config-file> Specify location of configuration file\n"
" -C +<config-file> Specify additional config file, can be repeated\n"
" -N Do not load %s%s\n"
" -c <programmer> Specify programmer; -c ? and -c ?type list all\n"
" -c <wildcard>/<flags> 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 <delay> ISP Clock Delay [in microseconds]\n"
" -P <port> 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 <terminal cmd line> Run terminal line when it is its turn\n"
" -U <memstr>:r|w|v:<filename>[: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 <exitsp>[,<exitsp>] List programmer exit specifications\n"
" -x <extended_param> Pass <extended_param> 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 <partno> Specify AVR device; -p ? lists all known parts\n"
" -p <wildcard>/<flags> Run developer options for matched AVR devices,\n"
" e.g., -p ATmega328P/s or /S for part definition\n"
" -b, --baud <baudrate> Override RS-232 baud rate\n"
" -B, --bitclock <bitclock> Specify bit clock period (us)\n"
" -C <config-file> Specify location of configuration file\n"
" -C +<config-file> Specify additional config file, can be repeated\n"
" -N Do not load %s%s\n"
" -c, --programmer <programmer>\n"
" Specify programmer; -c ? and -c ?type list all\n"
" -c, --programmer <wildcard>/<flags>\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 <delay> ISP Clock Delay [in microseconds]\n"
" -P, --port <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 <terminal cmd line> Run terminal line when it is its turn\n"
" -U, --memory <memstr>:r|w|v:<filename>[: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 <exitsp>[,<exitsp>] List programmer exit specifications\n"
" -x <extended_param> Pass <extended_param> 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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 818e0e8

Please sign in to comment.