diff options
Diffstat (limited to 'jim-subcmd.c')
-rw-r--r-- | jim-subcmd.c | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/jim-subcmd.c b/jim-subcmd.c index 24af265..e246941 100644 --- a/jim-subcmd.c +++ b/jim-subcmd.c @@ -70,6 +70,21 @@ static void show_full_usage(Jim_Interp *interp, const jim_subcmd_type *ct, int a } } +static void add_cmd_usage(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv) +{ + Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_GetString(argv[0], NULL), NULL); + if (command_table->args && *command_table->args) { + Jim_AppendStrings(interp, Jim_GetResult(interp), " ", command_table->args, NULL); + } +} + +static void set_wrong_args(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv) +{ + Jim_SetResultString(interp, "wrong # args: must be \"", -1); + add_cmd_usage(interp, command_table, argc, argv); + Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); +} + const jim_subcmd_type * Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv) { @@ -169,7 +184,7 @@ Jim_ParseSubCmd(Jim_Interp *interp, const jim_subcmd_type *command_table, int ar /* Check the number of args */ if (argc - 2 < ct->minargs || (ct->maxargs >= 0 && argc - 2> ct->maxargs)) { - Jim_SetResultString(interp, "wrong # args: should be \"", -1); + Jim_SetResultString(interp, "wrong # args: must be \"", -1); add_subcmd_usage(interp, ct, argc, argv); Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); @@ -192,9 +207,7 @@ int Jim_CallSubCmd(Jim_Interp *interp, const jim_subcmd_type *ct, int argc, Jim_ ret = ct->function(interp, argc - 2, argv + 2); } if (ret < 0) { - Jim_SetResultString(interp, "wrong # args: should be \"", -1); - add_subcmd_usage(interp, ct, argc, argv); - Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); + set_wrong_args(interp, ct, argc, argv); ret = JIM_ERR; } } @@ -209,14 +222,6 @@ int Jim_SubCmdProc(Jim_Interp *interp, int argc, Jim_Obj *const *argv) } /* The following two functions are for normal commands */ -static void add_cmd_usage(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv) -{ - Jim_AppendStrings(interp, Jim_GetResult(interp), Jim_GetString(argv[0], NULL), NULL); - if (command_table->args && *command_table->args) { - Jim_AppendStrings(interp, Jim_GetResult(interp), " ", command_table->args, NULL); - } -} - int Jim_CheckCmdUsage(Jim_Interp *interp, const jim_subcmd_type *command_table, int argc, Jim_Obj *const *argv) { @@ -266,9 +271,7 @@ Jim_CheckCmdUsage(Jim_Interp *interp, const jim_subcmd_type *command_table, int /* Check the number of args */ if (argc - 1 < command_table->minargs || (command_table->maxargs >= 0 && argc - 1> command_table->maxargs)) { - Jim_SetResultString(interp, "wrong # args: should be \"", -1); - add_cmd_usage(interp, command_table, argc, argv); - Jim_AppendStrings(interp, Jim_GetResult(interp), "\"", NULL); + set_wrong_args(interp, command_table, argc, argv); Jim_AppendStrings(interp, Jim_GetResult(interp), "\"\nUse \"", Jim_GetString(argv[0], NULL), " -help\" for help", NULL); return JIM_ERR; } |