tom_modem: enhance user input parsing and update usage instructions

This commit is contained in:
fujr 2025-04-22 17:53:36 +08:00
parent 31a6343467
commit 39227f66d0
2 changed files with 25 additions and 1 deletions

View File

@ -1,4 +1,3 @@
#include "main.h" #include "main.h"
FDS_T s_fds; FDS_T s_fds;
@ -7,11 +6,28 @@ PROFILE_T s_profile; // global profile
int parse_user_input(int argc, char *argv[], PROFILE_T *profile) int parse_user_input(int argc, char *argv[], PROFILE_T *profile)
{ {
int opt = 1; int opt = 1;
int anonymous_arg = 0;
int option; int option;
profile->sms_index = -1; profile->sms_index = -1;
#define has_more_argv() (opt < argc ? 1 : 0) #define has_more_argv() (opt < argc ? 1 : 0)
while (opt < argc) while (opt < argc)
{ {
if (argv[opt][0] != '-') {
if (anonymous_arg == 0) {
profile->tty_dev = argv[opt];
}
if (anonymous_arg == 1){
profile->at_cmd = argv[opt];
}
if (anonymous_arg >= 2) {
err_msg("Too many anonymous arguments");
return INVALID_PARAM;
}
anonymous_arg++;
opt++;
continue;
}
option = match_option(argv[opt]); option = match_option(argv[opt]);
if (option == -1) if (option == -1)
{ {
@ -120,7 +136,13 @@ int parse_user_input(int argc, char *argv[], PROFILE_T *profile)
break; break;
} }
} }
// default settings: // default settings:
if (profile->tty_dev == NULL)
{
usage(argv[0]);
return INVALID_PARAM;
}
if (profile->baud_rate == 0 ) if (profile->baud_rate == 0 )
{ {
profile->baud_rate = 115200; profile->baud_rate = 115200;

View File

@ -390,6 +390,8 @@ void escape_json(char *input, char *output)
int usage(char* name) int usage(char* name)
{ {
err_msg("Usage: %s [options]", name); err_msg("Usage: %s [options]", name);
err_msg("Or %s [device_path] [AT command]", name);
err_msg("Or %s [device_path] [operation]", name);
err_msg("Options:"); err_msg("Options:");
err_msg(" -c, --at_cmd <AT command> AT command"); err_msg(" -c, --at_cmd <AT command> AT command");
err_msg(" -d, --tty_dev <TTY device> TTY device **REQUIRED**"); err_msg(" -d, --tty_dev <TTY device> TTY device **REQUIRED**");