nshlib/stdsession: Handle the command arguments in any order
@@ -69,6 +69,7 @@ int nsh_session(FAR struct console_stdio_s *pstate,
{
FAR struct nsh_vtbl_s *vtbl;
int ret = EXIT_FAILURE;
+ int i;
DEBUGASSERT(pstate);
vtbl = &pstate->cn_vtbl;
@@ -110,8 +111,52 @@ int nsh_session(FAR struct console_stdio_s *pstate,
#endif
}
- if (argc < 2)
+ /* Process the command line option */
+
+ for (i = 1; i < argc; i++)
+ {
+ if (strcmp(argv[i], "-h") == 0)
+ {
+ nsh_output(vtbl, "Usage: %s [<script-path>|-c <command>]\n",
+ argv[0]);
+ return EXIT_SUCCESS;
+ }
+ else if (strcmp(argv[i], "-c") == 0)
+ {
+ /* Process the inline command */
+
+ if (i + 1 < argc)
+ {
+ return nsh_parse(vtbl, argv[i + 1]);
+ }
+ else
+ {
+ nsh_error(vtbl, g_fmtargrequired, argv[0]);
+ return EXIT_FAILURE;
+ }
+ }
+ else if (argv[i][0] != '-')
+ {
+ break;
+ }
+
+ /* Unknown option */
+
+ nsh_error(vtbl, g_fmtsyntax, argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ if (i < argc)
{
+#ifndef CONFIG_NSH_DISABLESCRIPT
+ /* Execute the shell script */
+
+ return nsh_script(vtbl, argv[0], argv[i]);
+#else
+ return EXIT_FAILURE;
+#endif
+ }
+
/* Then enter the command line parsing loop */
for (; ; )
@@ -170,26 +215,6 @@ int nsh_session(FAR struct console_stdio_s *pstate,
nsh_parse(vtbl, pstate->cn_line);
}
- }
- else if (strcmp(argv[1], "-h") == 0)
- {
- ret = nsh_output(vtbl, "Usage: %s [<script-path>|-c <command>]\n",
- argv[0]);
- }
- else if (strcmp(argv[1], "-c") != 0)
- {
-#ifndef CONFIG_NSH_DISABLESCRIPT
- /* Execute the shell script */
-
- ret = nsh_script(vtbl, argv[0], argv[1]);
-#endif
- }
- else if (argc >= 3)
- {
- /* Parse process the command */
-
- ret = nsh_parse(vtbl, argv[2]);
- }
return ret;
}
