Skip to content

Commit 818e0e8

Browse files
committed
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
1 parent 69c3293 commit 818e0e8

File tree

1 file changed

+62
-34
lines changed

1 file changed

+62
-34
lines changed

src/main.c

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <time.h>
4141
#include <unistd.h>
4242
#include <ctype.h>
43+
#include <getopt.h>
4344
#include <sys/types.h>
4445
#include <sys/stat.h>
4546
#include <sys/time.h>
@@ -237,39 +238,41 @@ static void usage(void) {
237238

238239
msg_error("Usage: %s [options]\n"
239240
"Options:\n"
240-
" -p <partno> Specify AVR device; -p ? lists all known parts\n"
241-
" -p <wildcard>/<flags> Run developer options for matched AVR devices,\n"
242-
" e.g., -p ATmega328P/s or /S for part definition\n"
243-
" -b <baudrate> Override RS-232 baud rate\n"
244-
" -B <bitclock> Specify bit clock period (us)\n"
245-
" -C <config-file> Specify location of configuration file\n"
246-
" -C +<config-file> Specify additional config file, can be repeated\n"
247-
" -N Do not load %s%s\n"
248-
" -c <programmer> Specify programmer; -c ? and -c ?type list all\n"
249-
" -c <wildcard>/<flags> Run developer options for matched programmers,\n"
250-
" e.g., -c 'ur*'/s for programmer info/definition\n"
251-
" -A Disable trailing-0xff removal for file/AVR read\n"
252-
" -D Disable auto-erase for flash memory; implies -A\n"
253-
" -i <delay> ISP Clock Delay [in microseconds]\n"
254-
" -P <port> Connection; -P ?s or -P ?sa lists serial ones\n"
255-
" -r Reconnect to -P port after \"touching\" it; wait\n"
256-
" 400 ms for each -r; needed for some USB boards\n"
257-
" -F Override invalid signature or initial checks\n"
258-
" -e Perform a chip erase at the beginning\n"
259-
" -O Perform RC oscillator calibration (see AVR053)\n"
260-
" -t Run an interactive terminal when it is its turn\n"
261-
" -T <terminal cmd line> Run terminal line when it is its turn\n"
262-
" -U <memstr>:r|w|v:<filename>[:format]\n"
263-
" Carry out memory operation when it is its turn\n"
264-
" Multiple -t, -T and -U options can be specified\n"
265-
" -n Do not write to the device whilst processing -U\n"
266-
" -V Do not automatically verify during -U\n"
267-
" -E <exitsp>[,<exitsp>] List programmer exit specifications\n"
268-
" -x <extended_param> Pass <extended_param> to programmer, see -x help\n"
269-
" -v Verbose output; -v -v for more\n"
270-
" -q Quell progress output; -q -q for less\n"
271-
" -l logfile Use logfile rather than stderr for diagnostics\n"
272-
" -? Display this usage\n"
241+
" -p <partno> Specify AVR device; -p ? lists all known parts\n"
242+
" -p <wildcard>/<flags> Run developer options for matched AVR devices,\n"
243+
" e.g., -p ATmega328P/s or /S for part definition\n"
244+
" -b, --baud <baudrate> Override RS-232 baud rate\n"
245+
" -B, --bitclock <bitclock> Specify bit clock period (us)\n"
246+
" -C <config-file> Specify location of configuration file\n"
247+
" -C +<config-file> Specify additional config file, can be repeated\n"
248+
" -N Do not load %s%s\n"
249+
" -c, --programmer <programmer>\n"
250+
" Specify programmer; -c ? and -c ?type list all\n"
251+
" -c, --programmer <wildcard>/<flags>\n"
252+
" Run developer options for matched programmers,\n"
253+
" e.g., -c 'ur*'/s for programmer info/definition\n"
254+
" -A Disable trailing-0xff removal for file/AVR read\n"
255+
" -D, --noerase Disable auto-erase for flash memory; implies -A\n"
256+
" -i <delay> ISP Clock Delay [in microseconds]\n"
257+
" -P, --port <port> Connection; -P ?s or -P ?sa lists serial ones\n"
258+
" -r, --reconnect Reconnect to -P port after \"touching\" it; wait\n"
259+
" 400 ms for each -r; needed for some USB boards\n"
260+
" -F Override invalid signature or initial checks\n"
261+
" -e, --erase Perform a chip erase at the beginning\n"
262+
" -O Perform RC oscillator calibration (see AVR053)\n"
263+
" -t, --terminal, --tty Run an interactive terminal when it is its turn\n"
264+
" -T <terminal cmd line> Run terminal line when it is its turn\n"
265+
" -U, --memory <memstr>:r|w|v:<filename>[:format]\n"
266+
" Carry out memory operation when it is its turn\n"
267+
" Multiple -t, -T and -U options can be specified\n"
268+
" -n, --test Do not write to the device whilst processing -U\n"
269+
" -V, --noverify Do not automatically verify during -U\n"
270+
" -E <exitsp>[,<exitsp>] List programmer exit specifications\n"
271+
" -x <extended_param> Pass <extended_param> to programmer, see -x help\n"
272+
" -v, --verbose Verbose output; -v -v for more\n"
273+
" -q Quell progress output; -q -q for less\n"
274+
" -l, --logfile logfile Use logfile rather than stderr for diagnostics\n"
275+
" -?, --help Display this usage\n"
273276
"\navrdude version %s, https://github.com/avrdudes/avrdude\n",
274277
progname, strlen(cfg) < 24? "config file ": "", cfg, AVRDUDE_FULL_VERSION);
275278

@@ -814,7 +817,32 @@ int main(int argc, char *argv[]) {
814817
#endif
815818

816819
// Process command line arguments
817-
while((ch = getopt(argc, argv, "?Ab:B:c:C:DeE:Fi:l:nNp:OP:qrtT:U:vVx:")) != -1) {
820+
struct option longopts[] = {
821+
{"help", no_argument, NULL, '?'},
822+
{"baud", required_argument, NULL, 'b'},
823+
{"bitclock", required_argument, NULL, 'B'},
824+
{"programmer", required_argument, NULL, 'c'},
825+
{"config", required_argument, NULL, 'C'},
826+
{"noerase", no_argument, NULL, 'D'},
827+
{"erase", no_argument, NULL, 'e'},
828+
{"logfile", required_argument, NULL, 'l'},
829+
{"test", no_argument, NULL, 'n'},
830+
{"noconfig", no_argument, NULL, 'N'},
831+
{"part", required_argument, NULL, 'p'},
832+
{"chip", required_argument, NULL, 'p'},
833+
{"port", required_argument, NULL, 'P'},
834+
{"quiet", no_argument, NULL, 'q'},
835+
{"reconnect", no_argument, NULL, 'r'},
836+
{"terminal", no_argument, NULL, 't'},
837+
{"tty", no_argument, NULL, 't'},
838+
{"memory", required_argument, NULL, 'U'},
839+
{"verbose", no_argument, NULL, 'v'},
840+
{"noverify", no_argument, NULL, 'V'},
841+
{NULL, 0, NULL, 0}
842+
};
843+
while((ch = getopt_long(argc, argv,
844+
"?Ab:B:c:C:DeE:Fi:l:nNp:OP:qrtT:U:vVx:",
845+
longopts, NULL)) != -1) {
818846
switch(ch) {
819847
case 'b': // Override default programmer baud rate
820848
baudrate = str_int(optarg, STR_INT32, &errstr);

0 commit comments

Comments
 (0)