Added -h/-? option for help to all command line tools.

This commit is contained in:
Michael Ossmann
2017-01-29 17:37:29 -07:00
parent 980abca0b5
commit 9211b70770
8 changed files with 74 additions and 24 deletions

View File

@ -45,6 +45,7 @@ uint8_t data[MAX_XSVF_LENGTH];
static struct option long_options[] = { static struct option long_options[] = {
{ "xsvf", required_argument, 0, 'x' }, { "xsvf", required_argument, 0, 'x' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -79,7 +80,8 @@ int parse_int(char* s, uint32_t* const value)
static void usage() static void usage()
{ {
printf("Usage:\n"); printf("Usage:\n");
printf("\t-x <filename>: XSVF file to be written to CPLD.\n"); printf("\t-h, --help: this help\n");
printf("\t-x, --xsvf <filename>: XSVF file to be written to CPLD.\n");
printf("\t-d <serialnumber>: Serial number of device, if multiple devices\n"); printf("\t-d <serialnumber>: Serial number of device, if multiple devices\n");
} }
@ -97,7 +99,7 @@ int main(int argc, char** argv)
ssize_t bytes_read; ssize_t bytes_read;
uint8_t* pdata = &data[0]; uint8_t* pdata = &data[0];
while ((opt = getopt_long(argc, argv, "x:d:", long_options, while ((opt = getopt_long(argc, argv, "x:d:h?", long_options,
&option_index)) != EOF) { &option_index)) != EOF) {
switch (opt) { switch (opt) {
case 'x': case 'x':
@ -107,8 +109,13 @@ int main(int argc, char** argv)
case 'd': case 'd':
serial_number = optarg; serial_number = optarg;
break; break;
case 'h':
case '?':
usage();
return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@ -28,6 +28,7 @@
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n");
printf("\t-n, --register <n>: set register number for subsequent read/write operations\n"); printf("\t-n, --register <n>: set register number for subsequent read/write operations\n");
printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n");
printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n"); printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n");
@ -41,6 +42,7 @@ static struct option long_options[] = {
{ "register", required_argument, 0, 'n' }, { "register", required_argument, 0, 'n' },
{ "write", required_argument, 0, 'w' }, { "write", required_argument, 0, 'w' },
{ "read", no_argument, 0, 'r' }, { "read", no_argument, 0, 'r' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -127,16 +129,16 @@ int main(int argc, char** argv) {
int result = hackrf_init(); int result = hackrf_init();
if( result ) { if( result ) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
result = hackrf_open(&device); result = hackrf_open(&device);
if( result ) { if( result ) {
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
while( (opt = getopt_long(argc, argv, "n:rw:", long_options, &option_index)) != EOF ) { while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
case 'n': case 'n':
result = parse_int(optarg, &register_number); result = parse_int(optarg, &register_number);
@ -157,8 +159,14 @@ int main(int argc, char** argv) {
} }
break; break;
default: case 'h':
case '?':
usage(); usage();
return EXIT_SUCCESS;
default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage();
return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
@ -170,10 +178,10 @@ int main(int argc, char** argv) {
result = hackrf_close(device); result = hackrf_close(device);
if( result ) { if( result ) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
return 0; return EXIT_SUCCESS;
} }

View File

@ -33,6 +33,7 @@ typedef int bool;
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n");
printf("\t-s, --serial <s>: specify a particular device by serial number\n"); printf("\t-s, --serial <s>: specify a particular device by serial number\n");
printf("\t-d, --device <n>: specify a particular device by number\n"); printf("\t-d, --device <n>: specify a particular device by number\n");
printf("\t-o, --address <n>: specify a particular operacake by address [default: 0x00]\n"); printf("\t-o, --address <n>: specify a particular operacake by address [default: 0x00]\n");
@ -45,6 +46,7 @@ static struct option long_options[] = {
{ "device", no_argument, 0, 'd' }, { "device", no_argument, 0, 'd' },
{ "serial", no_argument, 0, 's' }, { "serial", no_argument, 0, 's' },
{ "address", no_argument, 0, 'o' }, { "address", no_argument, 0, 'o' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -78,7 +80,7 @@ int main(int argc, char** argv) {
return -1; return -1;
} }
while( (opt = getopt_long(argc, argv, "d:s:o:a:b:v", long_options, &option_index)) != EOF ) { while( (opt = getopt_long(argc, argv, "d:s:o:a:b:vh?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
case 'd': case 'd':
device_index = atoi(optarg); device_index = atoi(optarg);
@ -103,9 +105,15 @@ int main(int argc, char** argv) {
case 'v': case 'v':
verbose = 1; verbose = 1;
break; break;
case 'h':
case '?':
usage();
return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();
return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {

View File

@ -29,6 +29,7 @@
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n");
printf("\t-n, --register <n>: set register number for subsequent read/write operations\n"); printf("\t-n, --register <n>: set register number for subsequent read/write operations\n");
printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n");
printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n"); printf("\t-w, --write <v>: write register specified by last -n argument with value <v>\n");
@ -42,6 +43,7 @@ static struct option long_options[] = {
{ "register", required_argument, 0, 'n' }, { "register", required_argument, 0, 'n' },
{ "write", required_argument, 0, 'w' }, { "write", required_argument, 0, 'w' },
{ "read", no_argument, 0, 'r' }, { "read", no_argument, 0, 'r' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -128,16 +130,16 @@ int main(int argc, char** argv) {
int result = hackrf_init(); int result = hackrf_init();
if( result ) { if( result ) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
result = hackrf_open(&device); result = hackrf_open(&device);
if( result ) { if( result ) {
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
while( (opt = getopt_long(argc, argv, "n:rw:", long_options, &option_index)) != EOF ) { while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
case 'n': case 'n':
result = parse_int(optarg, &register_number); result = parse_int(optarg, &register_number);
@ -157,9 +159,15 @@ int main(int argc, char** argv) {
result = dump_register(device, register_number); result = dump_register(device, register_number);
} }
break; break;
case 'h':
case '?':
usage();
return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();
return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
@ -172,10 +180,10 @@ int main(int argc, char** argv) {
result = hackrf_close(device); result = hackrf_close(device);
if( result ) { if( result ) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
return 0; return EXIT_SUCCESS;
} }

View File

@ -33,6 +33,7 @@ typedef int bool;
static void usage() { static void usage() {
printf("\nUsage:\n"); printf("\nUsage:\n");
printf("\t-h, --help: this help\n");
printf("\t-c, --config: print textual configuration information\n"); printf("\t-c, --config: print textual configuration information\n");
printf("\t-n, --register <n>: set register number for subsequent read/write operations\n"); printf("\t-n, --register <n>: set register number for subsequent read/write operations\n");
printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n");
@ -52,6 +53,7 @@ static struct option long_options[] = {
{ "read", no_argument, 0, 'r' }, { "read", no_argument, 0, 'r' },
{ "device", no_argument, 0, 'd' }, { "device", no_argument, 0, 'd' },
{ "serial", no_argument, 0, 's' }, { "serial", no_argument, 0, 's' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -209,10 +211,10 @@ int main(int argc, char** argv) {
int result = hackrf_init(); int result = hackrf_init();
if( result ) { if( result ) {
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
while( (opt = getopt_long(argc, argv, "d:s:cn:rw:", long_options, &option_index)) != EOF ) { while( (opt = getopt_long(argc, argv, "d:s:cn:rw:h?", long_options, &option_index)) != EOF ) {
switch( opt ) { switch( opt ) {
case 'n': case 'n':
result = parse_int(optarg, &register_number); result = parse_int(optarg, &register_number);
@ -237,9 +239,15 @@ int main(int argc, char** argv) {
case 's': case 's':
serial_number = optarg; serial_number = optarg;
break; break;
case 'h':
case '?':
usage();
return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();
return EXIT_FAILURE;
} }
if( result != HACKRF_SUCCESS ) { if( result != HACKRF_SUCCESS ) {
@ -261,7 +269,7 @@ int main(int argc, char** argv) {
if( result ) { if( result ) {
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
if(write) { if(write) {
@ -282,10 +290,10 @@ int main(int argc, char** argv) {
result = hackrf_close(device); result = hackrf_close(device);
if( result ) { if( result ) {
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
return -1; return EXIT_FAILURE;
} }
hackrf_exit(); hackrf_exit();
return 0; return EXIT_SUCCESS;
} }

View File

@ -52,6 +52,7 @@ static struct option long_options[] = {
{ "read", required_argument, 0, 'r' }, { "read", required_argument, 0, 'r' },
{ "write", required_argument, 0, 'w' }, { "write", required_argument, 0, 'w' },
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "help", no_argument, 0, 'h' },
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 },
}; };
@ -86,6 +87,7 @@ int parse_u32(char* s, uint32_t* const value)
static void usage() static void usage()
{ {
printf("Usage:\n"); printf("Usage:\n");
printf("\t-h, --help: this help\n");
printf("\t-a, --address <n>: starting address (default: 0)\n"); printf("\t-a, --address <n>: starting address (default: 0)\n");
printf("\t-l, --length <n>: number of bytes to read (default: %d)\n", MAX_LENGTH); printf("\t-l, --length <n>: number of bytes to read (default: %d)\n", MAX_LENGTH);
printf("\t-r <filename>: Read data into file.\n"); printf("\t-r <filename>: Read data into file.\n");
@ -115,7 +117,7 @@ int main(int argc, char** argv)
bool verbose = false; bool verbose = false;
bool reset = false; bool reset = false;
while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vR", long_options, while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vRh?", long_options,
&option_index)) != EOF) { &option_index)) != EOF) {
switch (opt) { switch (opt) {
case 'a': case 'a':
@ -148,9 +150,10 @@ int main(int argc, char** argv)
reset = true; reset = true;
break; break;
case 'h':
case '?': case '?':
usage(); usage();
return EXIT_FAILURE; return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);

View File

@ -235,6 +235,7 @@ int rx_callback(hackrf_transfer* transfer) {
static void usage() { static void usage() {
fprintf(stderr, "Usage:\n"); fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t[-h] # this help\n");
fprintf(stderr, "\t[-d serial_number] # Serial number of desired HackRF.\n"); fprintf(stderr, "\t[-d serial_number] # Serial number of desired HackRF.\n");
fprintf(stderr, "\t[-a amp_enable] # RX/TX RF amplifier 1=Enable, 0=Disable.\n"); fprintf(stderr, "\t[-a amp_enable] # RX/TX RF amplifier 1=Enable, 0=Disable.\n");
fprintf(stderr, "\t[-f freq_min:freq_max # Specify minimum & maximum sweep frequencies (MHz).\n"); fprintf(stderr, "\t[-f freq_min:freq_max # Specify minimum & maximum sweep frequencies (MHz).\n");
@ -276,7 +277,7 @@ int main(int argc, char** argv) {
uint16_t frequencies[MAX_FREQ_COUNT]; uint16_t frequencies[MAX_FREQ_COUNT];
uint32_t num_samples = DEFAULT_SAMPLE_COUNT; uint32_t num_samples = DEFAULT_SAMPLE_COUNT;
while( (opt = getopt(argc, argv, "a:f:p:l:g:x:d:n:")) != EOF ) { while( (opt = getopt(argc, argv, "a:f:p:l:g:x:d:n:h?")) != EOF ) {
result = HACKRF_SUCCESS; result = HACKRF_SUCCESS;
switch( opt ) switch( opt )
{ {
@ -325,6 +326,11 @@ int main(int argc, char** argv) {
result = parse_u32(optarg, &num_samples); result = parse_u32(optarg, &num_samples);
break; break;
case 'h':
case '?':
usage();
return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
usage(); usage();

View File

@ -469,6 +469,7 @@ int tx_callback(hackrf_transfer* transfer) {
static void usage() { static void usage() {
printf("Usage:\n"); printf("Usage:\n");
printf("\t-h # this help\n");
printf("\t[-d serial_number] # Serial number of desired HackRF.\n"); printf("\t[-d serial_number] # Serial number of desired HackRF.\n");
printf("\t-r <filename> # Receive data into file (use '-' for stdout).\n"); printf("\t-r <filename> # Receive data into file (use '-' for stdout).\n");
printf("\t-t <filename> # Transmit data from file (use '-' for stdin).\n"); printf("\t-t <filename> # Transmit data from file (use '-' for stdin).\n");
@ -544,7 +545,7 @@ int main(int argc, char** argv) {
float time_diff; float time_diff;
unsigned int lna_gain=8, vga_gain=20, txvga_gain=0; unsigned int lna_gain=8, vga_gain=20, txvga_gain=0;
while( (opt = getopt(argc, argv, "Hwr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:d:C:RS:")) != EOF ) while( (opt = getopt(argc, argv, "Hwr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:d:C:RS:h?")) != EOF )
{ {
result = HACKRF_SUCCESS; result = HACKRF_SUCCESS;
switch( opt ) switch( opt )
@ -673,9 +674,10 @@ int main(int argc, char** argv) {
result = parse_u32(optarg, &crystal_correct_ppm); result = parse_u32(optarg, &crystal_correct_ppm);
break; break;
case 'h':
case '?': case '?':
usage(); usage();
return EXIT_FAILURE; return EXIT_SUCCESS;
default: default:
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);